寒假作业4

单向链表

typedef int data_type;

typedef struct Node
{
    data_type data;
    struct Node *next;
}*Linklist;

Linklist create_node()
{
    Linklist s=(Linklist)malloc(sizeof(struct Node));
    if (NULL == s)
    {
        return NULL;
    }
    s->data = 0;
    s->next = NULL;
    return s;
    
}

int insert_head(data_type element,Linklist *head)
{
    Linklist s=create_node();
    if (NULL == s)
    {
        return -1;
    }
    s->data = element;

    if (NULL == *head)
    {
        *head = s;
    }
    else
    {
        s->next = *head;
        *head = s;
    }
    return 0;
    
}
int insert_tail(data_type element,Linklist *head)
{
    Linklist s=create_node();
    if (NULL == s)
    {
        return -1;
    }
    s->data = element;
    Linklist p = *head;
    
    if (NULL == p)
    {
        *head = s;
    }
    else
    {
        while (NULL != p->next)
        {
            p = p->next; 
        }
        p->next = s;
    } 
    return 0;
}

双向链表

typedef char data_type[30];

typedef struct Node
{
    data_type data;
    struct Node *next;
    struct Node *priv;
}*DoubleLink;

DoubleLink create_node()
{
    DoubleLink s=(DoubleLink)malloc(sizeof(struct Node));
    if (NULL == s)
    {
        return NULL;
    }
    strcpy(s->data,"");
    s->next = NULL;
    s->priv = NULL;
    return s;
    
}
int insert_head(data_type element,DoubleLink *head)
{
    DoubleLink s = create_node();
    if (NULL == s)
    {
        return -1;
    }
    strcpy(s->data,element);    

    if (NULL == *head)
    {
        *head = s;
    }
    else
    {
        s->next = *head;
        (*head)->priv = s;
        *head = s;
    }
    return 0;
}
int insert_tail(data_type element,DoubleLink *head)
{
    DoubleLink s = create_node();
    if (NULL == s)
    {
        return -1;
    }
    strcpy(s->data,element);   

    if (NULL == *head)
    {
        *head = s;
    }
    else
    {
        DoubleLink p = *head;
        while (NULL != p->next)
        {
            p = p->next;
        }
        p->next = s;
        s->priv = p;
    }
    return 0;
}

你可能感兴趣的:(java,前端,服务器)