Linked list: Create a simple linked list and output its contents

Keywords: C

Professional terms for lists:

The first node: the node that stores the first valid data
Tail node: the node that holds the last valid data
Head Node:
1. The data type of the header node is identical to that of the header node.
2. The header node is the node in front of the header node.
3. Header nodes do not store valid data
4. The purpose of setting header nodes is to facilitate the operation of linked lists.
Header pointer: A pointer variable that stores the address of the header node

Use a picture to show the basic framework of the list:

The pointer field of the tail node is empty and can be expressed as NULL

To determine a linked list, the most basic information is the header pointer, after which information can be found through the header pointer.

Next, write the program, only with practical examples, in order to understand more comprehensively! ___________

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 struct Node                          //Define a linked list structure 
 4 {
 5     int data;                        //Data of linked list nodes 
 6     struct Node * pNext;             //A pointer to the next node in a linked list 
 7 };
 8 int i;
 9 struct Node * create_list(void);     //Function declarations for creating linked lists 
10 void traverse_list(struct Node *);   //Function declarations for printing linked lists 
11 int main()
12 {
13     struct Node * pHead = NULL;     //First assign a value to the header pointer NULL 
14     pHead = create_list();          //Call the linked list to create a function and assign the value of the header pointer to it pHead 
15     traverse_list(pHead);           //Print list
16     return 0;
17 }
18 struct Node * create_list(void)     //Functions to create linked lists
19 {
20     int len;                        //Number of nodes in linked list 
21     int val;                        //Values of data fields in linked list nodes 
22     struct Node * pHead=(struct Node *)malloc(sizeof(struct Node));  //Dynamic allocation of memory for header nodes 
23     if(pHead == NULL)
24     {
25         printf("Memory allocation failed, program terminated!\n");
26         exit(-1);
27     }
28     struct Node * pTail = pHead;                        //Define a tail node pointer that will pHead The value assigned to it 
29     pTail->pNext = NULL;                                //The pointer field of the tail node must be empty 
30     printf("Enter the number of linked list nodes you need to generate: len =");
31     scanf("%d",&len);
32     for (i=0;i<len;i++)
33     {
34         printf("Please input number 1.%d Values of individual nodes:",i+1);
35         scanf("%d",&val);
36         struct Node * pNew=(struct Node *)malloc(sizeof(struct Node));  //Dynamic allocation of memory for new nodes
37         if(pNew== NULL)
38     {
39         printf("Memory allocation failed, program terminated!\n");
40         exit(-1);
41     }
42         pNew->data = val;       //Pass the input value to*pNew Data domain 
43         pNew->pNext = NULL;     // *pNew It's a new tail node, so pNew->pNext It should be empty.
44         pTail->pNext = pNew;    //hold*pNew Address passed to pTail->pNext Pointer field, equivalent to*pTail.pNext=pNew
45                                 //First time pTail->pNext = pNew It is equivalent to giving the address of the first node to the first node.
46                                 
47         pTail = pNew;           48                                 //Do this.*pNew It's new.*pTail 
49                                 //Subsequent pTail = pNew Let the last node connect to the last one.     
50      } 
51     return pHead;               //Return pHead Value 
52 }
53 void traverse_list(struct Node * pHead)  //Traversing linked list 
54 {
55     struct Node * p = pHead->pNext;      //Define a pointer to the next node 
56     while(p!=NULL)                       //The pointer field of the tail node must be NULL,If not NULL,Continue printing 
57     {
58         printf("%d\t",p->data);
59         p = p->pNext;                    //The address of the next node is assigned to p 
60     }
61     return;                              //Loop end 
62 }

In this program, I think about 44 lines and 47 lines of code for a long time.

At first, I always felt that there were 44 lines of code pTail - > pNext = pNew, 47 lines of code pTail = pNew repeated, after deletion, found no way, the following differences between the two are illustrated and analyzed:

 

 

Step 1: * pTail's address is pHead, so the pHead points to * pTail, and the newly created * pNew has not yet been assigned.

Step 2: val is assigned to * pNew's data domain, NULL is assigned to * pNew's pointer domain, and * pNew's address is passed to * pTail's pointer domain.

Step 3: Assign the address of * pNew to pTail to make * pNew a new * pTail

This is roughly the whole process. Understanding, in fact, is not difficult. To master more knowledge, we need to further study the data structure.

Posted by fitzromeo on Sat, 02 Feb 2019 13:21:15 -0800