[c++]代码库
# include <iostream>
using namespace std;
const int MaxSize = 10; // 线性表的最大容量
typedef char ElemType; // 自定义类型
class SeqList
{
private:
ElemType* data;
int length;
public:
bool InitList() // 初始化顺序表空间
{
data = new ElemType[MaxSize];
if ( NULL == data )
{
cout << "Not Have Enough Memory!\n";
return false;
}
length = 0;
return true;
}
bool CreatList( int site, ElemType e ) // 创建顺序表
{
if ( site<1 || site>MaxSize )
{ // 越界检查
cout << "Overflow, ";
return false;
}
else
{
data[site-1] = e;
length ++;
return true;
}
}
void DispList() // 输出顺序表
{
for ( int i=0;i < length;++i )
cout << data[i] << ' ';
cout << endl;
}
int ListLength() { return length; } // 求表长
bool ListEmpty() { return 0 == length; } // 判断是否为空表
bool GetElem( int site, ElemType &e ) // 求某位置上的元素
{
if ( site<1 || site>length )
return false;
else
e = data[site-1];
return true;
}
bool LocateElem( int& site, ElemType e ) // 求元素的位置
{
for ( int i=0;i < length;++i )
{
if ( e == data[i] )
{
site = i+1;
return true;
}
}
return false;
}
bool ListInsert( int site, ElemType e ) // 插入元素
{
if ( site<1 || site>length )
{
cout << "Overflow, ";
return false;
}
else
{
site --;
for ( int i=length;i > site;--i )
data[i] = data[i-1];
data[site] = e;
length ++;
return true;
}
}
bool ListDelete( int site, ElemType &e ) // 删除元素
{
if ( site<1 || site>length )
{
cout << "Overflow, ";
return false;
}
else
{
e = data[site--];
for ( int i=site;i < length;++i )
data[i] = data[i+1];
length --;
return true;
}
}
void DestoryList() { delete[] data; } // 清空顺序表
};
int main (void)
{
SeqList L; // 定义顺序表
cout << ">>>>>>>>> InitList Operation <<<<<<<<\n";
L.InitList(); // 初始化顺序表
int temp;
ElemType e;
cout << "Input The SeqList Length You Want To Creat : ";
cin >> temp;
cout << "Input Element : \n";
cout << ">>>>>>>> CreatList Operation <<<<<<<\n";
for ( int i=1;i <= temp;++i )
{
printf("No.%d : ", i);
cin >> e;
if ( L.CreatList( i, e ) ) // 创建顺序表
printf("Creat No.%d element success!\n", i);
else
printf("Creat No.%d element failed!\n", i);
cin.sync(); // 清除缓冲区
}
cout << "Now SeqList is : \n";
cout << ">>>>>>>>> DispList Operation <<<<<<<<\n";
L.DispList(); // 输出顺序表
cout << ">>>>>>>> ListLength Operation <<<<<<<\n";
printf("SeqList Length Is %d.\n", L.ListLength()); // 输出表长
cout << ">>>>>>>> ListEmpty Operation <<<<<<<<\n";
if ( L.ListEmpty() ) // 判断是否是空表
cout << "SeqList Is Empty!\n";
else
cout << "Not a Empty SeqList!\n";
cout << ">>>>>>>>> GetElem Operation <<<<<<<<<\n";
cout << "Input Site To Check Element : ";
cin >> temp;
if ( L.GetElem( temp, e ) ) // 用 e 返回顺序表中第 temp 个元素
printf("No.%d Element Is %c\n.", temp, e);
else
cout << "Overflow The SeqList!\n";
cout << ">>>>>>>> LocateElem Operation <<<<<<<<\n";
cout << "Input Element To Check Site : ";
cin >> e;
if ( L.LocateElem( temp, e ) ) // 输入元素查看元素所在的位置
printf("%c Exist In No.%d.\n", e, temp);
else
printf("Can't Find %c!\n", e);
cout << ">>>>>>>> ListInsert Operation <<<<<<<<\n";
cout << "Input Site : ";
cin >> temp;
cout << "Input Element : ";
cin >> e;
if ( L.ListInsert( temp, e ) ) // 在 temp 位置插入 e
{
cout << "Insert Success!\nNow SeqList Is : ";
L.DispList();
}
else
cout << "Insert Failed!\n";
cout << ">>>>>>>> ListDelete Operation <<<<<<<<\n";
cout << "Input Delete Site : ";
cin >> temp;
if ( L.ListDelete( temp, e ) ) // 删除 temp 位置的元素
{
printf("Delete %c Success!\nNow SeqList Is : ", e);
L.DispList();
}
else
cout << "Delete Failed!\n";
L.DestoryList(); // 销毁线性表
return 0;
}
[代码运行效果截图]