Struct node {int data; node * Next;}; // create a linked list and enter a number as the input. If the input is 0, the linked list ends (0 is excluded) node * creat () {node * head, * P, * s; int X, cycle = 1; head = (node *) malloc (sizeof (node); P = head; while (cycle) {cout <"Please input the data:"; CIN> X; If (X! = 0) {S = (node *) malloc (sizeof (node); s-> DATA = x; P-> next = s; P = s ;} else {cycle = 0 ;}} head = head-> next; P-> next = NULL; return head ;}
// Calculate the length of the linked list int computlength (node * head) {node * P; int n = 0; If (Head = NULL) {return 0;} p = head; while (P! = NULL) {n ++; P = p-> next;} return N ;}
// Print the data void printlist (node * head) {node * P; If (Head = NULL) {cout <"the list has no value! "<Endl; return;} p = head; while (P! = NULL) {cout <p-> data <Endl; P = p-> next ;}}
// The node whose data is num may be the first node or another node * delenode (node * head, int num) {node * P1, * P2; P1 = head; while (P1-> data! = Num & P1-> next! = NULL) {P2 = p1; P1 = p1-> next;} If (P1-> DATA = num) {If (p1 = head) // Delete the header node {head = p1-> next; Delete P1;} else // delete other nodes {P2-> next = p1-> next ;}} else cout <"not find the num! "<Endl; return head ;}
// Delete the linked table void delelist (node * head) {node * P; P = head; while (P! = NULL) {node * temp = P; P = p-> next; Delete temp;} cout <"delete list success" <Endl ;}
// Insert nodes into the linked list. In the three cases, the End Node * insertnode (node * head, int num) {node * Pre, * inser, * NEX; inser = (node *) malloc (sizeof (node); inser-> DATA = num; NEX = head; while (NEX-> data <inser-> Data & NEX-> next! = NULL) {pre = NEX; NEX = NEX-> next;} If (inser-> data <= NEX-> data) // determine whether data is inserted before the last node (including the last node) {If (NEX = head) // before the insert header {head = inser; inser-> next = NEX;} else // Insert the center {pre-> next = inser; inser-> next = NEX ;}} else // later than the last element inserted at the end {NEX-> next = inser; inser-> next = NULL;} return head ;}