Well, this is the first blog I wrote Ha, do not write bad people do not take offense, mainly want to share some of their ideas to everyone. You are also welcome to point out mistakes and progress together.
Speak not much, speak first. To reverse a single-linked list, it can be broadly divided into the following steps. Create a linked list first. Then consider the inverse of the linked list implementation. Finally, the output of the linked list. With such a few steps, we have to come up with a step-by-step implementation. Well, creating a linked list is not going to be said, everyone will. Then it is the inverse of the list, here I use the in-situ inversion method,, well, anyway, I was so called, we can refer to. Of course, you have to consider the function of the formal parameters and return values and the transfer of pointers, if there is a problem, the compiler will not error, so we must pay more attention. The rest of the small problem is to see the code.
Well, the previous sketch was gone, and now there is no way to draw a sketch to show you, I'm sorry. If you can't read the list, you can draw a sketch and see for yourself.
1#include <stdio.h>2#include <stdlib.h>3 4 structStudent5 {6 intdata;7 structStudent *Next;8 };9 Ten intICount; Define global variables Save code length One A structStudent *Create () - { - structStudent *phead =NULL; the structStudent *pnew,*pEnd; -ICount =0; -PEnd = Pnew = (structstudent*)malloc(sizeof(structstudent)); -printf"Please enter data:"); +scanf"%d",&pnew->data); - while(pnew->data!=0) + { Aicount++; at if(ICount = =1)//From the start of the IF statement will pay more attention to the hands of the handover oh, more easily wrong - { -Pnew->next =NULL; -PEnd =pnew; -Phead =pnew; - } in Else - { toPnew->next =NULL; +Pend->next =pnew; -PEnd =pnew; the } *Pnew = (structstudent*)malloc(sizeof(structstudent)); $printf"Please enter data:");Panax Notoginsengscanf"%d",&pnew->data); - } the Free(pnew); + returnPhead; A } the + structStudent *reverse (structStudent *phead)//list inverse function - { $ structStudent *p,*q,*T; P is the forward pointer, q is the back pointer, T is the Exchange pointer $Q =Phead; -p = (q->next); -Q->next =NULL; the while(t!=NULL) - {Wuyit = p->Next; theP->next =Q; -Q =p; Wu if(t!=null) p =T; - Else; About } $ return(p); - } - - voidShowlist (structStudent *phead)//pointer output function A { + structStudent *temp; thetemp =Phead; - $ while(temp) the { theprintf"%d",temp->data); thetemp = temp->Next; the } -printf"\ n"); in } the the intMain () About { the structStudent *First ; the theFirst =Create (); +printf"the data before the linked list is reversed:"); - showlist (first); the BayiFirst =reverse (first); the theprintf"linked list inverse data:"); - showlist (first); - the return 0; the}
C Language Single-link list reverse code implementation (easy to understand version)