C language-single-chain table (1), single-chain table in C Language
Define nodes:
typedef struct Node {int data;Node* pNext;}NODE, *PNODE;
For details, PNode represents the struct Node *. The above form can also be written in the following form with the same meaning.
typedef struct Node {int data;Node* pNext;}*PNODE,NODE ;
Algorithm operations
1. Create a linked list
// 1. initialize the linked list PNODE create_list (void) {int len, val; printf ("% s", "Enter the length of the linked list \ n "); scanf ("% d", & len); PNODE pHead = (PNODE) malloc (sizeof (PNODE); // create a header node PNODE pTail = pHead; // always point to the last pTail-> pNext = NULL; for (int I = 0; I <len; I ++) {printf ("Enter the value of variable % d", I); scanf ("% d", & val); PNODE p = (PNODE) malloc (sizeof (PNODE); if (NULL = p) {printf ("memory allocation failed");} p-> data = val; p-> pNext = NULL; pTail-> pNext = p; // The End Node points to Ptail = p; // pTail is the end node} return pHead ;}
2. Display linked list data
// 2. Output void show_list (PNODE pHead) {PNODE p = pHead-> pNext; // The first Node Address while (p! = NULL) {printf ("% d \ n", p-> data); p = p-> pNext;} printf ("\ n"); return ;}
3. Run the test
Int main () {PNODE pHead = NULL; // represents Struct Node * pHead = NULL; pHead = create_list (); // show_list (pHead); return 0 ;}