A - 数据结构实验之链表一:顺序建立链表
Description
输入N个整数,按照输入的顺序建立单链表存储,并遍历所建立的单链表,输出这些数据。
Input
第一行输入整数的个数N;
第二行依次输入每个整数。
Output
输出这组整数。
Sample
Input
8
12 56 4 6 55 15 33 62
Output
12 56 4 6 55 15 33 62
Hint
不得使用数组!
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *creat(int n)
{
struct node *tail,*head,*p;
head=(struct node *)malloc(sizeof(struct node));
head->next=NULL;
tail=head;
for(int i=0;i<n;i++)
{
p=(struct node *)malloc(sizeof(struct node));
scanf("%d",&p->data);
p->next=NULL;
tail->next=p;
tail=p;
}
return head;
}
void print(struct node *head)
{
struct node *p;
p=head->next;
while(p->next!=NULL)
{
printf("%d ",p->data);
p=p->next;
}
printf("%d",p->data);
}
int main()
{
struct node *head;
int n;
scanf("%d",&n);
head=creat(n);
print(head);
return 0;
}
B - 数据结构实验之链表二:逆序建立链表
Description
输入整数个数N,再输入N个整数,按照这些整数输入的相反顺序建立单链表,并依次遍历输出单链表的数据。
Input
第一行输入整数N;;
第二行依次输入N个整数,逆序建立单链表。
Output
依次输出单链表所存放的数据。
Sample
Input
10
11 3 5 27 9 12 43 16 84 22
Output
22 84 16 43 12 9 27 5 3 11
Hint
不能使用数组!
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *creat(int n)
{
struct node *head,*p;
head = (struct node *)malloc(sizeof(struct node));
head->next=NULL;
for(int i=0;i<n;i++)
{
p=(struct node *)malloc(sizeof(struct node));
scanf("%d",&p->data);
p->next=head->next;
head->next=p;
}
return head;
}
void print(struct node *head)
{
struct node *p;
p=head->next;
while(p->next!=NULL)
{
printf("%d ",p->data);
p=p->next;
}
printf("%d",p->data);
}
int main()
{
int n;
struct node *head;
scanf("%d",&n);
head = creat(n);
print(head);
return 0;
}
C - 数据结构实验之链表三:链表的逆置
Description
输入多个整数,以-1作为结束标志,顺序建立一个带头结点的单链表,之后对该单链表的数据进行逆置,并输出逆置后的单链表数据。
Input
输入多个整数,以-1作为结束标志。
Output
输出逆置后的单链表数据。
Sample
Input
12 56 4 6 55 15 33 62 -1
Output
62 33 15 55 6 4 56 12
Hint
不得使用数组。
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *creat()
{
struct node *head,*p;
int a;
head = (struct node *)malloc(sizeof(struct node));
head->next=NULL;
while(scanf("%d",&a)&&a!=-1)
{
p=(struct node *)malloc(sizeof(struct node));
p->data=a;
p->next=head->next;
head->next=p;
}
return head;
}
void print(struct node *head)
{
struct node *p;
p=head->next;
while(p->next!=NULL)
{
printf("%d ",p->data);
p=p->next;
}
printf("%d",p->data);
}
int main()
{
struct node *head;
head = creat();
print(head);
return 0;
}
D - 数据结构实验之链表四:有序链表的归并
Description
分别输入两个有序的整数序列(分别包含M和N个数据),建立两个有序的单链表,将这两个有序单链表合并成为一个大的有序单链表,并依次输出合并后的单链表数据。
Input
第一行输入M与N的值;
第二行依次输入M个有序的整数;
第三行依次输入N个有序的整数。
Output
输出合并后的单链表所包含的M+N个有序的整数。
Sample
Input
6 5
1 23 26 45 66 99
14 21 28 50 100
Output
1 14 21 23 26 28 45 50 66 99 100
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *creat(int n)
{
struct node *tail,*head,*p;
head=(struct node *)malloc(sizeof(struct node));
head->next=NULL;
tail=head;
for(int i=0;i<n;i++)
{
p=(struct node *)malloc(sizeof(struct node));
scanf("%d",&p->data);
p->next=NULL;
tail->next=p;
tail=p;
}
return head;
}
struct node *merge(struct node *t1,struct node *t2)
{
struct node *t,*p1,*p2;
p1=t1->next;
p2=t2->next;
t=t1;
free(t2);
while(p1 && p2)
{
if(p1->data>p2->data)
{
t->next=p2;
t=p2;
p2=p2->next;
}
else
{
t->next=p1;
t=p1;
p1=p1->next;
}
}
if(p1)
{
t->next=p1;
t=p1;
p1=p1->next;
}
else if(p2)
{
t->next=p2;
t=p2;
p2=p2->next;
}
return t1;
}
void print(struct node *head)
{
struct node *p;
p=head->next;
while(p->next!=NULL)
{
printf("%d ",p->data);
p=p->next;
}
printf("%d",p->data);
}
int main()
{
int n,m;
struct node *p1,*p2,*p;
scanf("%d%d",&n,&m);
p1=creat(n);
p2=creat(m);
p=merge(p1,p2);
print(p);
return 0;
}
E - 数据结构实验之链表五:单链表的拆分
Description
输入N个整数顺序建立一个单链表,将该单链表拆分成两个子链表,第一个子链表存放了所有的偶数,第二个子链表存放了所有的奇数。两个子链表中数据的相对次序与原链表一致。
Input
第一行输入整数N;;
第二行依次输入N个整数。
Output
第一行分别输出偶数链表与奇数链表的元素个数;
第二行依次输出偶数子链表的所有数据;
第三行依次输出奇数子链表的所有数据。
Sample
Input
10
1 3 22 8 15 999 9 44 6 1001
Output
4 6
22 8 44 6
1 3 15 999 9 1001
Hint
不得使用数组!
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
int length;
};
struct node *creat(int n)
{
struct node *head,*tail,*p;
head=(struct node *)malloc(sizeof(struct node));
head->next=NULL;
tail=head;
for(int i=0;i<n;i++)
{
p=(struct node *)malloc(sizeof(struct node));
scanf("%d",&p->data);
p->next=NULL;
tail->next=p;
tail=p;
}
return head;
}
void print(struct node *head)
{
struct node *p;
p=head->next;
while(p->next!=NULL)
{
printf("%d ",p->data);
p=p->next;
}
printf("%d\n",p->data);
}
int merge(struct node *head)
{
struct node *p1,*p2,*tail1,*tail2,*p;
int num1=0,num2=0;
p1=(struct node *)malloc(sizeof(struct node));
p2=(struct node *)malloc(sizeof(struct node));
p1->next=NULL;
p2->next=NULL;
tail1=p1;
tail2=p2;
p=head->next;
while(p!=NULL)
{
int t=p->data;
if(t%2==0)
{
tail2->next=p;
tail2=p;
num2++;
}
else
{
tail1->next=p;
tail1=p;
num1++;
}
p=p->next;
}
tail1->next=NULL;
tail2->next=NULL;
printf("%d %d\n",num2,num1);
print(p2);
print(p1);
return 0;
}
int main()
{
struct node *head,*t1;
int n;
scanf("%d",&n);
head=creat(n);
merge(head);
return 0;
}
F - 数据结构实验之链表六:有序链表的建立
Description
输入N个无序的整数,建立一个有序链表,链表中的结点按照数值非降序排列,输出该有序链表。
Input
第一行输入整数个数N;
第二行输入N个无序的整数。
Output
依次输出有序链表的结点值。
Sample
Input
6
33 6 22 9 44 5
Output
5 6 9 22 33 44
Hint
不得使用数组!
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *creat(int n)
{
struct node *head,*tail,*p;
head=(struct node *)malloc(sizeof(struct node));
head->next=NULL;
tail=head;
for(int i=0;i<n;i++)
{
p=(struct node *)malloc(sizeof(struct node));
scanf("%d",&p->data);
tail->next=p;
tail=p;
}
tail->next=NULL;
return head;
}
struct node *sort(struct node *head)
{
struct node *p1,*p2;
for(p1=head->next;p1!=NULL;p1=p1->next)
{
for(p2=p1->next;p2!=NULL;p2=p2->next)
{
if(p1->data>p2->data)
{
int t=p1->data;
p1->data=p2->data;
p2->data=t;
}
}
}
return head;
}
void print(struct node *head)
{
struct node *p;
p=head->next;
while(p!=NULL)
{
printf("%d ",p->data);
p=p->next;
}
printf("%d",p->data);
}
int main()
{
struct node *head;
int n;
scanf("%d",&n);
head=creat(n);
head=sort(head);
print(head);
return 0;
}
G - 数据结构实验之链表七:单链表中重复元素的删除
Description
按照数据输入的相反顺序(逆位序)建立一个单链表,并将单链表中重复的元素删除(值相同的元素只保留最后输入的一个)。
Input
第一行输入元素个数 n (1 <= n <= 15);
第二行输入 n 个整数,保证在 int 范围内。
Output
第一行输出初始链表元素个数;
第二行输出按照逆位序所建立的初始链表;
第三行输出删除重复元素后的单链表元素个数;
第四行输出删除重复元素后的单链表。
Sample
Input
10
21 30 14 55 32 63 11 30 55 30
Output
10
30 55 30 11 63 32 55 14 30 21
7
30 55 11 63 32 14 21
#include<stdio.h>
#include<stdlib.h>
int n;
struct node
{
int data;
struct node *next;
};
struct node *creat(int n)
{
struct node *head,*tail,*p;
head=(struct node *)malloc(sizeof(struct node));
head->next=NULL;
tail=head;
for(int i=0;i<n;i++)
{
p=(struct node *)malloc(sizeof(struct node));
scanf("%d",&p->data);
p->next=NULL;
p->next=tail->next;
tail->next=p;
}
return head;
}
struct node *del(struct node *head)
{
struct node *p1,*p2;
p1=head->next;
while(p1!=NULL)
{
p2=p1;
while(p2->next!=NULL)
{
if(p1->data==p2->next->data)
{
p2->next=p2->next->next;
n--;
}
else
{
p2=p2->next;
}
}
p1=p1->next;
}
return head;
}
void print(struct node *l)
{
struct node *p;
p=l->next;
while(p)
{
if(p->next!=NULL)
{
printf("%d ",p->data);
}
else
{
printf("%d\n",p->data);
}
p=p->next;
}
}
int main()
{
struct node *head;
scanf("%d",&n);
head=creat(n);
printf("%d\n",n);
print(head);
head=del(head);
printf("%d\n",n);
print(head);
return 0;
}
I - 数据结构实验之链表九:双向链表
Description
学会了单向链表,我们又多了一种解决问题的能力,单链表利用一个指针就能在内存中找到下一个位置,这是一个不会轻易断裂的链。但单链表有一个弱点——不能回指。比如在链表中有两个节点A,B,他们的关系是B是A的后继,A指向了B,便能轻易经A找到B,但从B却不能找到A。一个简单的想法便能轻易解决这个问题——建立双向链表。在双向链表中,A有一个指针指向了节点B,同时,B又有一个指向A的指针。这样不仅能从链表头节点的位置遍历整个链表所有节点,也能从链表尾节点开始遍历所有节点。对于给定的一列数据,按照给定的顺序建立双向链表,按照关键字找到相应节点,输出此节点的前驱节点关键字及后继节点关键字。
Input
第一行两个正整数n(代表节点个数),m(代表要找的关键字的个数)。第二行是n个数(n个数没有重复),利用这n个数建立双向链表。接下来有m个关键字,每个占一行。
Output
对给定的每个关键字,输出此关键字前驱节点关键字和后继节点关键字。如果给定的关键字没有前驱或者后继,则不输出。
注意:每个给定关键字的输出占一行。一行输出的数据之间有一个空格,行首、行末无空格。
Sample
Input
10 3
1 2 3 4 5 6 7 8 9 0
3
5
0
Output
2 4
4 6
9
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *prior;
struct node *next;
};
struct node *creat(int n)
{
struct node *head,*tail,*p;
head=(struct node *)malloc(sizeof(struct node));
head->next=NULL;
head->prior=NULL;
tail=head;
for(int i=0;i<n;i++)
{
p=(struct node *)malloc(sizeof(struct node));
scanf("%d",&p->data);
p->next=NULL;
tail->next=p;
p->prior=tail;
tail=p;
}
return head;
}
void get(struct node *head,int n)
{
struct node *p;
p=head->next;
while(p->data!=n)
{
p=p->next;
}
if(p->next!=NULL && p->prior!=head)
{
printf("%d %d\n",p->prior->data,p->next->data);
}
else if(p->next==NULL)
{
printf("%d\n",p->prior->data);
}
else if(p->prior==head)
{
printf("%d\n",p->next->data);
}
}
int main()
{
int n,m;
struct node *head,*p;
scanf("%d%d",&n,&m);
head=creat(n);
while(m--)
{
int t;
scanf("%d",&t);
get(head,t);
}
return 0;
}
C - 师--链表的结点插入
Description
给出一个只有头指针的链表和 n 次操作,每次操作为在链表的第 m 个元素后面插入一个新元素x。若m 大于链表的元素总数则将x放在链表的最后。
Input
多组输入。每组数据首先输入一个整数n(n∈[1,100]),代表有n次操作。
接下来的n行,每行有两个整数Mi(Mi∈[0,10000]),Xi。
Output
对于每组数据。从前到后输出链表的所有元素,两个元素之间用空格隔开。
Sample
Input
4
1 1
1 2
0 3
100 4
Output
3 1 2 4
Hint
样例中第一次操作1 1,由于此时链表中没有元素,1>0,所以此时将第一个数据插入到链表的最后,也就是头指针的后面。
#include<stdio.h>
#include<stdlib.h>
int m;
struct node
{
int data;
struct node *next;
};
void print(struct node *head)
{
while(head->next!=NULL)
{
if(head->next->next!=NULL)
{
printf("%d ",head->next->data);
}
else
{
printf("%d\n",head->next->data);
}
head=head->next;
}
}
int main()
{
struct node *head,*tail,*p;
int n;
while(scanf("%d",&n)!=EOF)
{
int d=0;
head=(struct node *)malloc(sizeof(struct node));
head->next=NULL;
for(int j=1;j<=n;j++)
{
p=(struct node *)malloc(sizeof(struct node));
scanf("%d%d",&m,&p->data);
tail=head;
if(m>d)
{
p->next=NULL;
for(int i=1;i<=d;i++)
{
tail=tail->next;
}
tail->next=p;
d++;
}
else
{
for(int i=1;i<=m;i++)
{
tail=tail->next;
}
p->next=tail->next;
tail->next=p;
d++;
}
}
print(head);
}
return 0;
}
约瑟夫问题
Description
n个人想玩残酷的死亡游戏,游戏规则如下:
n个人进行编号,分别从1到n,排成一个圈,顺时针从1开始数到m,数到m的人被杀,剩下的人继续游戏,活到最后的一个人是胜利者。
请输出最后一个人的编号。
Input
输入n和m值。
Output
输出胜利者的编号。
Sample
Input
5 3
Output
4
Hint
第一轮:3被杀第二轮:1被杀第三轮:5被杀第四轮:2被杀
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *creat(int n)
{
struct node *head,*tail,*p,*t;
head=(struct node *)malloc(sizeof(struct node));
head->data=1;
head->next=NULL;
p=tail=head;
for(int i=2;i<=n;i++)
{
t=(struct node *)malloc(sizeof(struct node));
t->data=i;
t->next=NULL;
tail->next=t;
tail=t;
}
tail->next=p;
return p;
}
int main()
{
int n,m,d=0;
struct node *head,*p,*q;
scanf("%d%d",&n,&m);
head=creat(n);
q=head;
while(q->next!=head)
{
q=q->next;
}
while(q->next!=q)
{
p=q->next;
d++;
if(d==m)
{
q->next=p->next;
free(p);
d=0;
}
else
{
q=q->next;
}
}
printf("%d\n",q->data);
return 0;
}
不敢死队问题
Description
说到“敢死队”,大家不要以为我来介绍电影了,因为数据结构里真有这么道程序设计题目,原题如下:
有M个敢死队员要炸掉敌人的一个碉堡,谁都不想去,排长决定用轮回数数的办法来决定哪个战士去执行任务。如果前一个战士没完成任务,则要再派一个战士上去。现给每个战士编一个号,大家围坐成一圈,随便从某一个战士开始计数,当数到5时,对应的战士就去执行任务,且此战士不再参加下一轮计数。如果此战士没完成任务,再从下一个战士开始数数,被数到第5时,此战士接着去执行任务。以此类推,直到任务完成为止。
这题本来就叫“敢死队”。“谁都不想去”,就这一句我觉得这个问题也只能叫“不敢死队问题”。今天大家就要完成这道不敢死队问题。我们假设排长是1号,按照上面介绍,从1号开始数,数到5的那名战士去执行任务,那么排长是第几个去执行任务的?
Input
输入包括多组数据,每组一行,包含一个整数M(0<=M<=10000)(敢死队人数),若M==0,输入结束,不做处理。
Output
输出一个整数n,代表排长是第n个去执行任务。
Sample
Input
9
6
223
0
Output
2
6
132
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *creat(int n)
{
struct node *head,*tail,*p,*t;
head=(struct node *)malloc(sizeof(struct node));
head->data=1;
head->next=NULL;
p=tail=head;
for(int i=2;i<=n;i++)
{
t=(struct node *)malloc(sizeof(struct node));
t->data=i;
t->next=NULL;
tail->next=t;
tail=t;
}
tail->next=p;
return p;
}
int ret(struct node *head)
{
int num=0,d=0;
struct node *tail,*p1,*p2;
p1=head;
while(p1->next!=head)
{
p1=p1->next;
}
while(p1->next!=p1)
{
p2=p1->next;
d++;
if(d==5)
{
if(p2->data==1)
{
break;
}
p1->next=p2->next;
free(p2);
d=0;
num++;
}
else
{
p1=p1->next;
}
}
return num+1;
}
int main()
{
struct node *head;
int n;
while(scanf("%d",&n) && n!=0)
{
head=creat(n);
printf("%d\n",ret(head));
}
return 0;
}
H - 数据结构实验之链表八:Farey序列
Description
Farey序列是一个这样的序列:其第一级序列定义为(0/1,1/1),这一序列扩展到第二级形成序列(0/1,1/2,1/1),扩展到第三极形成序列(0/1,1/3,1/2,2/3,1/1),扩展到第四级则形成序列(0/1,1/4,1/3,1/2,2/3,3/4,1/1)。以后在每一级n,如果上一级的任何两个相邻分数a/c与b/d满足(c+d)<=n,就将一个新的分数(a+b)/(c+d)插入在两个分数之间。对于给定的n值,依次输出其第n级序列所包含的每一个分数。
Input
输入一个整数n(0<n<=100)
Output
依次输出第n级序列所包含的每一个分数,每行输出10个分数,同一行的两个相邻分数间隔一个制表符的距离。
Sample
Input
6
Output
0/1 1/6 1/5 1/4 1/3 2/5 1/2 3/5 2/3 3/4 4/5 5/6 1/1
Hint
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data1;
int data2;
struct node *next;
};
int sum=0;
void insert(struct node *head,int n)
{
struct node *tail,*p,*q;
tail=head->next;
while(tail->next!=NULL)
{
p=tail->next;
if(p->data2+tail->data2<=n)
{
q=(struct node *)malloc(sizeof(struct node));
q->next=NULL;
q->data1=p->data1+tail->data1;
q->data2=p->data2+tail->data2;
q->next=tail->next;
tail->next=q;
tail=q;
}
else
{
tail=tail->next;
}
}
}
void print(struct node *head)
{
struct node *p;
p=head->next;
while(p!=NULL)
{
sum++;
if(sum%10==0)
{
printf("%d/%d\n",p->data1,p->data2);
}
else
{
printf("%d/%d\t",p->data1,p->data2);
}
p=p->next;
}
}
int main()
{
int n;
struct node *head,*tail,*p,*q;
scanf("%d",&n);
head=(struct node *)malloc(sizeof(struct node));
head->next=NULL;
tail=head;
p=(struct node *)malloc(sizeof(struct node));
p->data1=0;
p->data2=1;
p->next=NULL;
q=(struct node *)malloc(sizeof(struct node));
q->data1=1;
q->data2=1;
q->next=NULL;
tail->next=p;
tail=p;
tail->next=q;
for(int i=2;i<=n;i++)
{
insert(head,i);
}
print(head);
return 0;
}
移动小球
Description
给你n个小球,从左到右编号依次为1,2,3,4,5,6.........n排成一行。现在有以下2种操作:A x y表示把编号为x小球移动到编号为y的小球的左边(和y相邻)。Q x为询问编号为x的小球左边的球号,如果x左边没有小球的话输出"cyk666"。
Input
第一行输入一个T,表示有T组测试数据。(1<=T<=100)
随后每一组测试数据第一行是两个整数N,M,其中N表示球的个数(1 随后有M行询问,第一个字符是操作类型s。
当s为'A'时,输入x,y表示把编号为x小球移动到编号为y的小球的左边。
当s为'Q'时,输入x表示询问小球x左边的球号。保证(1<=x<=N,1<=y<=N,1<=N<=100000)
Output
输出每次询问的球号,如果这样的小球不存在,输出"cyk666"(不包括引号)。
Sample
Input
1 6 5 A 1 2 A 1 4 A 3 5 Q 5 Q 2
Output
3 cyk666
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
using namespace std;
struct node
{
int data;
struct node *next;
};
int a,b;
void insert(struct node *head,int a,int b)
{
int t;
struct node *q,*q1,*p,*pnew;
q=head;
q1=head->next;
while(q->next->data!=a)
{
q=q->next;
q1=q1->next;
}
t=q->next->data;
q->next=q1->next;
free(q1);
p=head;
while(p->next->data!=b)
{
p=p->next;
}
pnew=(struct node *)malloc(sizeof(struct node));
pnew->data=t;
pnew->next=p->next;
p->next=pnew;
}
void get(struct node *head,int a)
{
struct node *p;
p=head;
while(p->next->data!=a)
{
p=p->next;
}
if(p==head||p==NULL)
{
printf("cyk666\n");
}
else
{
printf("%d\n",p->data);
}
}
struct node *creat(int n)
{
struct node *head,*p,*tail;
head=(struct node *)malloc(sizeof(struct node));
head->next=NULL;
tail=head;
for(int i=1; i<=n; i++)
{
p=(struct node *)malloc(sizeof(struct node));
p->data=i;
tail->next=p;
tail=p;
p->next=NULL;
}
return head;
}
int main()
{
int T,n,m,x,y;
struct node *head;
char s;
cin>>T;
while(T--)
{
scanf("%d %d",&n,&m);
head=creat(n);
for(int i=1; i<=m; i++)
{
cin>>s;
if(s=='A')
{
scanf("%d %d",&x,&y);
insert(head,x,y);
}
else
{
scanf("%d",&x);
get(head,x);
}
}
}
return 0;
}