The reverse structure of the linked list basically repeats the contents of the basic linked list.
For example, when a basic linked list is created, the linked list is traversed, and then the linked list is reversed.
The linked list structure is as follows:
A-> B-> c-> d-> e-> NULL
Get the three pointers head, mid, last, and set the initial value:
Head points to
Mid points to NULL
Then start the pointer movement:
While (head! = NULL)
{
Last = mid;
Mid = head;
Head = head-> next;
Mid-> next = last;
}
// Last goes to mid, mid goes to head, head goes to head-> next, and they keep moving forward, while walking mid-> next = last until head = NULL.
The complete code is as follows:
/* ===================================================== = */
/* Program instance: 3_8.c */
/* Reverse link of the link string column */
/* ===================================================== = */
# Include <stdlib. h>
Struct llist/* declaration of string Column Structure */
{
Int num;/* mailing Number */
Struct llist * next;/* points to the next tag */
};
Typedef struct llist node;/* define new state */
Typedef node * llink;/* define new state indicator */
/*----------------------------------------*/
/* Print the key string column */
/*----------------------------------------*/
Void printllist (llink ptr)
{
While (ptr! = NULL)/* string-column access loop */
{
Printf ("[% d]", ptr-> num);/* print node information */
Ptr = ptr-> next;/* point to next node */
}
Printf ("");/* line feed */
}
/*----------------------------------------*/
/* Create a chain string column */
/*----------------------------------------*/
Llink createllist (int * array, int len)
{
Llink head;/* Start indicator of the string column */
Llink ptr, ptr1;
Int I;
/* Create the first node */
Head = (llink) malloc (sizeof (node);/* configure memory */
If (! Head)/* Check metrics */
Return NULL;
Head-> num = array [0];/* create node content */
Head-> next = NULL;/* set the initial value of the indicator */
Ptr = head;/* point ptr to the start of the string column */
For (I = 1; I <len; I ++)/* create other node circuits */
{
Ptr1 = (llink) malloc (sizeof (node ));
If (! Ptr1)
Return NULL;
Ptr1-> num = array [I];/* create node content */
Ptr1-> next = NULL;/* set the initial value of the indicator */
Ptr-> next = ptr1;/* link node */
Ptr = ptr-> next;/* point to next node */
}
Return head;
}
/*----------------------------------------*/
/* Reverse link of the link string column */
/*----------------------------------------*/
Llink invertllist (llink head)
{
Llink mid, last;
Mid = NULL;/* mid is the front node of the head */
While (head! = NULL)
{
Last = mid;/* last is the front node of the mid */
Mid = head;
Head = head-> next;/* next node */
Mid-> next = last;/* mid indicates last of the forward node */
}
Return mid;
}
/*----------------------------------------*/
/* Release the memory of the link string column */
/*----------------------------------------*/
Void freellist (llink head)
{
Llink ptr;
While (head! = NULL)/* visit the string-column loop */
{
Ptr = head;
Head = head-> next;/* point to next node */
Free (ptr);/* release back node memory */
}
}
/*----------------------------------------*/
/* Main program: reverse the string column */
/*----------------------------------------*/
Void main ()
{
Int llist1 [6] = {1, 2, 3, 4, 5, 6};/* array content */
Llink head;/* points to the start of the string column */
Head = createllist (llist1, 6);/* Create a string column */
If (! Head)
{
Printf ("memory configuration failed! ");
Exit (1 );
}
Printf ("original linked list :");
Printllist (head);/* print the original string column */
Head = invertllist (head);/* reverse the string column */
Printf ("reverse linked list :");
Printllist (head);/* print the inverted string column */
Freellist (head);/* returns the string column memory */
}