2008年(35)
分类: C/C++
2008-04-03 10:02:09
/**设线性表采用顺序表示方式,并假定顺序表是有序的(设表中元素已按非递减次序排列)。
编写函数,实现对线性表的如下描述运算
(1) int Searchinsert(const T&x)
后置条件:在有序地顺序表中搜索元素x,若x在表中,则返回小x在表中的位置。否则,若表未满,则在表中插入新元素x
并且插入后,线性表仍然是有序的,返回新元素x的位置;若表已满,无法插入新元素,返回-1.
(2) void SearchDelete(const T&x,const T&y)
前置条件:x
#include
#include
using namespace std;
enum ResultCode{OVERFLOW,UnderFlow,OutOfBounds};
template
class List;
template
class SeqList;
template
ostream& operator << (ostream& out,const SeqList
template
istream& operator >> (istream& in,SeqList
template
class List
{
public:
virtual void Insert(int pos,const T&x)=0;
virtual void Remove(int pos)=0;
virtual void Retrieve(int pos, T &x) const=0;
virtual void Replace(int pos,const T& x)=0;
virtual int Length() const=0;
virtual bool IsEmpty() const=0;
virtual bool IsFull() const=0;
virtual void Clear()=0;
};
template
class SeqList:public List
{
public:
SeqList(int mSize);
~SeqList(){delete[] l;}
void Insert(int pos,const T& x);
void Remove(int pos);
void Retrieve(int pos, T& x)const;
void Replace(int pos,const T& x);
int SearchInsert(const T& x);
void SearchDelete(const T& x,const T& y);
int Length() const{return n;}
bool IsEmpty() const{return n==0;}
bool IsFull()const{return n==maxSize;}
void Clear(){n=0;}
protected:
void Output(ostream& out)const;
T *l;
int maxSize;
int n;
friend ostream& operator<<<>(ostream& out,const SeqList
friend istream& operator>><>(istream& in,SeqList
};
template
SeqList
{
maxSize=mSize;
l=new T[maxSize];
n=0;
}
template
void SeqList
{
if(IsFull()) throw OVERFLOW;
if(pos<0||pos>n) throw OutOfBounds;
for(int i=n;i>=pos;i--)
l[i+1]=l[i];
l[pos]=x;
++n;
}
template
void SeqList
{
if(IsEmpty()) throw UnderFlow;
if(pos<0||pos>n) throw OutOfBounds;
for(int i=pos;i<=n-1;i++)
l[i]=l[i+1];
--n;
}
template
void SeqList
{
if(IsEmpty()) throw UnderFlow;
if(pos<0||pos>=n) throw OutOfBounds;
x=l[pos];
}
template
void SeqList
{
if(IsEmpty()) throw UnderFlow;
if(pos<0||pos>=n) throw OutOfBounds;
l[pos]=x;
}
template
int SeqList
{
int i=1;
for(;x>=l[i];i++)
{
if(x==l[i])
return i;
}
Insert(i,x);
return -1;
}
template
void SeqList
{
int i=1;
int j=1;
if(x>y) return;
else
{
while(x>l[i])
++i;
cout<while(y>l[j])
++j;
cout<
{
l[i+x]=l[j+x+1];
}
}
n=n-(j-i+1);
}
template
void SeqList
{
if(IsEmpty())
out<<"Empty"<
{
out<<"The SqlList constains:"<
}
template
ostream& operator<<(ostream& out,const SeqList
{
x.Output(out);
return out;
}
template
istream& operator>>(istream& in,SeqList
{
cout<<"please input the member of list:"<
int i=1;
while(c!='\n')
{
cin>>x.l[i];
++i;
c=getchar();
}
x.n=i-1;
return in;
}
int main()
{
SeqList
cin>>x;
cout<
cout<
return 0;
}