#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
node *next;
}*head,*p,*q,*r;
int main()
{
int n,i=0;
while(~scanf("%d",&n))
{
struct node *tail;
if(n==-1)
break;
if(i==0)
{
head=(struct node*)malloc(sizeof(struct node));
head->data=n;
head->next=NULL;
tail=head;
}
else
{
p=(struct node*)malloc(sizeof(struct node));
p->data=n;
p->next=NULL;
tail->next=p;
tail=p;
}
i++;
} //以上为顺序建链表,head内就开始有值
p=head->next; //记录head的下一个节点
q=head; //记录head节点
head->next=NULL; //让头指针变为尾指针,指向空
while(p)
{
r=p->next; //记录下一个节点
p->next=q; //指向上一个节点
q=p;
head=p; //移动头指针
p=r;
}
p=head;
while(p) //按顺序输出
{
printf("%d",p->data);
p=p->next;
if(p)
printf(" ");
}
printf("\n");
}
数据结构实验之链表三:链表的逆置
最新推荐文章于 2020-03-18 23:55:32 发布
本文介绍了一种链表逆置的算法实现方法,通过逐步建立单链表并完成链表数据的逆序输出,详细展示了如何在不使用数组的情况下完成这一任务。

500

被折叠的 条评论
为什么被折叠?



