线性表是最常见和常用的ADT。假设线性表的元素为整数,请基于顺序存储结构实现线性表ADT。
基本功能包括:
(1)建立线性表;
输入有两行,第一行是一个整数n,线性表的长度; 第二行是n和数据元素
(2)插入:
输入两个整数,即元素插入的位置和元素值
(3)删除:
输入一个整数,即要删除的元素
(4)搜索:
输入一个整数,即搜索元素的值
(5)输出:
输出线性表的各个元素,空格分开。
(6)集合的并运算:
输入创建第二个集合(线性表),完成并运算
(7)集合的交运算:
输入创建第二个集合(线性表),完成交运算
(8)合并两个有序线性表:
两个有序线性表,合并后仍然有序
测试数据:
5 //线性表A的长度
1 3 5 7 9 //线性表A的数据
2 10 //表示在第2个位置插入10
10 //表示删除值=10的数据元素
9 //查找元素9
11/ 查找元素22
6 //线性表B的长度
1 2 3 4 5 6
输入
5
1 3 5 7 9
2 10
10
9
22
6
1 2 3 4 5 6
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 <bits/stdc++.h>
using namespace std;
class SeqList
{
private:
int * data;
int last;
public:
SeqList ( int sz );
~SeqList ( )
{
delete [ ] data;
}
void input ();
void output() ;
void Insert ( const int &x, int i);
void Remove ( int i);
int Search ( const int &x);
int getData ( int i )const//返回下标为时i的数值data
{
if(i>=0&&i<=last)
{
return data[i];
}
else
return -1 ;
};
int Length()const//顺序表长度
{
return last+1;
};
void Sort();//排序 交换法
void cross(SeqList &LA,SeqList &LB);//交运算 生成一个新的顺序表
void unions (SeqList &LA,SeqList &LB);//并运算 生成一个新的顺序表
} ;
SeqList::SeqList ( int sz )
{
if ( sz > 0 )
{
data = new int[sz];
last = -1;
}
}
void SeqList:: input()
{
cin >>last;
for (int i=0; i<last; i++)
cin>>data [i];
last--;
}
void SeqList:: output()
{
for(int i=0; i<=last; i++)
{
cout<<data[i]<<' ';
}
}
void SeqList:: Insert(const int &x, int i)
{
for(int j=last; j>=i; j--)
{
data[j+1]=data[j];
}
data[i]=x;
last++;
}
void SeqList:: Remove( int i)
{
for(int j=i; j<=last; j++)
{
data[j-1]=data[j];
}
last--;
}
int SeqList:: Search( const int &x)
{
for(int i=0; i<=last; i++)
{
if(data[i]==x)
return i+1;
}
return 0;
}
void SeqList :: cross(SeqList &LA,SeqList &LB) //交运算
{
int n=LA.Length(),m=LB.Length(),i=0,j=0,k,x;
while(i<=n)
{
x=LA.getData(i);//以下标从LA表中取出数值
k=LB.Search(x);//在B中查找
if(k!=0) //若存在,既LA和LB共有元素
{
data[j]=x; //加入下一表项
j++;
last=j-1;
}
i++;
}
}
void SeqList :: unions (SeqList &LA,SeqList &LB) //并运算
{
int n=LA.Length(),m=LB.Length(),i=0,j=0,k,x;
while(i<n)//先将LA表中所有元素加入
{
data[i]=LA.getData(i);
i++;
}
while(j<m) //从LB开始循环,若没有加入表项
{
x=LB.getData(j);//以下标从LB表中取出数值
k=LA.Search(x);//在LA中查找
if(k==0) //如果不存在
{
data[i]=x;//加入下一表项
i++;
last=i-1;
}
j++;
}
}
void SeqList :: Sort()//顺序表排序 冒泡
{
for(int i=0;i<=last-1;i++)
{
for(int j=i+1;j<=last;j++)
{
if(data[i]>data[j])
{
int tmp=data[j];
data[j]=data[i];
data[i]=tmp;
}
}
}
}
/
int main()
{
SeqList myList(50);
SeqList myList1(50);
SeqList lcross(50);
SeqList lunion(50);
myList.input();
cout<<"A is created as: ";
myList.output();
cout<<endl;
int where,value;
cin >>where;
cin >>value;
myList.Insert(value,where-1);
cout<<"After inserted A is ";
myList.output ();
cout<<endl;
int delnum;
cin>>delnum;
myList.Remove(myList.Search(delnum));
cout<<"After deleted A is ";
myList.output ();
cout<<endl;
cin >>where;
if(myList.Search(where))
cout<<where<<" is located at index of "<<myList.Search(where)<<endl;
else
cout<<where<<" is not found"<<endl;
cin >>where;
if(myList.Search(where))
cout<<where<<" is located at index of "<<myList.Search(where)<<endl;
else
cout<<where<<" is not found"<<endl;
myList1.input();
cout<<"B is created as: ";
myList1.output();
cout<<endl;
cout<<"A cross B is ";
lcross.cross(myList,myList1);
lcross.output();
cout<<endl;
cout<<"A union B is ";
lunion.unions(myList,myList1);
lunion.output();
cout<<endl;
cout<<"A union B in sequence is ";
lunion.Sort();
lunion.output();
return 1;
}