以一個現成的模板實現了線性表的順序結構實現,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;
//請各位多寫測試代碼,爭取把上面的類的功能都用一次
//這就是一個簡單的程序測試工程師的一部分工作內容
}
//歡迎大家從這個線性表的數組實現代碼中更加完美