C language implementation of the basic operation of single-linked list (lead node)

Source: Internet
Author: User

In my previous blog, "C language implementation of single-linked list (do not take the lead node)," The implementation of a single-linked list of non-lead node 11 operations: such as calculating the length of the list, initialization, create linked list, empty list and so on. However, in actual use, the single-linked list of the leading nodes is often used more and more conveniently than the single-linked list without the leading nodes. Because the first node does not have to be considered separately, the first node and the other subsequent nodes are treated the same, simplifying the operation. This blog will come up with 11 operations for the single-linked list of leading nodes. Code upload to: Https://github.com/chenyufeng1991/LinkedList_HeadNode.

(1) Define the node type of the lead-node single-linked list

typedef int ELEMTYPE;TYPEDEF struct nodelist{    int element;    struct NodeList *next;} Node;

(2) Initialize the single linked list of lead nodes "This is very important."

1. Initialize the single-linked list of lead nodes void Initiallist (Node **pnode) {    //personally suggest that every time malloc allocates memory space, it should be judged whether the allocation is successful or not;    At this point the Pnode is a head node;    //After initialization succeeds, it is actually equivalent to a normal linked list;    *pnode = (node *) malloc (sizeof (node));    if (*pnode = = NULL) {        printf ("%s function performed, memory allocation failed, initialization of single-linked list failed", __function__);    } else{        (*pnode)->next = NULL;        printf ("%s function execution, single-linked list initialization of the lead node \ n", __function__);}    }

(3) The tail interpolation method creates a single linked list of leading nodes.

2. Create a single linked list of lead nodes void CreateList (Node *pnode) {    /**     *  Even if the first input number is less than or equal to 0, the single linked list of the lead nodes will be created successfully, but the single linked list is empty. That is, there is no other node in it except for the head node.     */    Node *pinsert;    Node *pmove;    Pinsert = (node *) malloc (sizeof (node));//need to detect if allocating memory is successful Pinsert = = NULL  ?    memset (pinsert, 0, sizeof (Node));    Pinsert->next = NULL;    scanf ("%d",& (pinsert->element));    Pmove = Pnode;    while (Pinsert->element > 0) {        pmove->next = Pinsert;        Pmove = Pinsert;        Pinsert = (node *) malloc (sizeof (node)); Need to detect if allocating memory succeeded Pinsert = = NULL  ?        memset (pinsert, 0, sizeof (Node));        Pinsert->next = NULL;        scanf ("%d",& (pinsert->element));    }    printf ("%s function execution, single-linked list of lead nodes created successfully \ n", __function__);}

(4) Print a single linked list of lead nodes

3. Print lead node single-stranded list void printlist (Node *pnode) {    /**     *  Note here, if the single-linked list is empty (only one header node), I also let it print (print success). It's just that there are no elements, it's empty, so it's empty on the console;     */        Node *pmove;        Pmove = pnode->next;        while (pmove! = NULL) {            printf ("%d", pmove->element);            Pmove = pmove->next;        }        printf ("\n%s function execution, print lead node single-linked list succeeded \ n", __function__);}

(5) Clear list

4. Clears all elements in the linear table, i.e. releases all nodes (except the head node), making it an empty table void Clearlist (node *pnode) {    node *pmove;    Pmove = pnode->next;    while (pmove! = NULL) {        Pnode->next = pmove->next;        Free (pmove);        Pmove = pnode->next;    }    printf ("%s function execution, empty list of lead nodes successfully \ n", __function__);}

(6) Calculating the chain table length

5. Returns the length of the single-linked list of lead nodes int sizelist (Node *pnode) {    /**     *  when there is only one head node, size = 0; The head node is not counted in the list length.     */    int i = 0;    Node *pmove;    Pmove = pnode->next;    while (pmove! = NULL) {        i++;        Pmove = pmove->next;    }    printf ("%s" function execution, the length of the single-linked list of the lead nodes is:%d\n ", __function__,i);    return i;}

(7) Determine if the linked list is empty

6. Determine if the single-linked list of lead nodes is empty, or null to return 1, otherwise return 0int isemptylist (Node *pnode) {    /**     *  when there is only one head node, the list is empty     */    if (Pnode->next = = NULL) {        printf ("%s" function executed, the list of leading nodes is empty \ n ", __function__);        return 1;    }    printf ("%s function execution, the list of leading nodes is not empty \ n", __function__);    return 0;}


(8) Find a location element in a linked list

7. Returns the element in the POS node of the single-linked list, if 1 is returned, indicating that the int getelement (Node *pnode,int pos) {    int i = 1 is not found;    Node *pmove;    Pmove = pnode->next;    while (pmove! = NULL) {        if (i = = pos) {            printf ("%s function execution, pos=%d position value is%d\n", __function__,pos,pmove->element);            Return pmove->element;        }        i++;        Pmove = pmove->next;    }    printf ("%s function performed, no value found at pos=%d location \ n", __function__,pos);    return-1;}

(9) Returns the memory address of an element in the linked list

8. Find the first element with the given value x from a single-linked list, and return the storage address of the data domain of the node if the lookup succeeds, otherwise return nullelemtype* getelemaddr (node *pnode,int x) {    node *pmove;    Pmove = pnode->next;    while (pmove! = NULL) {        if (pmove->element = = x) {            printf ("%s" function performed, found to x=%d memory address is: 0x%x\n ", __function__,x, & (Pmove->element));            Return & (Pmove->element);        }        Pmove = pmove->next;    }    printf ("%s" function execution, in the single linked list of the lead node did not find the value of x=%d, unable to obtain the memory address \ n ", __function__,x);    return NULL;}

(10) Modify the value of a node

9. Change the value of POS node in the single-linked list to the value of x node* Modifyelem (Node *pnode,int pos,int x) {    int i = 1;    Node *pmove;    Pmove = pnode->next;    while (pmove! = NULL) {        if (i = = pos) {            pmove->element = x;            printf ("%s function execution, change the value of pos=%d location to%d success \ n", __function__,pos,x);            return pnode;        }        i++;        Pmove = pmove->next;    }    printf ("%s" function is executed, the linked list is empty or the input POS value is illegal, the modification value fails \ n ", __function__);    return pnode;}

(11) Table header Insert a node

10. Insert an element into the table header of the single-linked list node *insertheadlist (node *pnode,int x) {    node *pinsert;    Pinsert = (node *) malloc (sizeof (node));    memset (pinsert, 0, sizeof (Node));    pinsert->element = x;    Pinsert->next = pnode->next;    Pnode->next = Pinsert;    printf ("%s" function performed, insert element%d in table header succeeded \ n ", __function__,x);    return pnode;}

(12) Insert a node at the end of the table

11. Add an element to the end of the single-linked list node *inserttaillist (node *pnode,int x) {    node *pmove;    Node *pinsert;    Pinsert = (node *) malloc (sizeof (node));    memset (pinsert, 0, sizeof (Node));    pinsert->element = x;    Pinsert->next = NULL;    Pmove = Pnode;    while (pmove->next! = NULL) {        pmove = pmove->next;    }    Pmove->next = Pinsert;    printf ("%s function executed, insert element%d successfully \ n" At the end of the table, __function__,x);    return pnode;}

(13) Test function

int main (int argc, const char * argv[]) {    Node *plist;    Initiallist (&plist);    CreateList (pList);    Printlist (pList);    Sizelist (pList);    Isemptylist (pList);    GetElement (PList, 3);    Getelemaddr (PList, 5);    Modifyelem (PList, 2, 111);    Printlist (pList);    Insertheadlist (plist,1234);    Printlist (pList);    Sizelist (pList);    Inserttaillist (plist,9999);    Printlist (pList);    Sizelist (pList);    Clearlist (pList);    Printlist (pList);    return 0;}


C language implementation of the basic operation of single-linked list (lead node)

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.