Use C language to describe static and dynamic linked lists
Static and Dynamic Linked lists are two different forms of linear table chain storage structure. The initial length of a static linked list is usually fixed. You do not need to move any element during insertion or deletion, but only need to modify the pointer. Therefore, it still has the main advantages of the chain storage structure. A Dynamic Linked List is relative to a static linked list. Generally, a dynamic linked list is described by default if the chain storage structure of a linear table is described. The following provides their simple implementation. For more detailed implementation of linear tables in C language, refer http://www.cnblogs.com/choon/p/3876606.html Static linked list # define _ CRT_SECURE_NO_DEPRECATE # define _ CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES 1 # include <stdio. h> # include <stdlib. h>/* all nodes are defined in the program. They are not opened temporarily and cannot be released after use. Such a linked list is called a "static linked list ". */Struct Student {int num; float score; struct Student * next;}; int main () {struct Student stu1, stu2, stu3, * head, * p; stu1.num = 1001; stu1.score = 80; // value stu2.num and score members of node stu1 to stu2.num = 1002; stu2.score = 85; // assign stu3.num = 1003 to the num and score members of node stu2; stu3.score = 90; // assign head = & stu1 to the num and score members of node stu3; // The header Pointer Points to the 1st node stu1 stu1.next = & stu2; // assign the address of the node stu2 to stu1 node's next member stu2.next = & stu3; // The address is assigned to stu2 node's next member stu3.next = NULL; // stu3 is the last node. Its next member does not store the address of any node and is set to NULL p = head; // make the p pointer also point to 1st nodes // traverse the static linked list do {printf ("% d, % f \ n", p-> num, p-> score ); // output the data p points to the node p = p-> next; // then let p point to the next node} while (p! = NULL); // until the next member of p is NULL, system ("pause") is traversed ");} dynamic Linked List # define _ CRT_SECURE_NO_DEPRECATE # define _ CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES 1 # include <stdio. h> # include <stdlib. h>/* the so-called Dynamic Linked List refers to creating a linked list from scratch during program execution, that is, opening up nodes one by one and inputting data of each node, and establish the relationship between the two sides. */Struct Student {int No; // Student ID struct Student * next;}; int main () {struct Student * p1, * p2, * head; int n = 0; // number of nodes head = NULL; p1 = (struct Student *) malloc (sizeof (struct Student); printf ("Enter 1 Student ID \ n "); scanf ("% d", & p1-> No); p2 = p1; // At the beginning, both p1 and p2 point to the 1st nodes while (p1-> No! = 0) {n ++; if (n = 1) {head = p1;} else {p2-> next = p1;} p2 = p1; // p2 is the last node printf ("Enter the Student ID and enter 0 to terminate: \ n"); p1 = (struct Student *) malloc (sizeof (struct Student )); scanf ("% d", & p1-> No) ;}; p2-> next = NULL; // After the input is complete, p2-> next is NULL // traverse the Dynamic Linked List struct Student * p; p = head; while (p! = NULL) {printf ("% d,", p-> No); p = p-> next;} printf ("\ n "); system ("pause ");}