The basic operation of C language to realize bidirectional non-cyclic chain list (lead node end node)

Source: Internet
Author: User

I have a blog in the "C language to achieve a two-way non-circular chain list (not the lead node) of the basic operation" in detail to achieve the end of the head of the two-way non-cyclic chain of the list of many operations. In fact, like a single linked list, do not lead the linked list of many operations are more cumbersome, often need to do the first node to make additional judgments, improve the cost of error. Today we are going to implement the operation of the bidirectional non cyclic linked list of the leading node tail node, although the two nodes are maintained, the simplicity of the operation is greatly improved. Code uploaded to Https://github.com/chenyufeng1991/DoubleLinkedList_HeadList.

(1) The node type of the non-cyclic bidirectional linked list defining the end node of the leading node

typedef int ELEMTYPE;
typedef struct nodelist{

    int element;
    struct nodelist *prior;
    struct nodelist *next;
} Node;

(2) Initialization of the double linked list

1. Non-cyclic bidirectional linked list
void initiallist (node **phead,node **ptail) {

    *phead = (node *) malloc (node) for initialization of the leading node and tail node;
    *ptail = (node *) malloc (sizeof (node));

    if (*phead = null | | *ptail = NULL) {
        printf ("%s function execution, memory allocation failure, initialization of double linked list failed \ \", __function__);
    } else{
        //This inside is the key, but also the important condition of the short term
        (*phead)->prior = null;
        (*ptail)->next = NULL;

        When the list is empty, the head node and the tail knot are connected
        (*phead)->next = *ptail;
        (*ptail)->prior = *phead;

        printf ("%s function execution, leading node and tail node's bidirectional non-cyclic list initialization succeeded \ n", __function__);
    }

(3) The tail inserts the method to create the double linked list

2. Create the bidirectional non cyclic chain table
void createlist (node *phead,node *ptail) {

    node *pinsert of the leading node and tail node;
    Node *pmove;
    Pinsert = (node*) malloc (sizeof (Node));
    memset (pinsert, 0, sizeof (Node));
    Pinsert->prior = NULL;
    Pinsert->next = NULL;

    scanf ("%d",& (pinsert->element));
    Pmove = Phead;
    while (Pinsert->element > 0) {

        pmove->next = Pinsert;
        Pinsert->prior = Pmove;
        Pinsert->next = Ptail;
        Ptail->prior = Pinsert;
        Pmove = Pinsert;

        Pinsert = (node *) malloc (sizeof (node));
        memset (pinsert, 0, sizeof (Node));
        Pinsert->prior = NULL;
        Pinsert->next = NULL;

        scanf ("%d",& (pinsert->element));
    }

    printf ("%s function execution completed, lead node and end point bidirectional non-cyclic list creation succeeded \ n", __function__);

}

(4) Positive sequence printing chain list

3. Positive sequence print chain table
void printlist (node *phead,node *ptail) {

    node *pmove;
    Pmove = phead->next;
    while (Pmove!= ptail) {
        printf ("%d", pmove->element);
        Pmove = pmove->next;
    }

    printf ("\n%s function execution, positive sequence printing leading node tail node in bidirectional non circular chain table creation success \ n", __function__);
}


(5) Reverse Print List

4. Reverse Print List
void printreverselist (node *phead,node *ptail) {

    node *pmove;
    Pmove = ptail->prior;
    while (Pmove!= phead) {
        printf ("%d", pmove->element);
        Pmove = pmove->prior;
    }

    printf ("\n%s function execution, reverse-order printing of the two-way non-cyclic chain table of the lead node" created successfully \ n ", __function__);
}

(6) Empty the node to make it an empty table

5. Clears all elements in the list to make null table
void clearlist (node *phead,node *ptail) {

    node *pmove;
    Pmove = phead->next;

    while (Pmove!= ptail) {

        Phead->next = pmove->next;
        Pmove->next->prior = Phead;
        Free (pmove);
        Pmove = phead->next;
    }

    printf ("%s function execution, two-way non-cyclic list emptying succeeded \ n", __function__);
}

(7) Calculate the length of the linked list

6. Calculate the length of the list
int sizelist (Node *phead,node *ptail) {

    int i = 0;
    Node *pmove;
    Pmove = phead->next;
    while (Pmove!= ptail) {
        i++;
        Pmove = pmove->next;
    }
    printf ("%s function executes, the length of the list is%d\n", __function__,i);

    return i;
}

(8) to determine whether the linked list is empty

7. To determine the lead node in the two-way non-cyclic linked list is null, NULL return 1, otherwise return 0
int isemptylist (Node *phead,node *ptail) {
    if (Phead->next = = Ptail) {

        printf ("%s function execution, current list is empty \ \ __function__");
        return 1;
    }

    printf ("%s function executes, current list is not empty \ n", __function__);
    return 0;
}

(9) Returns the elements of the POS position in the list

8. Returns the elements of the POS node in the list, and returns-1, indicating that the
int getelement (Node *phead,node *ptail,int pos) {

    int i = 1
    ) is not found. Node *pmove;
    Pmove = phead->next;
    while (Pmove!= ptail) {
        if (i = = pos) {
            printf ("%s function execution, pos=%d position element is%d\n",__function__,pos,pmove-> Element);
            Return pmove->element;
        }
        i++;
        Pmove = pmove->next;
    }

    printf ("%s function execution, find pos=%d position element failed \ \", __function__,pos);

    return-1;
}

(10) Find a node with a value of X, and return the address if present

9. Find the first element of the given value x from the list and return the memory address of the data field, otherwise return the null
int *getelemaddr (node *phead,node *ptail,int x) {

    node *pmove;
    Pmove = phead->next;
    while (Pmove!= ptail) {
        if (pmove->element = = x) {
            printf ("%s function executes, the value%d of the element memory address is 0x%x\n",__function__,x,& (pmove->element));
            Return & (Pmove->element);
        }
        Pmove = pmove->next;
    }

    printf ("%s function execution, find element address with%d value failed \ \ \ n", __function__,x);

    return NULL;
}

(11) Change the value of the POS node to X

10. Modify the value of the POS node in the list to x
int Modifyelem (node *phead,node *ptail,int pos,int x) {

    int i = 1;
    Node *pmove;
    Pmove = phead->next;
    while (Pmove!= ptail) {
        if (i = = pos) {

            pmove->element = x;
            printf ("%s function executes, modifying pos=%d location value is%d successful \ \", __function__,pos,x);
            return 1;
        }
        i++;
        Pmove = pmove->next;
    }

    printf ("%s function execution, modifying pos=%d location element failed \ \", __function__,pos);

    return-1;
}

(12) Table header inserts an element

11. Insert an element
int insertheadlist (node *phead,node *ptail,int x) {

    node *pinsert to the table header of the list;
    Pinsert = (node *) malloc (sizeof (node));
    memset (pinsert, 0, sizeof (Node));
    pinsert->element = x;
    Pinsert->prior = NULL;
    Pinsert->next = NULL;

    Pinsert->next = phead->next;
    Phead->next->prior = Pinsert;
    Phead->next = Pinsert;
    Pinsert->prior = Phead;

    printf ("%s function executes, insert%d in header \ \ n", __function__,x);
    return 1;
}

(13) Insert an element at the end of the table

12. Insert an element
int inserttaillist (node *phead,node *ptail,int x) {

    node *pinsert to the footer of the list;
    Pinsert = (node *) malloc (sizeof (node));
    memset (pinsert, 0, sizeof (Node));
    pinsert->element = x;
    Pinsert->prior = NULL;
    Pinsert->next = NULL;

    Ptail->prior->next = Pinsert;
    Pinsert->prior = ptail->prior;
    Pinsert->next = Ptail;
    Ptail->prior = Pinsert;

    printf ("%s function execution, insert%d at footer \ n", __function__,x);

    return 1;
}

(14) Test code

int main (int argc, const char * argv[]) {

    node *phead;//header node
    *ptail;//tail node

    initiallist (&phead, & Ptail);

    CreateList (Phead, ptail);
    Printlist (Phead, ptail);
    Printreverselist (phead,ptail);

    Sizelist (Phead, ptail);

    Isemptylist (phead,ptail);

    GetElement (Phead, Ptail, 2);

    Getelemaddr (Phead, Ptail, 5);

    Modifyelem (Phead, Ptail, 2,);
    Printlist (Phead, ptail);
    
    Insertheadlist (phead,ptail,100);
    Printlist (Phead, ptail);

    Inserttaillist (phead,ptail,900);
    Printlist (Phead, ptail);
    
    Clearlist (phead,ptail);
    Printlist (Phead, ptail);
    Isemptylist (phead,ptail);
    
    return 0;
}


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.