顺序表

#include
using namespace std;
typedef  int  DataType;
#define  MaxListSize 10


class SeqList
{
public:
SeqList(void);
~SeqList(void);
private:
DataType data[MaxListSize];
int size;
public:
//////////////////////////////////////////////////////////////////////////
int ListSize(void) const;
int ListEmpty(void) const;
int Find(DataType &item) const;
DataType GetData(int pos) const;
void Insert(const DataType&item,int pos);
DataType Delete(const int pos);
void ClearList(void);
void InsertEnd(const DataType &item);
void DeleteEnd(const int pos);

};
SeqList::SeqList(void):size(0){}
SeqList::~SeqList(void){}
SeqList::ListSize(void) const
{
return size;
}
int SeqList::ListEmpty(void)const
{
if (size == 0)
{
return 1;
}
else
return 0;
}
int SeqList::Find(DataType &item) const
{
if (size == 0)
{
return -1;
}
int i = 0;
while (i {
i++;
}
if (i {
return i;
}
else return -1;
}
DataType SeqList::GetData(int pos) const
{
if (pos<0||pos>size-1)
{
cout<<"参数pos越界了!"< exit(0);
}
return data[pos];
}


void SeqList::Insert(const DataType &item,int pos)
{
int i;
if (size == MaxListSize)
{
cout<<"顺序表已满无法插入!"< exit(0);
}
if (pos<0||pos>size)
{
cout<<"data out of num!"< exit(0);
}
for (i=size;i>pos;i--)
{
data[i] = data[i-1];
}
data[pos] = item;
size++;
}


DataType SeqList::Delete(const int pos)
{
if (size == 0)
{
cout<<"顺序表已满!"< exit(0);
}
if (pos<0||pos>size-1)
{
cout<<""<<" ";
exit(0);
}
DataType temp = data[pos];
for (int i=pos;i {
data[i] = data[i+1];
}
size--;
return temp;


}
void SeqList::ClearList(void)
{
size = 0;
}
void SeqList::InsertEnd(const DataType &item)
{
if (size == MaxListSize)
{
cout<<"data is full!"< exit(0);
}
data[size] = item;
size++;
}
void SeqList::DeleteEnd(const int pos)
{
if (pos>size||pos<0)
{
cout<<"list is out!"< exit(0);
}
data[pos] = data[size-1];
size--;


}




void main()
{
SeqList list;
int i;
for (i=0;i<5;i++)
{
list.Insert(i+10,i);
}


int nendnum = 100;
list.InsertEnd(nendnum);
list.InsertEnd(nendnum);
for (i=0;i {
cout< }
cout< list.DeleteEnd(2);
for (i=0;i {
cout< }


// for (i=0;i<5;i++)
// {
// cout< // }




}

你可能感兴趣的:(insert,delete,include,class)