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<cstdlib>
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
struct node
{
int data;
struct node *next;
};
struct node*creat(struct node*head, int n)
{
head=(struct node*)malloc(sizeof(struct node));
head->next=NULL;
struct node*tail=head;
for(int i=1;i<=n;i++)
{
struct node*p=(struct node*)malloc(sizeof(struct node));
p->next=NULL;
scanf("%d",&p->data);
tail->next=p;
tail=p;
}
return head;
};
void show(struct node*head)
{
struct node*tail;
tail=head;
int top=1;
while(tail->next)
{
if(top)
top=0;
else
printf(" ");
printf("%d",tail->next->data);
tail=tail->next;
}
}
int main()
{
int n;
struct node*head1;
scanf("%d",&n);
head1=(struct node*)malloc(sizeof(struct node));
head1=creat(head1,n);
show(head1);
printf("\n");
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<cstdlib>
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
struct node
{
int data;
struct node *next;
};
struct node*creat(struct node*head, int n)
{
head=(struct node*)malloc(sizeof(struct node));
head->next=NULL;
struct node*tail=head;
for(int i=1;i<=n;i++)
{
struct node*p=(struct node*)malloc(sizeof(struct node));
p->next=NULL;
scanf("%d",&p->data);
tail->next=p;
tail=p;
}
return head;
};
struct node*creat1(struct node*head,int n)
{
head=(struct node*)malloc(sizeof(struct node));
head->next=NULL;
struct node *p,*tail;
tail=head;
for(int i=0;i<n;i++)
{
p=(struct node*)malloc(sizeof(struct node));
p->next=NULL;
scanf("%d",&p->data);
p->next=tail->next;
tail->next=p;
}
return head;
};
void show(struct node*head)
{
struct node*tail;
tail=head;
int top=1;
while(tail->next)
{
if(top)
top=0;
else
printf(" ");
printf("%d",tail->next->data);
tail=tail->next;
}
}
int main()
{
int n;
struct node*head1;
scanf("%d",&n);
head1=(struct node*)malloc(sizeof(struct node));
head1=creat1(head1,n);
show(head1);
printf("\n");
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<cstdlib>
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
struct node
{
int data;
struct node *next;
};
struct node*creat(struct node*head)
{
head=(struct node*)malloc(sizeof(struct node));
head->next=NULL;
struct node*tail=head;
for(int i=1;;i++)
{
struct node*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;
};
struct node*creat1(struct node*head,int n)
{
head=(struct node*)malloc(sizeof(struct node));
head->next=NULL;
struct node *p,*tail;
tail=head;
for(int i=0;i<n;i++)
{
p=(struct node*)malloc(sizeof(struct node));
p->next=NULL;
scanf("%d",&p->data);
p->next=tail->next;
tail->next=p;
}
return head;
};
struct node*nizhi(struct node*head)
{
struct node*p,*q;
p=head->next;
q=p->next;
head->next=NULL;
while(q)
{
p->next=head->next;
head->next=p;
p=q;
q=q->next;
}
p->next=head->next;
head->next=p;
return head;
};
void show(struct node*head)
{
struct node*tail;
tail=head;
int top=1;
while(tail->next)
{
if(top)
top=0;
else
printf(" ");
printf("%d",tail->next->data);
tail=tail->next;
}
}
int main()
{
int n;
struct node*head1;
head1=(struct node*)malloc(sizeof(struct node));
head1=creat(head1);
nizhi(head1);
show(head1);
printf("\n");
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
Hint
不得使用数组!
#include <bits/stdc++.h>
using namespace std;
struct node
{
int data;
struct node *next;
};
int main()
{
int n,m;
struct node *head, *head1, *q, *tail, *p,*p1,*head2;
head=(struct node*)malloc(sizeof(struct node));
head1=(struct node*)malloc(sizeof(struct node));
head2=(struct node*)malloc(sizeof(struct node));
head->next=NULL;
head1->next=NULL;
head2->next=NULL;
scanf("%d %d",&m,&n);
tail=head;
while(m--)
{
p=(struct node*)malloc(sizeof(struct node));
scanf("%d",&p->data);
p->next=tail->next;
tail->next=p;
tail=p;
}
tail=head1;
while(n--)
{
p=(struct node*)malloc(sizeof(struct node));
scanf("%d",&p->data);
p->next=tail->next;
tail->next=p;
tail=p;
}
p=head->next;
p1=head1->next;
head->next=NULL;
tail=head;
while(p1&&p)
{
if(p->data<p1->data)
{
tail->next=p;
tail=p;
p=p->next;
tail->next=NULL;
}
else
{
tail->next=p1;
tail=p1;
p1=p1->next;
tail->next=NULL;
}
}
if(p)
{
tail->next=p;
}
else
{
tail->next=p1;
}
p=head->next;
printf("%d",p->data);
p=p->next;
while(p!=NULL)
{
printf(" %d",p->data);
p=p->next;
}
printf("\n");
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 main()
{
int n,m,x,y;