链表的就地逆置

来源:互联网 发布:门窗软件 编辑:程序博客网 时间:2024/05/01 12:30
#include <iostream>using namespace std; /*结构体*/typedef struct LNode  {int data;struct LNode *next;}LNode,*linklist;void reverse(linklist &L){//逆向输出函数  linklist p,q;  p=L->next;  L->next=NULL;  while (p!=NULL)  {  q=p->next;  p->next=L;  L=p;  p=q;  }  p=L;  while (p!=NULL)  {  cout<<p->data;  p=p->next;}}int main(){   linklist L,p,q;   p=L=new LNode;   for (int i=1;i<=5;i++)   {   cin>>p->data;   q=new LNode;   if (i==5)   {   p->next=NULL;   }else{   p->next=q;   p=q;      }     }  reverse(L);    return 0;}