Chinaunix首页 | 论坛 | 博客
  • 博客访问: 213083
  • 博文数量: 35
  • 博客积分: 1480
  • 博客等级: 上尉
  • 技术积分: 390
  • 用 户 组: 普通用户
  • 注册时间: 2007-11-14 14:27
文章分类

全部博文(35)

文章存档

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后置条件:删除有序表中,元素值在x和y之间(含x和y)的所有元素。*/
#include
#include
using namespace std;
enum ResultCode{OVERFLOW,UnderFlow,OutOfBounds};
template
class List;
template
class SeqList;
template
ostream& operator << (ostream& out,const SeqList& x);
template
istream& operator >> (istream& in,SeqList& x);   
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& x);
  friend istream& operator>><>(istream& in,SeqList& x);
};
template
SeqList::SeqList(int mSize)
{
 maxSize=mSize;
 l=new T[maxSize];
 n=0;
}
template
void SeqList::Insert(int pos,const T& x)
{
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::Remove(int pos)
{
 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::Retrieve(int pos, T&x)const
{
 if(IsEmpty()) throw UnderFlow;
 if(pos<0||pos>=n) throw OutOfBounds;
 x=l[pos];
}
template
void SeqList::Replace(int pos, const T&x)
{
 if(IsEmpty()) throw UnderFlow;
 if(pos<0||pos>=n) throw OutOfBounds;
 l[pos]=x;
}
template
int SeqList::SearchInsert(const T& x)
{
 int i=1;
for(;x>=l[i];i++)
{
   if(x==l[i])
   return i;
}
  Insert(i,x);
   return -1;
   
}
template
void  SeqList::SearchDelete(const T& x,const T& y)
{
int i=1;
int j=1;
if(x>y) return;
else
{
while(x>l[i])
++i;
cout<while(y>l[j])
++j;
cout<for(int x=0;x<(j-i+1);x++)
{
 l[i+x]=l[j+x+1];
}
}
n=n-(j-i+1);

}
template
void SeqList::Output(ostream& out)const
{
if(IsEmpty())
out<<"Empty"<else
{
out<<"The SqlList constains:"<for(int i=1;iout<out<}
}
template
ostream& operator<<(ostream& out,const SeqList& x)
{
 x.Output(out);
 return out;
}
template
istream& operator>>(istream& in,SeqList& x)
{
 
 cout<<"please input the member of list:"< char c=0;
 int i=1;
 while(c!='\n')
 {
  cin>>x.l[i];
  ++i;
  c=getchar();
 }
 x.n=i-1;
return in;
}

int main()
{
SeqList x(20);
cin>>x;
cout<x.SearchDelete(4,6);
cout<system("pause");
return 0;
}

阅读(2008) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~