//列表逆序输出
#include<iostream>
using namespace std;
typedef struct Node
{
int date;
Node *next;
}Node,*PNode;
PNode invert(PNode head)//逆序输出
{
PNode m,p,q,r;
if(head->next==NULL)
{
cout<<"链表为空或不存在,无法输出!"<<endl;
exit(1);
}
p=head;
m=head->next;
q=p->next;
r=q->next;
while(r!=NULL)
{
q->next=p;
p=q;
q=r;
r=r->next;
}
q->next=p;
head->next=q;
m->next=NULL;
return head;
}
PNode create()
{
PNode pHead;
pHead = new Node;
if(pHead==NULL)
{
cout<<"链表创建失败!"<<endl;
exit(-1);
}
PNode pTail = pHead;
pTail->next=NULL;
int iTemp;
cout<<"输入是个整数:";
for(int i=0;i<10;i++)
{
cin>>iTemp;
PNode pTemp = new Node;
pTemp->date = iTemp;
pTail->next=pTemp;
pTemp->next=NULL;
pTail=pTemp;
}
return pHead;
}
void display(PNode head)
{
PNode p=head->next;
while(p!=NULL)
{
cout<<p->date<<' ';
p=p->next;
}
cout<<endl;
}
int main()
{
PNode pHead=create();
display(pHead);
pHead=invert(pHead);
display(pHead);
return 0;
}
#include<iostream>
using namespace std;
typedef struct Node
{
int date;
Node *next;
}Node,*PNode;
PNode invert(PNode head)//逆序输出
{
PNode m,p,q,r;
if(head->next==NULL)
{
cout<<"链表为空或不存在,无法输出!"<<endl;
exit(1);
}
p=head;
m=head->next;
q=p->next;
r=q->next;
while(r!=NULL)
{
q->next=p;
p=q;
q=r;
r=r->next;
}
q->next=p;
head->next=q;
m->next=NULL;
return head;
}
PNode create()
{
PNode pHead;
pHead = new Node;
if(pHead==NULL)
{
cout<<"链表创建失败!"<<endl;
exit(-1);
}
PNode pTail = pHead;
pTail->next=NULL;
int iTemp;
cout<<"输入是个整数:";
for(int i=0;i<10;i++)
{
cin>>iTemp;
PNode pTemp = new Node;
pTemp->date = iTemp;
pTail->next=pTemp;
pTemp->next=NULL;
pTail=pTemp;
}
return pHead;
}
void display(PNode head)
{
PNode p=head->next;
while(p!=NULL)
{
cout<<p->date<<' ';
p=p->next;
}
cout<<endl;
}
int main()
{
PNode pHead=create();
display(pHead);
pHead=invert(pHead);
display(pHead);
return 0;
}