Creation and Addition of 4-18 Mistake-prone Point List

Keywords: C Programming

Today, I learned how to create and add links. First, I summarized the mistakes I made.

  1. The second-level pointer is not well understood, which leads to no change in the value of the argument after passing it on.
  2. Symbol priority is not remembered skillfully, which leads to many grammatical problems after writing.

First up code

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 
 4 typedef struct NODE
 5 {
 6     int id;
 7     char *name;
 8     char *tel;
 9     struct NODE *next;
10 }Node;
11 Node *GetNode(int id,char *name,char *tel);
12 void addNode(Node **ppHead,Node **ppEnd,Node *pNode);
13 int main()
14 {
15     Node *pHead = NULL;     //1
16     Node *pEnd = NULL;
17 
18     addNode(&pHead,&pEnd,GetNode(1,"cyc","164864613521"));
19     addNode(&pHead,&pEnd,GetNode(2,"xmx","164864613521"));
20     addNode(&pHead,&pEnd,GetNode(3,"wdh","164864613521"));
21     addNode(&pHead,&pEnd,GetNode(4,"yk","164864613521"));
22 
23     while(pHead != NULL)
24     {
25         printf("%d    %s    %s\n",pHead->id,pHead->name,pHead->tel);
26         pHead = pHead->next;
27     }
28 
29 
30 
31     return 0;
32 }
33 Node *GetNode(int id,char *name,char *tel)
34 {
35     Node *pTemp = (Node *)malloc(sizeof(Node));
36     pTemp->id = id;
37     pTemp->name = name;
38     pTemp->tel = tel;
39     pTemp->next = NULL;
40 
41     return pTemp;
42 }
43 void addNode(Node **ppHead,Node **ppEnd,Node *pNode)   // 2
44 {
45     if(*ppHead == NULL)
46     {
47         *ppHead = pNode;
48         *ppEnd = pNode;
49     }
50     else
51     {
52         (*ppEnd)->next = pNode;
53         *ppEnd = pNode;
54     }
55     return;
56 
57 }

Code comments:

  1. The head pointer and tail pointer set here are convenient for adding nodes.
  2. Function parameterization, if you want to change the value of the incoming parameter, you should use address transfer, instead, use value transfer (in this exercise, I want to change the value of the pointer, so use address transfer, and secondary pointer is exactly the address used to store the pointer)

Idea analysis:

This exercise begins with the creation of a single node in a simple linked list in a data structure. After analysis, it can be concluded that if you want to create and add operations in the process of running the program:

  1. The space should be applied in the heap area, because the size of the stack area can not be changed after the program runs, and will disappear with the end of the custom function, resulting in the loss of information;
  2. There are many forms of input parameters. I use either direct input or scanf () real-time input.

 

The simple addition of linked list can be divided into two cases:

  1. There is no linked list in the program, so it can be judged by traversing the header pointer pHead = 0, then the header pointer points to the added node, and the tail pointer points to the added node.

If a linked list has already appeared in the program, then the head pointer can be pointed to the first node unchanged. At this time, the tail pointer can point to the added node stored in the node, and then continue pointing the tail pointer to the last node.

 

2019-04-18 22:37:16 Programming novice reflection, big man do not spray thank you!

Posted by soulreaver on Thu, 18 Apr 2019 10:18:33 -0700