线性表是最常见和常用的ADT。假设线性表的元素为整数,请基于单链式存储结构实现线性表ADT。
基本功能包括:
(1)建立线性表;
输入有两行:第一行一个整数,是输入元素的结束标志,例如0,则在输入结束时输入0,就表示输入结束了。第二行是线性表的各个元素,最后一个是结束标志。
(2)插入:
输入两个整数,即元素插入的位置和元素值
(3)删除:
输入一个整数,即要删除的元素值
(4)搜索:
输入一个整数,即要搜索元素的值
(5)输出:
输出线性表的各个元素,空格分开。
(6)集合的并运算:
输入创建第二个集合(线性表),完成并运算
(7)集合的交运算:
输入创建第二个集合(线性表),完成交运算
(8)合并两个有序线性表:
两个有序线性表,合并后仍然有序
测试样例:
0 //线性表输入结束标志
1 3 5 7 9 0 //线性表A的各个元素,最后是结束标志
2 10 //表示在第2个位置插入10
10 //表示删除值=10的数据元素
9 //查找元素9
22 // 查找元素22
0 //线性表输入结束标志
1 2 3 4 5 6 0 //线性表B的各个元素,最后是结束标志
例如:
输入
0
1 3 5 7 9 0
2 10
10
9
22
0
1 2 3 4 5 6 0
Result
A is created as: 1 3 5 7 9
After inserted A is 1 10 3 5 7 9
After deleted A is 1 3 5 7 9
9 is located at index of 5
22 is not found
B is created as: 1 2 3 4 5 6
A cross B is 1 3 5
A union B is 1 3 5 7 9 2 4 6
A union B in sequence is 1 2 3 4 5 6 7 9
#include<cstdio>
#include<cstdlib>
using namespace std;
typedef struct node
{
int data;
struct node *next;
}Node;
class List
{
private:
Node *head;
int len;
public:
List();
void create(int tag);
void Insert(int index,int x);
void Delete(int x);
void Find(int x);
void show();
void Cross(List a,List b);
void Union(List a,List b);
void Merge(List a,List b);
};
List::List()
{
head=new Node;
if(head==NULL)
{
printf("存储分配错误!\n");
exit(1);
}
head->next=NULL;
len=0;
}
void List::create(int tag)
{
int v;
Node *p;
p=head;
scanf("%d",&v);
while(v!=tag)
{
Node *t=new Node;
if(t==NULL)
{
printf("存储分配错误!\n");
exit(1);
}
t->data=v;
p->next=t;
p=t;
len++;
scanf("%d",&v);
}
p->next=NULL;
}
void List::Insert(int index,int x)
{
if(index<=0||index>len+1)
{
printf("位置不存在!\n");
exit(1);
}
Node *p;
int i=0;
p=head;
while(p->next!=NULL&&i<=index-2)
{
p=p->next;
i++;
}
Node *t=new Node;
t->data=x;
t->next=p->next;
p->next=t;
len++;
}
void List::Delete(int x)
{
Node *p;
p=head;
while(p->next!=NULL)
{
p=p->next;
if(p->next->data==x)
{
Node *t;
t=p->next;
p->next=t->next;
delete t;
len--;
return;
}
}
printf("找不到元素!\n");
}
void List::Find(int x)
{
Node *p;
p=head->next;
int i=0;
while(p!=NULL)
{
i++;
if(p->data==x)
{
printf("%d is located at index of %d\n",x,i);
return;
}
p=p->next;
}
printf("%d is not found\n",x);
}
void List::show()
{
Node *p;
p=head->next;
while(p!=NULL)
{
printf(" %d",p->data);
p=p->next;
}
printf("\n");
}
void List::Cross(List a,List b)
{
Node *t1;
Node *t2;
t1=a.head->next;
int k=0;
while(t1!=NULL)
{
t2=b.head->next;
while(t2!=NULL)
{
if(t1->data==t2->data)Insert(++k,t1->data);
t2=t2->next;
}
t1=t1->next;
}
}
void List::Union(List a,List b)
{
Cross(a,b);
Node *t1,*t2;
t1=a.head->next;
while(t1!=NULL)
{
t2=b.head->next;
while(t2!=NULL)
{
if(t1->data==t2->data)break;
t2=t2->next;
}
if(t2==NULL)Insert(len+1,t1->data);
t1=t1->next;
}
t2=b.head->next;
while(t2!=NULL)
{
t1=head->next;
while(t1!=NULL)
{
if(t2->data==t1->data)break;
t1=t1->next;
}
if(t1==NULL)Insert(len+1,t2->data);
t2=t2->next;
}
}
void List::Merge(List a,List b)
{
int k=0;
Node *t1,*t2;
t1=a.head->next;
t2=b.head->next;
while(t1!=NULL&&t2!=NULL)
{
if(t1->data==t2->data)
{
Insert(++k,t1->data);
t1=t1->next;
t2=t2->next;
}
else if(t1->data<t2->data)
{
Insert(++k,t1->data);
t1=t1->next;
}
else
{
Insert(++k,t2->data);
t2=t2->next;
}
}
while(t1!=NULL)
{
Insert(++k,t1->data);
t1=t1->next;
}
while(t2!=NULL)
{
Insert(++k,t2->data);
t2=t2->next;
}
}
int main()
{
List la,lb,lcross,lunion,lmerge;
int tag,index,value,f1,f2;
scanf("%d",&tag);
la.create(tag);
printf("A is created as:");
la.show();
scanf("%d %d",&index,&value);
la.Insert(index,value);
printf("After inserted A is");
la.show();
scanf("%d",&value);
la.Delete(value);
printf("After deleted A is");
la.show();
scanf("%d",&f1);
la.Find(f1);
scanf("%d",&f2);
la.Find(f2);
scanf("%d",&tag);
lb.create(tag);
printf("B is created as:");
lb.show();
lcross.Cross(la,lb);
printf("A cross B is");
lcross.show();
lunion.Union(la,lb);
printf("A union B is");
lunion.show();
lmerge.Merge(la,lb);
printf("A union B in sequence is");
lmerge.show();
return 0;
}
实习目的:熟练掌握链表的建立及基本操作
问题描述:
1)实现链表的排序(升序)
2)实现两个有序链表的合并:A=A∪B,要求合并后仍然有序。
提交前请将所有的提示信息去掉,只保留最后的输出结果。例如运行时:从键盘直接输入:
2 1 2
3 1 2 3
输出结果为:
1
2
3
分别表示第一个链表元素个数为2,元素分别为 1,2 ;第二个链表元素个数为3,元素分别为1,2,3。
例如:
输入
2 2 1
3 1 2 3
Result
1
2
3
#include<cstdio>
#include<cstdlib>
#include<algorithm>
using namespace std;
typedef struct node
{
int data;
struct node *next;
}Node;
class List
{
private:
Node *head;
int len;
public:
List();
void create(int l);
void Insert(int index,int x);
void ascendsort();
void Merge(List a,List b);
void show();
};
List::List()
{
head=new Node;
if(head==NULL)
{
printf("存储分配错误!\n");
exit(1);
}
head->next=NULL;
len=0;
}
void List::create(int l)
{
Node *p;
p=head;
int v;
for(int i=1;i<=l;i++)
{
scanf("%d",&v);
Node *t=new Node;
t->data=v;
p->next=t;
p=t;
len++;
}
p->next=NULL;
}
void List::Insert(int index,int x)
{
if(index>len+1||index<0)
{
printf("位置不存在!\n");
exit(1);
}
int i=0;
Node *p;
p=head;
while(p->next!=NULL&&i<=index-2)
{
p=p->next;
i++;
}
Node *t=new Node;
t->data=x;
t->next=p->next;
p->next=t;
len++;
}
void List::ascendsort()
{
Node *t1,*t2;
t1=head->next;
while(t1!=NULL)
{
t2=t1;
while(t2!=NULL)
{
if(t1->data>t2->data)swap(t1->data,t2->data);
t2=t2->next;
}
t1=t1->next;
}
}
void List::show()
{
Node *t=head->next;
while(t!=NULL)
{
printf("%d\n",t->data);
t=t->next;
}
}
void List::Merge(List a,List b)
{
int k=0;
Node *t1,*t2;
t1=a.head->next;
t2=b.head->next;
while(t1!=NULL&&t2!=NULL)
{
if(t1->data==t2->data)
{
Insert(++k,t1->data);
t1=t1->next;
t2=t2->next;
}
else if(t1->data<t2->data)
{
Insert(++k,t1->data);
t1=t1->next;
}
else
{
Insert(++k,t2->data);
t2=t2->next;
}
}
while(t1!=NULL)
{
Insert(++k,t1->data);
t1=t1->next;
}
while(t2!=NULL)
{
Insert(++k,t2->data);
t2=t2->next;
}
}
int main()
{
List l1,l2,lmerge;
int len1,len2;
scanf("%d",&len1);
l1.create(len1);
scanf("%d",&len2);
l2.create(len2);
l1.ascendsort();
l2.ascendsort();
lmerge.Merge(l1,l2);
lmerge.show();
return 0;
}