以一个现成的模板实现了线性表的顺序结构实现,VC6.0调试OK

请大家以开源的方式来完善这个算法 ,以跟贴方式来添加代码

请大家往这个下面继续添加完整的可以运行的线性表的顺序结构实现代码

/*

线性表的顺序结构实现,数组C++实现法,VC调试OK
线性表可以用顺序结构(是用数组线性表来实现)来实现,也可以用链式结构来实现。
我们以顺序结构为例:
 线性表的基本的操作:
(1)创建线性表
(2)初始化线性表  SeqList()
(3)插入元素   Insert(Type& x ,int i)
(4)删除元素   Remove(Type& x) 
(5)查找元素   
按位置查找对象  Get(int i)
按对象查找位置  Find(Type& x)
(6)计算表长度  Length();

扩展功能:
顺序表满判断  IsFull()
顺序表空判断  IsEmpty()  
输出顺序表的数据 PrintList()
输入顺序表的数据 InputList()
判断x是否在表中  IsIn(Type& x)
寻找x的后继   Next(Type& x)
寻找x的前驱   Prior(Type& x)
合并两个数组  Union(SeqList<Type>& LA,SeqList<Type>& LB)
求两个数组的交集


注意:
(1)插入元素时要把插入位置后的所有元素“从后往前”各向后移动一个位置。
(2)删除元素时要把删除位置后的所有元素“从前往后”各向前移动一个位置。

*/
//SeqList.h
#ifndef SEQLIST_H
#define SEQLIST_H

template< class Type> class SeqList
{
public: 
SeqList(int MaxSize=6); //构造函数定义和实现 
~SeqList() {delete []data; }  //析构函数定义和实现 
int Length() const {return last+1;} //计算表长度定义和实现 
Type Get(int i)            //取第i个元素的值
{ if (i<0||i>last)
  cerr<<"out of range...";
 else
 return data[i];
}
int Find(Type& x) const;   //定位函数:找x在表中的位置
int Insert(Type& x ,int i);   //插入x在表中第i个位置处 
int Remove(Type& x);    //删除x
//新添加功能
int Next(Type& x);     //寻找x的后继 
int Prior(Type& x);     //寻找x的前驱 
int IsEmpty() {return last==-1;}   //判断顺序表是否为空,空则返回1;否则返回0 
int IsFull() {return last==MaxSize-1;}   //判断顺序表满否,满则返回1;否则返回0 
int IsIn(Type& x);     //判断x是否在表中 
void Union(SeqList<Type>& LA,SeqList<Type>& LB);  //合并LA,LB,重复元素只留一下 
void Intersection(SeqList<Type>& LA,SeqList<Type>& LB); //求LA,LB中的共有元素 
void PrintList();     //输出顺序表的数据 
void InputList();     //输入顺序表的数据

private: 
Type* data;       //存放顺序表的数组 
int MaxSize;      //顺序表最大可容纳项数 
int last;       //顺序表当前是已存表项的最后位置
};
//构造函数,通过描写参数sz定义数组的长度。 
template <class Type> SeqList<Type>::SeqList(int sz)
{
 if(sz>0)
  MaxSize=sz;
 else
  MaxSize=6;
 last=MaxSize - 1;
 data=new Type[MaxSize];

}

 

//定位,找x在表中位置 ,若查找成功,函数 返回表项的位置,否则函数返回-1 
template <class Type> int SeqList<Type>::Find(Type& x) const

int i=0; 
while(i<=last&&data[i]!=x)  
i++; 
if(i>last)  
return -1; 
else  
return i;
}

//判断x是否在表中
template <class Type> int SeqList<Type>::IsIn(Type& x)

int i=0,found=0; 
while(i<==last&&!found)  
if(data[i]!=x)   
i++;  
else   
found=1; 
return found;}

//插入x在表中第i个位置处。函数返回插入是否成功的信息,若为0则插入不成功。
template <class Type> int SeqList<Type>::Insert(Type& x,int i)

if(i<0||i>last+1||last==MaxSize-1) 
return 0; 
else 
{
last++;  
for(int j=last;j>i;j--)   
data[j]=data[j-1];  
data[i]=x;  
return 1; }
}
template <class Type> int SeqList<Type>::Remove(Type& x)
{
int i=Find(x); 
if(i>=0) 
{  
last--;  
for(int j=i;j<=last;j++)   
data[j]=data[j+1];  
return 1; 

return 0;
}
//寻找x的后继数据
template <class Type> int SeqList<Type>::Next(Type& x)

if(i>=0&&i<last)  
 return i+1; 
else   
 return -1;
}
//寻找x的前驱数据
template <class Type> int SeqList<Type>::Prior(Type& x)

int i=Find(x); 
if(i>0&&i<=last)
 return i-1; 
else   
 return -1;
}

