I have in the previous several blog "C language to achieve the insertion of linked list" "C language to achieve the deletion of linked list node" C implementation of the head interpolation method and the tail interpolation method to construct the chain list "C language to realize the basic operation of the list" to achieve a single linked list of many additions and deletions to check operations. Here we want to implement a single linked list of reverse order printing, using C to achieve. Code uploaded to Https://github.com/chenyufeng1991/ReverseLinkedList.
The basic algorithms are:
(1) using the tail interpolation method to construct the original linked list;
(2) sequentially traversing the original linked list;
(3) Remove the nodes in the traversal using the head interpolation method to establish a new linked list;
(4) Printing the new list after the reverse order;
The principle is that the first insertion of each node is the first one of the list, the first inserted will become the last one, the last one inserted into the first node. So it will cause reverse order.
The core code is as follows:
The linked table Node *preverselist is declared after reverse order; The head-interpolation method establishes the chain table void Headinsert (node *pinsert) {if (preverselist = NULL) {//Is the first node preverselist = PI
nsert;
}else{//Below is the header insert statement pinsert->next = preverselist;
Preverselist = Pinsert; }//traverse the list and use the header to construct a new list void Scanlist (Node *pnode) {//First determine if the original list is empty; if (Pnode = NULL) {printf ("%s function executes, the original list is
NULL, cannot reverse output \ n ", __function__); }else{Node *pmove; This node is used to move node *pinsert in the original linked list;
The node is the new insert node Pinsert = (node *) malloc (sizeof (node));
memset (pinsert, 0, sizeof (Node));
Pinsert->next = NULL;
Pmove = Pnode;
while (Pmove!= NULL) {//traversal to each node, the invocation header function inserts a new list pinsert->element = pmove->element;
Headinsert (Pinsert);
Allocates space for the next node that is inserted Pinsert = (node *) malloc (sizeof (node));
memset (pinsert, 0, sizeof (Node));
Pinsert->next = NULL; Pmove = pmove-≫next;
printf ("%s function execution, reverse list complete \ n", __function__);
}
}