Single list
-------------------------------------------------- ------------------------------
/ * Preprocessing command and structural definition starting with the program: * /
#include
Typedef struct node {int data; structure;
/ * Establish a single-stranded function: * // * Description: This program has a head node, and the data in the array is stored from the subscript 1 * /
Node * Createlist (int A [], int N) {node * h, * p, * q; INT i; if (n <= 0) return (null); Q = (Node *) Malloc (Sizeof (Node) ); H = q; for (i = 1; i <= n; i ) {p = (node *) malloc (sizeof (node)); p-> data = a [i]; q-> next = P ; Q = P;} Q-> next = null; return (h);}
/ * Insert a new element before the i element: * /
Void Insert1 (Node * P, INT I, INT Key) {INT J = 0; Node * T; While (p! = null && j
/ * Insert a new element before the value of the value of Key (assuming the elements in the linear table): * /
Void Insert2 (Node * h, int key, int key1) {INT j = 0; Node * P = H, * Temp; while (p-> next! = null && p-> next-> data! = key) { P = P-> Next;} if (p-> next-> data == key) {temp = (node *) malloc (size); if (temp == null) error ("There is not enough storage Space "); else {temp-> data = key1; temp-> next = p-> next; p-> next = TEMP;}}}
/ * Insert a new element in the linear table arranged in ascending, remain inserted after insertion: * /
Void insert_node (node * h, int key) {node * q, * p, * s; s = (node *) malloc (sizeof (node)); S-> Data = key; q = h; p = h- > Next; While (p! = null && s-> data> p-> data) {q = p; p = p-> next;} Q-> next = S; S-> Next = P;} / * Delete linear table i element: * /
Void delete1 (Node * h, INT i) {INT j = 0; Node * p, * q; p = h; while (p-> next! = null && j
/ * Delete elements in the single-stranded table value of Key (assuming that there is no other element in the linear table): * /
Void delete2 (Node * h, int key) {node * q, * p = h; while (p! = null && p-> data! = key) {q = p; p = p-> next;} P-> DATA == Key) {q-> next = p-> next; free (p);}}
/ * Display function of all elements of the output single-link table: * /
Void PrintList (node * h) {node * p = h-> next; while (p! = null) {printf ("% d", p-> data); p = p-> next;} printf ("/ N / N ");
/ * The program can be written or the above function can be written when it is exercised. E.g:*/
Void main () {int a [] = {0, 10, 20, 30, 40, 50}, n = 5; / * Data From the subscript 1 Start Start * / Node * H; h = Createlist (A, N );
/ * Display each element of the output linear table, verify that the function of establishing the one-way table is correct * /
Printlist (h);
/ * Delete the 4th element, then output the verification * /
Delete1 (h, 4); Printf ("Delete the 4th Element:"); PrintList (h);
/ * Elements that delete 30, then output verification * /
Delete2 (H, 30); PRINTF ("Delete value of 30):"); PrintList (h);
/ * Insert an element 15 before the second element, then output verification * /
INSERT1 (H, 2, 15); Printf ("Insert element 15:") before the second element: "); PrintList (h);
/ * Insert an element 45 before the element is 50, then output verification * /
Insert2 (H, 50, 45); Printf ("Insert element 45:") before the element is 50: "); PrintList (h);
/ * Insert an element 22 in a linear table, keep an order, then output verification * /
INSERT_NODE (H, 22); Printf ("Insert an element 22, to remain in order:"); PrintList (h);}
-------------------------------------------------- ------------------------------