1.Delete a node from a single-chain table
- If the header node is deleted, the head pointer is directed to the next node of the header node. Free P1 at the same time, as shown in:
-
- If you delete an intermediate node, use the next of P2 to point to the next OF P1 and free P1, as shown in:
-
- If the last node is deleted, if the node is P1 and the previous node is P2, the P1 is released and the next node of P2 is set to null.
- CodeImplementation
// Delete node * remove (node * head, int num) {node * P1, * P2; P1 = head; while (num! = P1-> Data & P1-> next! = NULL) // find the node whose data is num {P2 = p1; P1 = p1-> next;} If (num = p1-> data) // If the num node exists, delete {If (p1 = head) {head = p1-> next; free (P1 );} else {P2-> next = p1-> next;} else {printf ("\ n % d cocould not been found", num);} return (head );}
2.Insert a single-chain table
- If it is inserted before the header node, next of P0 points to P1, And the header node points to P0, as shown in:
-
- If an intermediate node is inserted, as shown in:
-
- If the last node is inserted, point next of P1 to P0, and then point p0 to null, as shown in:
- Code Implementation
// Insert node * insert (node * head, int num) into a single-chain table {node * P0, * P1, * P2; P1 = head; p0 = (node *) malloc (sizeof (node); P0-> DATA = num; while (P0-> DATA> P1-> Data & P1-> next! = NULL) {P2 = p1; P1 = p1-> next;} If (P0-> data <= p1-> data) {If (Head = p1) {P0-> next = p1; head = P0;} else {P2-> next = P0; P0-> next = p1 ;}} else {p1-> next = P0; p0-> next = NULL;} return (head );}
3.Sorting of Single-Chain tables
- Code Implementation
// Node * sort (node * head) {node * P, * P2, * P3; int N; int temp; n = length (head ); if (Head = NULL | head-> next = NULL) // if there is only one or no node, return head; P = head; For (Int J = 1; j <n; ++ J) {P = head; For (INT I = 0; I <n-J; ++ I) {If (p-> DATA> P-> next-> data) {temp = p-> data; P-> DATA = p-> next-> data; p-> next-> DATA = temp;} p = p-> next ;}} return (head );}
4.Reverse placement of a single-chain table
- Code Implementation
// Node * reverse (node * head) {node * P1, * P2, * P3; If (Head = NULL | head-> next = NULL) return head; P1 = head; P2 = p1-> next; while (P2) {P3 = P2-> next; P2-> next = p1; P1 = P2; p2 = P3;} head-> next = NULL; head = p1; return head ;}
From: http://www.cnblogs.com/iuices/archive/2011/11/06/2238606.html