Data structure C Language & amp; gt; 3 basic linked list & amp; gt; 3-8 reverse structure of linked list

Source: Internet
Author: User
Tags key string

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 */
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.