First lookCode:
File Name: Main. c
1 # Include <stdio. h>
2 # Include <stdlib. h>
3 /* **************************************** **************************
4
5 Familiar with the structure of a list:
6 1. Concepts of head pointer, head node, Start node, and End Node
7 2. Create a linked listAlgorithm
8 3. Algorithms for traversing a linked list
9
10 **************************************** ************************** */
11 Struct Node
12 {
13 Int Value;
14 Struct Node * pnext;
15 };
16
17
18 Struct Node * initlist ( Void );
19 Void Printlistelements ( Struct Node * pheader );
20
21 Int Main ( Void )
22 {
23
24 Struct Node * pH = initlist ();
25 Printlistelements (ph );
26 Return 0 ;
27 }
28
29 /* **************************************** *********
30
31 Dynamically create a linked list based on the number and value of input linked list elements
32
33 **************************************** ********* */
34 Struct Node * initlist ( Void )
35 {
36 Struct Node * pheader; // Header pointer, pointing to the header Node
37 Struct Node * ptail; // Similar to a cursor, each move points to the last element.
38 Struct Node * pnode;
39 Int I, num;
40 Pheader = ( Struct Node *) malloc ( Sizeof ( Struct Node )); // Create a header Node
41 Ptail = ( Struct Node *) malloc ( Sizeof (Struct Node )); // Create a first element
42 Pheader-> pnext = ptail;
43
44 Printf ( " Enter the number of linked list elements: " );
45 Scanf ( " % D " , & Num );
46 For (I = 1 ; I <num + 1 ; I ++)
47 {
48 Printf ( " Enter the element value % d: " , I );
49
50 Scanf ( " % D " , & Ptail-> value );
51 Pnode = ( Struct Node *) malloc ( Sizeof ( Struct Node ));
52
53 Ptail-> pnext = pnode;
54 Ptail = ptail-> pnext;
55
56
57 }
58
59 Ptail-> pnext = NULL; // * ***** Note that the pointer field of the last element is assigned null separately. Otherwise, the linked list has no tail node.
60
61 Return Pheader;
62
63
64 }
65 /* **************************************** *******************************
66
67 Traverse all element values and print the output
68
69 **************************************** ******************************* */
70 Void Printlistelements (Struct Node * pheader)
71 {
72 Struct Node * node;
73 Int I = 1 ;
74 Node = pheader-> pnext; // Node points to the first element.
75 While (Null! = Node-> pnext)
76 {
77 Printf ( " Element value % d: % d \ n " , I, node-> value );
78 I ++;
79 Node = node-> pnext; // Similar to a cursor, a node moves backward and points to the next element.
80
81 }
82 }
Summary:
1. Understand the structure of a one-way linked list, as shown in:
1) header node:It plays a starting role in the process of creating a linked list, but it should not be stored in its data domain.ProgramSo as not to be powerless when deleting the first data, because the node pointing to the first node cannot be found, which is not conducive to the circular scrolling operation on all data. It does not contain valid data, but the pointer field points to the first node.
2) the empty linked list indicates that the pointer field of the header node is null.
3) when creating a linked list, be sure to assign null to the pointer field of the last node. Otherwise, there is no end.
4) when you operate a linked list with a header node, you must process the first node to perform loop operations accurately.
the above is a simple note made when you review C's basic knowledge. I hope you will not laugh at it, haha