#include<iostream>
#include<stdio.h>
#define LIST_INIT_SIZE 100
#define LIST_INCREMENT 10
using namespace std;
struct Sqlist{
long *elem, *newlist;
int Length;
int listsize;
};
void Error(char *s)
{
cout << s << endl;
exit(1);
}
void InitList_Sq(Sqlist &L) //创建含有若干个数据元素的顺序表
{
L.elem = new long[LIST_INIT_SIZE];
if (!L.elem)
Error("Overflow!");
L.Length = 0;
L.listsize = LIST_INIT_SIZE;
}
void Create_Sq(Sqlist *L)
{
int i, num;
cout << "请输入顺序表元素个数:";
cin >> num;
cout << "请输入数据元素:";
for (i = 0; i<num; i++)
{
cin >> L->elem[i];
L->Length++;
}
cout << "创建顺序表成功:!" << endl;;
}
void Increment(Sqlist &L) //为顺序表扩展LIST_INCREMEN个数据元素的空间
{
L.newlist = new long[L.listsize + LIST_INCREMENT];//增加LIST_INCREMENT个存储空间
if (!L.newlist)
Error("Overflow!");//存储分配失败
for (int i = 0; i < L.Length; i++) //腾挪原空间中的数据元素到新的数据空间中
{
L.newlist[i] = L.elem[i];
}//释放元素所占用的原空间
L.elem = L.newlist;//移交空间首地址;//移交空间首地址
delete[] L.elem;
L.listsize += LIST_INCREMENT; //修改当前顺序表的最大空间
}
void ListInsert_Sq(Sqlist &L, int i,int e)
//在顺序表L中第i个位置前插入元素e;若插入位置不合理则给出相关信息并退出运行,
//i的合理范围是1<=i<=L.length+1
{
if ((i<1) || (i>L.Length + 1)) //插入元素的参数不合理
Error("Position Error!");
if (L.Length >= LIST_INIT_SIZE) //若当前存储空间已满,则增加空间
Increment(L); //增加空间函数
long *q = &(L.elem[i - 1]); //令指针q指向插入位置
long *p = &(L.elem[L.Length - 1]);
for (p; p >= q; p--) //以此向后移动元素
{
*(p + 1) = *p;
}
*q = e; //在L的第i个位置插入元素e
L.Length++; //修改当前顺序表的长度
}
void ListDelete_Sq(Sqlist &L, int i, int &e)
//删除顺序表中第i个元素并用e返回其值
{
if (i<1 || i>L.Length) //删除元素的参数不合理
Error("Position Error!");
e = L.elem[i - 1]; //将待删除的元素的值赋给e
long *p = &(L.elem[i - 1]); //指向待删处元素的位置
for (++p; p <= (L.elem + L.Length - 1); p++)//以此向前移动元素
{
*(p - 1) = *p;
}
L.Length--;
cout << "删除的元素是:";
cout << e << endl;
}//修改L的长度
int LocatElem_Sq(Sqlist L, int e)
{
int j = 1;
long *q = L.elem;
while ((j <= L.Length) && (*q!= e))
{
j++;
q++;
}
if (j < L.Length)
return j;
else
return 0;
}
void getElem_Sq(Sqlist L, int i, int &e) //输出顺序表中要操作的那个元素
{
if ((i<1) || (i>L.Length)) //判断输出要求参数是否合理
Error("Position Error!");
e = L.elem[i - 1]; //用e返回顺序表中第i个元素值
cout << e << endl;
}
void printElem_Sq(Sqlist L) //输出顺序表中现有的所有元素
{
cout << " 顺序表中的元素如下:" << endl;
int i;
for (i = 0; i< L.Length; i++)
{
cout <<" "<< L.elem[i];
}
cout << endl;
}
int main()
{
Sqlist L;
int e,n,number;
while (1)
{
cout << " 1、创建信息表" << endl;
cout << " 2、插入元素" << endl;
cout << " 3、查询元素" << endl;
cout << " 4、删除元素" << endl;
cout << " 5、退出程序" << endl;
cout << " 请选择所要执行的操作:";
cin >> n;
switch (n)
{
case 1:
InitList_Sq(L);
Create_Sq(&L);
printElem_Sq(L);
break;
case 2:
cout << "请输入插入的位置和元素:";
cin >> number >> e;
while (e != 0)
{
ListInsert_Sq(L, number, e);
printElem_Sq(L);
cout << "请输入插入的位置和元素:";
cin >> number >> e;
}
break;
case 3:
cout << "请输入查找的元素:";
cin >> e;
while (e!=0)
{
LocatElem_Sq(L, e);
cout << "该元素所在顺序表的位置位置是:" << LocatElem_Sq(L, e) << endl;
cout << "该元素是:";
getElem_Sq(L, LocatElem_Sq(L, e), e);
cout << "请输入查找的元素:";
cin >> e;
}
break;
case 4:
cout << "请输入要删除的位置 :";
cin >> number;
ListDelete_Sq(L, number, e);
printElem_Sq(L);
break;//程序结束
case 5:
exit(1);
break;//程序结束
default:
cout << "输入错误,请重新输入!!!!!" << endl;
continue;
}
}
return 0;
}