数据结构实验之链表三:链表的逆置
来源:互联网 发布:网络机顶盒能接音响吗 编辑:程序博客网 时间:2024/06/05 08:46
数据结构实验之链表三:链表的逆置
Time Limit: 1000MS Memory Limit: 65536KB
SubmitStatistic
Problem Description
输入多个整数,以-1作为结束标志,顺序建立一个带头结点的单链表,之后对该单链表的数据进行逆置,并输出逆置后的单链表数据。
Input
输入多个整数,以-1作为结束标志。
Output
输出逆置后的单链表数据。
Example Input
12 56 4 6 55 15 33 62 -1
Example Output
62 33 15 55 6 4 56 12
Hint
不得使用数组。
Author
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node * next;
};
void re(struct node * head,int n)
{
struct node *p,*tail;
tail=head;
while(tail->next!=NULL)///为了符合顺序建链表
{
tail=tail->next;
}
p=(struct node *)malloc(sizeof(struct node));
p->data=n;
p->next=NULL;
tail->next=p;
tail=p;
}
struct node *ar(struct node * head)
{
struct node *p,*q;
p=head->next;
head->next=NULL;
q=p->next;
while(p!=NULL)///换最后一个
{
p->next=head->next;///逆序插入
head->next=p;
p=q;
if(q!=NULL)
q=q->next;
}
return head;
}
int main()
{
struct node * head,*p;
int n;
head=(struct node *)malloc(sizeof(struct node));
head->next=NULL;
while(scanf("%d",&n)!=EOF&&n!=-1)
re(head,n);
head=ar(head);
p=head->next;//从头开始取值;
while(p->next!=NULL)
{
printf("%d ",p->data);
p=p->next;
}
printf("%d\n",p->data);
return 0;
}
///解决一直输入的情况
/**struct node * creat(struct node * head)
{
struct node * p,*tail;
head=(struct node * )malloc(sizeof(struct node ));
head->next=NULL;
tail=head;
while(1)
{
p=(struct node * )malloc(sizeof(struct node ));
p->next=NULL;
scanf("%d",&p->data);
if(p->data==-1)
break;
tail->next=p;
tail=p;
}
return head;
};*/
#include <stdlib.h>
struct node
{
int data;
struct node * next;
};
void re(struct node * head,int n)
{
struct node *p,*tail;
tail=head;
while(tail->next!=NULL)///为了符合顺序建链表
{
tail=tail->next;
}
p=(struct node *)malloc(sizeof(struct node));
p->data=n;
p->next=NULL;
tail->next=p;
tail=p;
}
struct node *ar(struct node * head)
{
struct node *p,*q;
p=head->next;
head->next=NULL;
q=p->next;
while(p!=NULL)///换最后一个
{
p->next=head->next;///逆序插入
head->next=p;
p=q;
if(q!=NULL)
q=q->next;
}
return head;
}
int main()
{
struct node * head,*p;
int n;
head=(struct node *)malloc(sizeof(struct node));
head->next=NULL;
while(scanf("%d",&n)!=EOF&&n!=-1)
re(head,n);
head=ar(head);
p=head->next;//从头开始取值;
while(p->next!=NULL)
{
printf("%d ",p->data);
p=p->next;
}
printf("%d\n",p->data);
return 0;
}
///解决一直输入的情况
/**struct node * creat(struct node * head)
{
struct node * p,*tail;
head=(struct node * )malloc(sizeof(struct node ));
head->next=NULL;
tail=head;
while(1)
{
p=(struct node * )malloc(sizeof(struct node ));
p->next=NULL;
scanf("%d",&p->data);
if(p->data==-1)
break;
tail->next=p;
tail=p;
}
return head;
};*/
0 0
- 数据结构实验之链表三:链表的逆置
- 数据结构实验之链表三:链表的逆置
- 数据结构实验之链表三:链表的逆置
- 数据结构实验之链表三:链表的逆置
- 数据结构实验之链表三:链表的逆置
- 数据结构实验之链表三:链表的逆置
- 数据结构实验之链表三:链表的逆置
- 数据结构实验之链表三:链表的逆置
- 【2118】数据结构实验之链表三:链表的逆置
- 数据结构实验之链表三:链表的逆置
- 数据结构实验之链表三:链表的逆置
- 数据结构实验之链表三:链表的逆置
- 数据结构实验之链表三:链表的逆置
- OJ 数据结构实验之链表三:链表的逆置
- 数据结构实验之链表三:链表的逆置
- 数据结构实验之链表三:链表的逆置
- 数据结构实验之链表三:链表的逆置
- 数据结构实验之链表三:链表的逆置
- hdu 置换群
- 1070. 结绳(25)
- selenium 超时设置/等待时间过长自动停止(python)
- iOS 两种压缩方式比较
- 最小生成树
- 数据结构实验之链表三:链表的逆置
- L1-009. N个数求和
- HashMap&&HshTable以及简单实现HashMap
- 调试休眠和唤醒
- CSS制作水平垂直居中对齐
- HBase原理-数据读取流程解析
- CAGradientLayer学习
- 《ACM程序设计》书中题目Y-25
- |BZOJ 1619|搜索|[Usaco2008 Nov]Guarding the Farm 保卫牧场