#include <stdlib.h>
#include <iostream>
using namespace std;
const int Size=100;
template <class T>
class SeqList{
protected:
int maxSize; //表的最大空间
int last; //元素最后下标
T *data; //数组
void reSize(int size); //改变数组的空间大小
public:
SeqList(int sz=Size); //构造函数
SeqList(const SeqList<T> &S); //复制构造函数
SeqList<T> &operator=(SeqList<T> &S); //赋值函数
~SeqList() {delete []data;} //析构函数
int Size() const {return maxSize;} //表的最大容量
int Length() const {return (last+1);} //计算表的长度
bool isEmpty() {return (last==-1)? true:false;} //判断表空
bool isFull() {return (maxSize==last+1)? true:false;} //判断表满
int Search1(T &x)const; //搜索指定值,返回表项值
int Search(T &x)const; //搜索指定值,返回表项值
int Locate(int i)const; //定位第i个表项
bool getData(int i,T &x)const; //取出第i个表项
bool setData(int i,T &x); //修改第i个表项
bool Insert(int i,T &x); //在第i个表项后插入值
bool Remove(int i); //删除第i个表项
void Sort(); //排序
void Input(); //输入
void Output(); //输出
void Use(int size) {reSize(size);} //调用reSize函数
void Union(SeqList <T> L); //实现两个集合的并集
void Intersection(SeqList<T> L); //实现两个集合的交集
void Union1(SeqList <T> L); //实现有序表的合并
void delete1(T &x);
};
template <class T>
SeqList<T>::SeqList(int sz){
if(sz>0){
maxSize=sz;
last=-1;
data=new T[sz];
if(data==NULL) {
cout<<"存储分配错误!"<<endl;
exit(1);
}
}
}
template <class T>
SeqList<T>::SeqList(const SeqList<T> &S) {
maxSize=S.Size();
last=S.Length()-1;
T value;
data=new T[maxSize];
if(data==NULL) {
cout<<"存储分配错误!"<<endl;
exit(1);
}
for(int i=1;i<=last+1;i++) {
S.getData(i,value);
data[i-1]=value;
}
}
template <class T>
SeqList<T> &SeqList<T>::operator=(SeqList<T> &S) {
maxSize=S.Size();
last=S.Length()-1;
T value;
data=new T[maxSize];
if(data==NULL) {
cout<<"存储分配错误"<<endl;
exit(1);
}
for(int i=1;i<=last+1;i++) {
S.getData(i,value);
data[i-1]=value;
}
return (*this);
}
template <class T>
int SeqList<T>::Search1(T&x)const{
int i;
for(i=0;i<=last;i++) {
if(data[i]==x) {
cout<<x<<" is located at index of ";
cout<<i+1<<endl;
return i+1;
}
}
cout<<x<<" is not found"<<endl;
return -1;
}
template <class T>
int SeqList<T>::Search(T&x)const{
int i;
for(i=0;i<=last;i++) {
if(data[i]==x) {
cout<<i+1<<endl;
return i+1;
}
}
return -1;
}
template <class T>
int SeqList<T>::Locate(int i)const {
if(i>=1&&i<=last+1)
return i;
return -1;
}
template <class T>
void SeqList<T>::delete1(T &x) {
int j;
for(int i=0;i<=last;i++) {
if(data[i]==x) {
data[i]=data[i+1];
j=i+1;
break;
}
}
for(int z=j;z<=last;z++) {
data[z]=data[z+1];
}
last--;
}
template <class T>
bool SeqList<T>::getData(int i,T &x)const {
if(i>=1&&i<=last+1) {
x=data[i-1];
return true;
}
return false;
}
template <class T>
bool SeqList<T>::setData(int i,T &x){
if(i>=1&&i<=last+1) {
data[i-1]=x;
return true;
}
return false;
}
template <class T>
bool SeqList<T>::Insert(int i,T &x){
if(last+1==maxSize) return false;
if(i<0||i>last+1) return false;
int j;
for(j=last;j>=i;j--) {
data[j+1]=data[j];
}
data[i]=x;
last++;
return true;
}
template <class T>
bool SeqList<T>::Remove(int i){
if(last==-1) return false;
if(i<1||i>last+1) return false;
for(int j=i-1;j<last;j++) {
data[j]=data[j+1];
}
last--;
return true;
}
template <class T>
void SeqList<T>::Input() {
while(1) {
int x;
cin>>x;
last=x-1;
if(last>=0&&last<=maxSize-1)
break;
}
for(int j=0;j<=last;j++) {
cin>>data[j];
}
}
template <class T>
void SeqList<T>::Output() {
for(int j=0;j<=last;j++) {
cout<<" "<<data[j];
}
cout<<endl;
}
template <class T>
void SeqList<T>::Sort() {
T value;
for(int i=1;i<=last;i++) {
for(int j=0;j<=last-i;j++) {
if(data[j]>data[j+1])
{
value=data[j];
data[j]=data[j+1];
data[j+1]=value;
}
}
}
}
template <class T>
void SeqList<T>::reSize(int size) {
if(size<=0) cout<<"无效的空间大小"<<endl;
if(size>maxSize) {
T *newarray=new T[size];
if(newarray==NULL) {
cout<<"存储分配错误!"<<endl;
exit(1);
}
int j=last+1;
T *a=data;
T *b=newarray;
while(j--) {
* b++=* a++;
}
delete []data;
data=newarray;
maxSize=size;
}
}
template <class T>
void SeqList<T>::Union(SeqList<T> L){
int m=L.Length();
int n=this->Length();
int flag;
T x;
for(int i=1;i<=n;i++) {
this->getData(i,x);
flag=L.Search(x);
if(flag==-1) {
L.Insert(m,x);
m++;
}
}
cout<<"A union B is";
L.Output();
}
template<class T>
void SeqList<T>::Intersection(SeqList <T> L) {
int m=L.Length();
int flag;
T x;
int i=1;
while(i<=m) {
L.getData(i,x);
flag=this->Search(x);
if(flag==-1) {
L.Remove(i);
m--;
}
else i++;
}
cout<<"A cross B is";
L.Output();
}
template <class T>
void SeqList<T>::Union1(SeqList<T> L){
this->Sort();
L.Sort();
int m=this->Length();
int n=L.Length();
int flag;
T x;
for(int i=1;i<=n;i++) {
L.getData(i,x);
flag=this->Search(x);
if(flag==-1) {
this->Insert(m,x);
m++;
}
}
cout<<"A union B in sequence is";
this->Output();
}
int main()
{
SeqList<int> com(20);
com.Input();
cout<<"A is created as:";
com.Output();
int x,y;
cin>>x>>y;
com.Insert(x-1,y);
cout<<"After Inserted A is";
com.Output();
int z;
cin>>z;
com.delete1(z);
cout<<"After deleted A is";
com.Output();
int q;
cin>>q;
com.Search1(q);
cin>>q;
com.Search1(q);
SeqList<int> con(20);
con.Input();
com.Intersection(con);
com.Union(con);
com.Union1(con);
return 0;
}
顺序表的使用
猜你喜欢
转载自blog.csdn.net/jthello123/article/details/80297928
今日推荐
周排行