#include <iostream>
using namespace std;
struct Lnode{
int data;
struct Lnode *next;
};
int main()
{
int n,i;
Lnode *head,*p,*q,*r;
head=new Lnode;//建立头节点
head->next=NULL;//头节点的next域设成NULL
r=head;
cin>>n;//输入要输入的值的个数
for(i=0;i<n;i++)//尾插法建立链表并输入值
{
p=new Lnode;
cin>>p->data;
r->next=p;
p->next=NULL;
r=p;
}
q=head->next;
while(q!=NULL)//输出链表中的值
{
cout<<q->data<<" ";
q=q->next;
}
cout<<endl;
//链表反向操作
head=head->next;
r=NULL,q=NULL,p=head;
while(p!=NULL)
{
q=p->next;
p->next=r;
r=p;
p=q;
}
while(r!=NULL)//反向输出链表中的值
{
cout<<r->data<<" ";
r=r->next;
}
}
单链表逆置操作(交换节点)
于 2022-03-16 23:15:07 首次发布