In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the C language how to achieve the reverse order of the linked list and output related knowledge, the content is detailed and easy to understand, the operation is simple and fast, and has a certain reference value. I believe you will gain something after reading this C language how to reverse the linked list and output the article. Let's take a look.
C language data structure to reverse the order of linked list and output
Reverse a linked list and output it. I implemented it in two ways, the first is with the help of a new empty linked list, and the second is to reverse the order directly based on the original linked list.
Example code:
Header file:
# include # include # include typedef int ElemType; typedef struct Node {/ / Node structure ElemType value; / / range struct Node * next;// pointer Domain} Node,*ptr_Node; typedef struct LinkList {/ / linked list structure ptr_Node head; / / Link list header node pointer ptr_Node tail;// chain list tail node pointer int length; / / list length} LinkList,*ptr_LinkList Ptr_LinkList CreateList (void) {/ / create an empty linked list ptr_LinkList linklist; linklist= (LinkList *) malloc (sizeof (LinkList)); if (! linklist) {printf ("allocation failed.\ n");} linklist- > head=NULL; linklist- > tail=NULL; linklist- > length=0; return linklist;} bool IsListEmpty (ptr_LinkList linklist) {/ / determine whether the linked list is empty if (linklist- > length==0) {return true;} return false } void InsertListHead (ptr_LinkList linklist,ElemType element) {/ / insert the node with the value element as the new header ptr_Node ptr_node; ptr_node= (Node *) malloc (sizeof (Node)); / / generate the insertion node if (! ptr_node) {printf ("allocation failed.\ n");} else {ptr_node- > value=element; if (linklist- > length==0) {linklist- > head=ptr_node; linklist- > tail=linklist- > head Linklist- > tail- > next=NULL;} else {ptr_node- > next=linklist- > head; linklist- > head=ptr_node; / / chain header} linklist- > length++; / / linked list length plus 1}} void InsertListTail (ptr_LinkList linklist,ElemType element) {ptr_Node ptr_node; ptr_node= (Node *) malloc (sizeof (Node)); / / generate insertion node if (! ptr_node) {printf ("allocation failed.\ n") } else {ptr_node- > value=element; if (linklist- > length==0) {linklist- > head=ptr_node; linklist- > tail=linklist- > head; linklist- > tail- > next=NULL;} else {linklist- > tail- > next=ptr_node; linklist- > tail=ptr_node; / / chain tail} linklist- > length++; / / list length plus 1}} void InsertListPosition (ptr_LinkList linklist,int pos,ElemType element) {int I; ptr_Node ptr_node Ptr_Node temp_ptr_node; if (poslinklist- > length) {printf ("The insert position is invalidate.\ n");} else {ptr_node= (Node *) malloc (sizeof (Node)); / / generate insertion node if (! ptr_node) {printf ("allocation failed.\ n");} ptr_node- > value=element; if (pos==1) {InsertListHead (linklist,element) } else if (pos==linklist- > length) {InsertListTail (linklist,element);} else {temp_ptr_node=linklist- > head; for;} ptr_node- > next=temp_ptr_node- > next; temp_ptr_node- > next=ptr_node; linklist- > length++;} void Destroy (ptr_LinkList linklist) {/ / destroy the linked list ptr_Node paired linklist-> head; ptr_Node Q While (p) {/ / release each node space qroomp-> next; free (p); pendant null; psigq;}} void Traverse (ptr_LinkList linklist) {/ / output the entire linked list ptr_Node p; pairlinklist-> head; while (p) {printf ("% 4d", p-> value); pairp> next;}}
Several basic operations of the linked list are implemented in the header file, some are necessary and some are non-essential.
Implementation code:
# include "stdafx.h" # include "LinkList.h" # include ptr_LinkList InvertList (ptr_LinkList list) {/ / this method uses a new empty linked list to reverse the order of the linked list ptr_LinkList inverted_linklist; ptr_Node p; paired list-> head; inverted_linklist=CreateList () / / create an empty linked list while (p) {/ / input the node values in the list linked list in reverse order into the newly created linked list to reverse the linked list InsertListHead (inverted_linklist,p- > value); pairp-> next;} return inverted_linklist;} void InvertLinkList (ptr_LinkList linklist) {/ / this method directly reverses the order of the original linked list without the help of other linked lists ptr_Node pmag Q r m; mendpendlinklist-> head; qure p-> next Rroomq- > next; while (r) {/ / reverses the nodes in the linked list Q-> next=p; pairq; qroomr; rreverr-> next;} Q-> next=p; / / the last node reverses linklist- > head=q; linklist- > tail=m; linklist- > tail- > next=NULL;} int _ tmain (int argc, _ TCHAR* argv []) {ptr_LinkList linklist; ptr_LinkList list; linklist=CreateList () If (linklist) {printf ("We have created a new linklist.\ n");} InsertListHead (linklist,12); InsertListHead (linklist,35); InsertListHead (linklist,66); InsertListHead (linklist,06); InsertListHead (linklist,11); InsertListHead (linklist,54); InsertListHead (linklist,79); Traverse (linklist); printf ("\ n"); printf ("The first method:\ n"); list=InvertList (linklist); Traverse (list); printf ("\ n") Printf ("The second method:\ n"); InvertLinkList (linklist); Traverse (linklist); printf ("\ n"); getch (); return 0;} this article on "C language how to reverse and output linked lists" ends here, thank you for reading! I believe you all have a certain understanding of the knowledge of "how to reverse and output the linked list in C language". If you want to learn more, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.