定义一个顺序表SpList类,及其相应的操作
#include <iostream>
using namespace std;
const int MAXSIZE=100;
class SqList
{
private:
int elem[MAXSIZE];
int length;
public:
SqList();
~SqList();
void Insert(int i, int x);
void Delete(int i);
void Print();
void MySort();//按非递减排序
void NiZhi();//逆置
void NoSame();//将表中元素相同的多余元素删除
};
SqList::SqList()
{
for (int i=0;i<5;i++)
{
elem[i]=i;
}
length=5;
}
SqList::~SqList()
{
}
void SqList::Insert(int i, int x)
{
i--;
for (int j=length;j>i;j--)
{
elem[j]=elem[j-1];
}
elem[i]=x;
length++;
}
void SqList::Delete(int i)
{
i--;
for (int j=i;j<length;j++)
{
elem[j]=elem[j+1];
}
length--;
}
void SqList::Print()
{
for (int j=0;j<length;j++)
{
cout<<elem[j]<<" ";
}
cout<<endl;
}
void SqList::MySort()
{
for (int i=0;i<length;i++)
{
for (int j=i;j<length;j++)
{
if (elem[j]>elem[i])
{
int temp;
temp=elem[i];
elem[i]=elem[j];
elem[j]=temp;
}
}
}
}
void SqList::NiZhi()
{
for (int i=0;i<length/2;i++)
{
int temp;
temp=elem[length-i-1];
elem[length-1-i]=elem[i];
elem[i]=temp;
}
}
void SqList::NoSame()
{
for (int i=0;i<length;i++)
{
while(i+1<length && elem[i]==elem[i+1])
{
for (int j=i+1;j<length-1;j++)
{
elem[j]=elem[j+1];
}
length--;
}
}
}
int main()
{
SqList list;
list.Insert(3,5);
list.Print();
list.Delete(4);
list.Print();
list.MySort();
list.Print();
list.NiZhi();
list.Print();
list.NoSame();
list.Print();
return 0;
}