This article original, reproduced Please note: http://blog.csdn.net/j903829182/article/details/38173681
# Include <stdio. h> # include <malloc. h> // chain storage and implementation of linear tables, leading point # define true 1 # define false 0 typedef int datatype; // define the abstract data type // node's struct typedef struct node {datatype data; // node's data field struct node * Next; // node's pointer field} slnode; // initialization // the pointer is used for the parameter here to direct the header pointer in the main function to the space node opened by this function // if no pointer is used, point to the header pointer, then the C language functions are all passed values. Here the parameter is saved // a copy of the header pointer in the main function, after the parameter is assigned a value, you cannot change the value pointed to by the header pointer. // the address of the header pointer is saved by the pointer, it is worth changing in the first pointer // so we need to use the pointer here to change the void l of the real parameter value. Istinitiate (slnode ** head) {* head = (slnode *) malloc (sizeof (slnode); // request a header node so that the header Pointer Points to the header node (* head) -> next = NULL; // set the end flag to null} // calculate the number of current elements int listlength (slnode * head) {int num = 0; // The initial variable num is used to count slnode * P = head; // P points to the header node while (p-> next! = NULL) {// cyclic count P = p-> next; // P points to the next node num ++ of P; // accumulate} return num; // return the Count value} // insert node. Here, we set the head node to 0. when inserting data elements, we should start from 1 to int listinsert (slnode * head, int I, datatype data) {Int J = 0; // The counting variable is initialized to 0 slnode * P = head, * q; // P points to the header node // to insert data at the I position, I should find the node where the I-1 is located. To facilitate counting, I set the node from 1 to count the while (p-> next! = NULL & J <i-1) {// finally point P to the I-1 node P = p-> next; // P points to the next node J ++; // variable accumulate count} If (J! = I-1) {printf ("Insertion Location Error !!! \ N "); // return false; // return operation result} q = (slnode *) malloc (sizeof (slnode )); // generate a new node Q-> DATA = data; // send the data to the new node Q-> next = p-> next; // The next node that Q points to is the next node that P points to. P-> next = Q; // The next node of P changes to qreturn true; // return operation result} // Delete the specified node int listdelete (slnode * head, int I, datatype * Data) {slnode * P = head, * s; // point to the header node Int J = 0; // The variable is used to count while (p-> next! = NULL & J <i-1) {// locate the I-1 node P = p-> next; // P points to the next node J ++ of P; // auto-increment} If (J! = I-1) {printf ("location deletion Error !!! \ N "); // return false; // return operation result} s = p-> next; // find * Data = s-> data; // assign the data of the drug deletion node to datap-> next = s-> next; // The next node of P is I + 1 free (s ); // release the S node, that is, return true for the I node; // return result} // get the data element int listget (slnode * head, int I, datatype * Data) {slnode * P = head; // point to the header node Int J = 0; // The Count variable is initialized to 0 while (p-> next! = NULL & J <I) {// find the I-th node P = p-> next; // P points to the next element J ++; // variable auto-increment} If (J! = I) {// determine whether J is equal to I and whether printf ("An error occurred while retrieving the element !!! \ N "); // output error message: Return false; // return operation result} * Data = p-> data; // value the data to the Data Pointer return true; // return operation result} // output linked list void displaylist (slnode * head) {// print all data of the output linked list slnode * P = head; // point to the header node while (p-> next! = NULL) {// determine whether the next node is null P = p-> next; // P points to the next node printf ("-> % d", p-> data ); // output information} printf ("\ n"); // wrap} // undo a single-chain table // the pointer used by the parameter is used to change the value of the header pointer. If this parameter is not used, you cannot change void destroy (slnode ** head) {// The Node slnode * P = * head, * q; while (p-> next! = NULL) {// determines whether it is null q = P; // points Q to the node P pointing to p = p-> next; // P points to free (q) under a node; // releases the node pointed to by Q} * head = NULL; // point the header pointer to null} // The main function int main () {slnode * head; // defines the pointer variable int I, X; //// define the int variable listinitiate (& head); // initialize the header pointer variable, passing the header pointer address, in order to get the value of printf ("sizeof = % d Head = % d \ n", sizeof (slnode), head) for the function to change the header pointer variable ); // test the memory size occupied by the struct, and the value of the header pointer for (I = 1; I <= 10; I ++) {// cyclically listinsert (Head, i, I); // insert to the linked list} displaylist (head); // all data in the output chain table for (I = 1; I <= listlength (head ); I ++) {// cyclically listget (Head, I, & X); // obtain the values in the linked list and put them in the X variable printf ("% d ", x); // output x value} printf ("\ n"); // line feed listdelete (Head, 5, & X ); // Delete the fifth element printf ("x = % d \ n", x); // output the value of the deleted element displaylist (head ); // return 0 for all data in the chain table again ;}