//合并顺序表LA与LB,重复元素只留一下。这个值得深入研究
template <class Type> void SeqList<Type>::Union(SeqList<Type>& LA,SeqList<Type>& LB)
{  
int n=LA.Length(); 
int m=LB.Length(); 
for(int i=0;i<m;i++) 
 {  
 Type x=LB.Get(i);  
 int k=LA.Find(x);  
 if(k==-1)  
  {   
   LA.Insert(n+1,x);   
   n++;  
  } 
 }
}
//求顺序表LA与LB中的共有元素,其实也是寻找交集元素
template <class Type> void SeqList<Type>::Intersection(SeqList<Type> &LA,SeqList<Type>& LB)

int n=LA.Length(); 
int m=LB.Length(); 
int i=0; 
while(i<n) 
 {  
 Type x=LA.Get(i);  
 int k=LB.Find(x);  
 if(-1==k)  
 {
  LA.Remove(x);   
  n--;  
 }  
 else   
  i++; 
 }
}

//自己写的输出数据方法 
template <class Type> void SeqList<Type>::PrintList()
{
int l=Length()-1; 
for(int i=0;i<l;i++)  
cout<<"list"<<i<<"= "<<data[i]<<endl;;
}
//自己写的输入数据方法 
template <class Type> void SeqList<Type>::InputList()

int l=Length()-1; 
for(int i=0;i<l;i++) 
{
cout<<"enter data["<<i<<"]:";  
cin>>data[i];  
}}
#endif

//调用程序

//seqlist.cpp
/* 测试主程序 * * - */
#include <iostream.h>
#include "SeqList.h"
//const defaultSize=8;
void main()
{
SeqList<int> Intseqlist(8);  
cout<<"Intseqlist.length:"<<Intseqlist.Length()<<endl; 
cout<<"未初始化的数据是没有规则的如下:"<<endl;  
Intseqlist.PrintList();  

cout<<"输入int到数组:"<<endl;  
Intseqlist.InputList(); 

cout<<"新的数据:"<<endl;
Intseqlist.PrintList();

//add check get(x) code
int tv;
cout<<"Get(x):enter a int:";
cin>>tv;
cout<<"get(x)="<<Intseqlist.Get(tv)<<endl;

//请各位多写测试代码,争取把上面的类的功能都用一次
//这就是一个简单的程序测试工程师的一部分工作内容

}

//欢迎大家从这个线性表的数组实现代码中更加完美

以一个现成的模板实现了线性表的顺序结构实现,VC6.0调试OK

请大家以开源的方式来完善这个算法 ,以跟贴方式来添加代码

请大家往这个下面继续添加完整的可以运行的线性表的顺序结构实现代码

/*

线性表的顺序结构实现,数组C++实现法,VC调试OK
线性表可以用顺序结构(是用数组线性表来实现)来实现,也可以用链式结构来实现。
我们以顺序结构为例:
 线性表的基本的操作:
(1)创建线性表
(2)初始化线性表  SeqList()
(3)插入元素   Insert(Type& x ,int i)
(4)删除元素   Remove(Type& x) 
(5)查找元素   
按位置查找对象  Get(int i)
按对象查找位置  Find(Type& x)
(6)计算表长度  Length();

扩展功能:
顺序表满判断  IsFull()
顺序表空判断  IsEmpty()  
输出顺序表的数据 PrintList()
输入顺序表的数据 InputList()
判断x是否在表中  IsIn(Type& x)
寻找x的后继   Next(Type& x)
寻找x的前驱   Prior(Type& x)
合并两个数组  Union(SeqList<Type>& LA,SeqList<Type>& LB)
求两个数组的交集


注意:
(1)插入元素时要把插入位置后的所有元素“从后往前”各向后移动一个位置。
(2)删除元素时要把删除位置后的所有元素“从前往后”各向前移动一个位置。

*/
//SeqList.h
#ifndef SEQLIST_H
#define SEQLIST_H

template< class Type> class SeqList
{
public: 
SeqList(int MaxSize=6); //构造函数定义和实现 
~SeqList() {delete []data; }  //析构函数定义和实现 
int Length() const {return last+1;} //计算表长度定义和实现 
Type Get(int i)            //取第i个元素的值
{ if (i<0||i>last)
  cerr<<"out of range...";
 else
 return data[i];
}
int Find(Type& x) const;   //定位函数:找x在表中的位置
int Insert(Type& x ,int i);   //插入x在表中第i个位置处 
int Remove(Type& x);    //删除x
//新添加功能
int Next(Type& x);     //寻找x的后继 
int Prior(Type& x);     //寻找x的前驱 
int IsEmpty() {return last==-1;}   //判断顺序表是否为空,空则返回1;否则返回0 
int IsFull() {return last==MaxSize-1;}   //判断顺序表满否,满则返回1;否则返回0 
int IsIn(Type& x);     //判断x是否在表中 
void Union(SeqList<Type>& LA,SeqList<Type>& LB);  //合并LA,LB,重复元素只留一下 
void Intersection(SeqList<Type>& LA,SeqList<Type>& LB); //求LA,LB中的共有元素 
void PrintList();     //输出顺序表的数据 
void InputList();     //输入顺序表的数据

private: 
Type* data;       //存放顺序表的数组 
int MaxSize;      //顺序表最大可容纳项数 
int last;       //顺序表当前是已存表项的最后位置
};
//构造函数,通过描写参数sz定义数组的长度。 
template <class Type> SeqList<Type>::SeqList(int sz)
{
 if(sz>0)
  MaxSize=sz;
 else
  MaxSize=6;
 last=MaxSize - 1;
 data=new Type[MaxSize];

}

 

