Simple implementation tutorial of one-way linked list in C language and C Language
Simple implementation of a C-language one-way linked list
# Include
# Include
// Define a struct representing the linked list node struct node {int data; // the data stored in the node struct node * next; // pointer to the next node }; // For the convenience of providing a new node type name Lnode typedef struct node Lnode; int main () {// define three pointers 1. head: Header pointer; 2.P: obtains the new memory space each time through this pointer; // 3. p1: Used as the memory connection node; Lnode * head, * p1, * p; // gets the header node. At this time, the three pointers point to the same memory space head = (Lnode *) malloc (sizeof (Lnode); p = head; p1 = head; // enter an integer and save it to scanf ("% d", & p-> data) in the header node data ); // set to exit the loop when the data field data is 0, that is, when the user inputs 0, and the linked list is established and ended while (p-> data! = 0) {// get new memory space p = (Lnode *) malloc (sizeof (Lnode); // connection node p1-> next = p; // use p1 to trace the current last node p1 = p; // store the node data scanf ("% d", & p-> data );}; // point the pointer variable at the end node to NULL p-> next = NULL; // Point p1 to the header node for the traversal Link Table p1 = head; // The traversal chain table while (p1-> next! = NULL) {printf ("% d \ n", p1-> data); p1 = p1-> next;}; return 0 ;}
Compile and run the code, and enter1, 2, 3, 4, 5, 0The running result is as follows: