The first write code of the blog, a new contact with the novice, to come here mainly to record themselves, convenient for their later browsing, but also welcome correction. Let's start with a simple, dynamic list creation and traversal.
#include <stdio.h>#include<stdlib.h>#include<malloc.h>//Define a node for a linked listtypedefstructlnode{intdata; structLnode *Next;} *linklist;//Create a linked list functionlinklist createlist () {linklist ltail, lhead, p; intI, length,input; Lhead= (linklist) malloc (sizeof(Lnode));//Create a table header node if(! Lhead) Exit (0);//determine if the table header was created successfullyLtail = Lhead;//Define a footerLtail->next = NULL;//the pointer at the end of the footer assigns nullprintf"Please enter the number of nodes you need: \ n"); scanf_s ("%d", &length); for(i =0; i < length; i++) {printf ("Please enter data for%d nodes", i +1); scanf_s ("%d", &input); P= (linklist) malloc (sizeof(Lnode));//to create a new node if(!p) Exit (0); P->data = input;//assigns the input value to the new nodeP->next = NULL;//because the new node is going to be the footer of the list, its pointer is given NULL.Ltail->next = p;//connect the footer to the new nodeLtail = p;//assign a new node to Ltail to become the new footer } returnLhead;//return to table header}//traversing linked list functionsvoidtravellist (linklist Head) {linklist P= head->next;//start traversing from the next node of the linked list head node while(P! = NULL)//stop traversal When the list is empty{printf ("%d\t", p->data); P= p->next;//move one node backwards }}intMain () {linklist head; Head=createlist (); Travellist (head); return 0;}
Creation and traversal of the [data structure] "C language" list