//定位,找x在表中位置 ,若查找成功,函数 返回表项的位置,否则函数返回-1 
template <class Type> int SeqList<Type>::Find(Type& x) const

int i=0; 
while(i<=last&&data[i]!=x)  
i++; 
if(i>last)  
return -1; 
else  
return i;
}

//判断x是否在表中
template <class Type> int SeqList<Type>::IsIn(Type& x)

int i=0,found=0; 
while(i<==last&&!found)  
if(data[i]!=x)   
i++;  
else   
found=1; 
return found;}

//插入x在表中第i个位置处。函数返回插入是否成功的信息,若为0则插入不成功。
template <class Type> int SeqList<Type>::Insert(Type& x,int i)

if(i<0||i>last+1||last==MaxSize-1) 
return 0; 
else 
{
last++;  
for(int j=last;j>i;j--)   
data[j]=data[j-1];  
data[i]=x;  
return 1; }
}
template <class Type> int SeqList<Type>::Remove(Type& x)
{
int i=Find(x); 
if(i>=0) 
{  
last--;  
for(int j=i;j<=last;j++)   
data[j]=data[j+1];  
return 1; 

return 0;
}
//寻找x的后继数据
template <class Type> int SeqList<Type>::Next(Type& x)

if(i>=0&&i<last)  
 return i+1; 
else   
 return -1;
}
//寻找x的前驱数据
template <class Type> int SeqList<Type>::Prior(Type& x)

int i=Find(x); 
if(i>0&&i<=last)
 return i-1; 
else   
 return -1;
}

//合并顺序表LA与LB,重复元素只留一下。这个值得深入研究
template <class Type> void SeqList<Type>::Union(SeqList<Type>& LA,SeqList<Type>& LB)
{  
int n=LA.Length(); 
int m=LB.Length(); 
for(int i=0;i<m;i++) 
 {  
 Type x=LB.Get(i);  
 int k=LA.Find(x);  
 if(k==-1)  
  {   
   LA.Insert(n+1,x);   
   n++;  
  } 
 }
}
//求顺序表LA与LB中的共有元素,其实也是寻找交集元素
template <class Type> void SeqList<Type>::Intersection(SeqList<Type> &LA,SeqList<Type>& LB)

int n=LA.Length(); 
int m=LB.Length(); 
int i=0; 
while(i<n) 
 {  
 Type x=LA.Get(i);  
 int k=LB.Find(x);  
 if(-1==k)  
 {
  LA.Remove(x);   
  n--;  
 }  
 else   
  i++; 
 }
}

//自己写的输出数据方法 
template <class Type> void SeqList<Type>::PrintList()
{
int l=Length()-1; 
for(int i=0;i<l;i++)  
cout<<"list"<<i<<"= "<<data[i]<<endl;;
}
//自己写的输入数据方法 
template <class Type> void SeqList<Type>::InputList()

int l=Length()-1; 
for(int i=0;i<l;i++) 
{
cout<<"enter data["<<i<<"]:";  
cin>>data[i];  
}}
#endif

//调用程序

//seqlist.cpp
/* 测试主程序 * * - */
#include <iostream.h>
#include "SeqList.h"
//const defaultSize=8;
void main()
{
SeqList<int> Intseqlist(8);  
cout<<"Intseqlist.length:"<<Intseqlist.Length()<<endl; 
cout<<"未初始化的数据是没有规则的如下:"<<endl;  
Intseqlist.PrintList();  

cout<<"输入int到数组:"<<endl;  
Intseqlist.InputList(); 

cout<<"新的数据:"<<endl;
Intseqlist.PrintList();

//add check get(x) code
int tv;
cout<<"Get(x):enter a int:";
cin>>tv;
cout<<"get(x)="<<Intseqlist.Get(tv)<<endl;

//请各位多写测试代码,争取把上面的类的功能都用一次
//这就是一个简单的程序测试工程师的一部分工作内容

}

//欢迎大家从这个线性表的数组实现代码中更加完美