#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#define MAXSIZE 100
using namespace std;
typedef int ElemType;
typedef struct{
ElemType *elem;//存储空间基地址
int length;//当前纵列
}SqList;//顺序存储结构类型
int choice;
int select()
{
cout << "1----初始化一个线性表" << endl;
cout << "2----销毁线性表" << endl;
cout << "3----清空线性表" << endl;
cout << "4----判断线性表是否为空" << endl;
cout << "5----求线性表长度" << endl;
cout << "6----获取线性表指定位置元素" << endl;
cout << "7----求前驱" << endl;
cout << "8----求后驱" << endl;
cout << "9----在线性表指定位置插入元素" << endl;
cout << "10---删除线性表指定位置元素" << endl;
cout << "11----显示线性表" << endl;
cout << "\t退出,输入一个负数!" << endl;
cout << "请输入操作代码:" ;
cin>>choice;
cout<<endl;
return choice;
}
void InitList_Sq(SqList &L)//1.初始化线性表
{//构造一个空的线性表
L.elem = new ElemType[MAXSIZE];
if ( !L.elem )
cout<<"ERROR!!!!!!1"<<endl;;//存储空间失败
L.length =0;
cout<<"OK!!!"<<endl;
}
void DestroyList(SqList &L)//2.销毁线性表
{
if(L.elem) delete[] L.elem;
L.length = 0;
L.elem = NULL;
}
void ClearList(SqList &L)//3.清空线性表
{
L.length=0;
}
bool IsEmpty(SqList L)//4.判断是否为空
{
if (L.length == 0) return true;
else return false;
}
int GetLength(SqList L)//5.求线性表长度
{
//cout<<"";
return L.length;
}
int GetElem(SqList L,ElemType i)//6.获取线性表指定位置元素
{
//判断i值是否合理
if (i<1||i>L.length) cout<<"ERROR!!!"<<endl;
else cout<<L.elem[i-1]<<endl;
}
int LocateElem(SqList L,ElemType e)//7.8.查找e元素的位置
{
for (int i=0;i<L.length;i++)
if(L.elem[i]==e) return i+1;
return 0;
}
int ListInsert_Sq(SqList &L,int i ,ElemType e){//9.插入元素
if(L.length==MAXSIZE) cout<<"ERROR!!!"<<endl;
//当前存储空间已满
for(int j=L.length-1;j>=i-1;j--)
L.elem[j+1]=L.elem[j]; //插入位置及之后的元素后移
L.elem[i-1]=e; //将新元素e放入第i个位置
++L.length; //表长增1
cout<<"OK!!!"<<endl;
}
int ListDelete_Sq(SqList &L,int i){//10.删除第i个元素
if((i<1)||(i>L.length)) cout<<"ERROR!!!"<<endl; //i值不合法
int m;
for (int j=i;j<=L.length-1;j++)
{ m=L.elem[j];
L.elem[j-1]=m; //被删除元素之后的元素前移
}
--L.length;
cout<<"OK!!!"<<endl;
}
void JudgeChoice(SqList &L,int choice)
{
int i=0 ;
ElemType e;
//cout<<"------------------------";
switch(choice){
case 1:InitList_Sq(L);break;
case 2:DestroyList(L);break;
case 3:ClearList(L);break;
case 4:if(IsEmpty(L) == true) cout<<"为空。"<<endl;
else cout<<"不为空。"<<endl ;
break;
case 5:cout<<"长度:"<<GetLength(L)<<endl;break;
case 6:cout <<"请输入你想找的元素的位置:";
cin>>e;
GetElem(L,e);break;
case 7:cout <<"请输入你想求前驱的元素:";
cin>>e;
i=LocateElem(L,e);
if(0==i) cout<<"没有 "<<e<<" 该元素!"<<endl;
else {if(1==i) cout<<e<<" 该元素,没有前驱!"<<endl;
else cout<<L.elem[i-2]<<endl;}
break;
case 8:cout <<"请输入你想求后驱的元素:";
cin>>e;
i=LocateElem(L,e);
if(0==i) cout<<"没有 "<<e<<" 该元素!"<<endl;
else {if(L.length==i) cout<<e<<" 该元素,没有后驱!"<<endl;
else cout<<L.elem[i]<<endl;}
break;
case 9:cout <<"请输入你想插入的元素的位置:";
cin>>i;
if(i<1||i>L.length+1) cout<<"输入非法!!"<<endl;
else {cout <<"请输入你想插入的元素:";
cin>>e;
ListInsert_Sq(L,i ,e);}
break;
case 10:cout <<"请输入你想删除的元素的位置:";
cin>>i;
if((i<1)||(i>L.length)) cout<<"ERROR!!!"<<endl; //i值不合法
else ListDelete_Sq(L,i);
break;
case 11:for( i=0; i<L.length ;i++)
cout<<"*"<<L.elem[i]<<endl; break;
default: break;
}
if(choice==0)
{
cout << "输入非法,请重新输入!!!" <<endl;
}
}
int main()
{
choice=select();
SqList L;
L.elem = (ElemType*) malloc(sizeof(MAXSIZE));
L.length = 10;
for(int j=0;j<10;j++)
{
L.elem[j]=j+1;
}
while(choice>=0)
{
JudgeChoice(L,choice);
cout<<endl;
choice=select();
cout<<endl;
}
cout << "Over!!!" <<endl;
return 0;
}
顺序表的基本操作
于 2023-03-26 16:59:33 首次发布