关于栈的顺序存储问题
关于栈的顺序存储问题
[此贴子已经被作者于2005-12-13 17:35:57编辑过]

#include<iostream.h>
template<class T>
class L
{
private :
T *data;
int msize, currlen;
public:
L(int size=10);
~L(){ delete[] data;}
int Find(T x);
bool empty();
Insert(T x,int i);
remove(T x,int i);
void print();
};
template<class T>
L<T>::L(int size)
{
if(size<=0)
cout<<"waring:输入错误,顺序表元素个数不能小于零"<<endl;
msize=size;
currlen=0;
data=new T [msize];
}
template<class T>
bool L<T>::empty()
{
if(currlen==0)
return 0;
else return 1;
}
template<class T>
int L<T>::Find(T x)
{
if (!empty())
cout<<"waring:这是一个空表,请输入数据"<<endl;
for(int i=1;i<=currlen&&data[i-1]!=x;i++);
cout<<"查找成功,very good!"<<endl;
return i;
cout<<"sorry,找不到你给的数据!!"<<endl;
return 0;
}
template<class T>
L<T>::Insert(T x,int i)
{
if(i<=0||currlen==msize)
cout<<"error:输入错误或空间已满,不能这样的啦!"<<endl;
else
{
for(int j=currlen;j>i;j--)
data[j]=data[j-1];
data[i-1]=x;
currlen++;}
}
template<class T>
L<T>::remove(T x,int i)
{
if(i<0||i>currlen)
cout<<"error:输入错误或不能这样的啦!"<<endl;
int j=Find(x);
if(j>0)
{
for(j;j<=currlen;j++)
data[j-1]=data[j];
currlen--;
cout<<"\""<<x<<"\""<<"已删除"<<endl;
}
}
template<class T>
void L<T>::print()
{int i=1;
for(i;i<=currlen;i++)
cout<<data[i-1]<<" "<<endl;
}
void main()
{
L<int> a(8);
a.Insert(1,1);
a.Insert(2,2);
a.Insert(3,3);
a.Insert(4,4);
a.Insert(5,5);
a.Insert(6,6);
a.Insert(7,7);
a.Insert(8,8);
a.print();
int i=a.Find(3);
cout<<i<<endl;
a.remove(3,3);
cout<<"删除后的顺序表 :"<<endl;
a.print();
}









