[数据结构] 顺序表
作者:互联网
//#include <bits/stdc++.h>
#include <iostream>
#include <string>
#define MAXSIZE 100
#define OK 1;
#define ERROR 0;
using namespace std;
typedef int Status;
typedef struct{
string name;
string gender;
int age;
}Student;
typedef struct{
Student * elem;
int length;
}SqList;
void menu(){
cout<<"************************************************"<<endl;
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<<"* 退出,请输入除以上操作指令的任意数"<<endl;
cout<<"************************************************"<<endl;
}
Status InitList(SqList & L){
L.elem=new Student[MAXSIZE];
if(!L.elem){
cout<<"初始化失败"<<endl;
return ERROR;
}
L.length=0;
return OK;
}
void DestroyList(SqList & L){
if(L.elem){
delete[] L.elem;
}
L.length=0;
L.elem=NULL;
}
void ClearList(SqList & L){
L.length=0;
}
bool IsEmpty(SqList & L){
if(L.length==0){
return true;
}
return false;
}
int GetLen(SqList & L){
return L.length;
}
Status GetElem(SqList&L,int i,Student & stu){
if(i<1||i>L.length){
cout<<"查询位置有误"<<endl;
return ERROR;
}
else{
stu=L.elem[i-1];
return OK;
}
}
Status PreElem(SqList&L,int i,Student & stu){
if(i==1){
cout<<"线性表第一个位置没有前驱"<<endl;
return ERROR;
}
else if(i<1||i>L.length){
cout<<"查询位置有误"<<endl;
return ERROR;
}
else{
stu=L.elem[i-2];
return OK;
}
}
Status NextElem(SqList&L,int i,Student & stu){
if(i==L.length){
cout<<"线性表最后一个位置没有后继"<<endl;
return ERROR;
}
else if(i<1||i>L.length){
cout<<"查询位置有误"<<endl;
return ERROR;
}
else{
stu=L.elem[i];
return OK;
}
}
Status ListInsert(SqList&L,int i,Student stu){
if((i<1) || (i>L.length+1)){
cout<<"插入位置有误"<<endl;
return ERROR;
}
else if(L.length==MAXSIZE){
cout<<"存储空间已满"<<endl;
return ERROR;
}
else{
for(int j=L.length-1;j>=i-1;j--){
L.elem[j+1]=L.elem[j];
}
L.elem[i-1]=stu;
L.length++;
return OK;
}
}
Status ListDelete(SqList&L,int i){
if((i<1)||(i>L.length)){
cout<<"删除位置有误"<<endl;
return ERROR;
}
else{
for(int j=i;j<=L.length-1;j++){
L.elem[j-1]=L.elem[j];
}
L.length--;
return OK;
}
}
void ShowList(SqList&L){
cout<<endl;
cout<<"姓名\t性别\t年龄"<<endl;
for(int i=0;i<L.length;i++){
cout<<L.elem[i].name<<"\t"<<L.elem[i].gender<<"\t"<<L.elem[i].age<<endl;
}
}
int main()
{
while(true){
menu();
cout<<"请输入操作代码:";
int n;
cin>>n;
SqList L;
switch(n){
case 1:
{
InitList(L);
break;
}
case 2:
{
DestroyList(L);
break;
}
case 3:
{
ClearList(L);
break;
}
case 4:
{
if(IsEmpty(L)){
cout<<"线性表为空"<<endl;
}
else{
cout<<"线性表不为空"<<endl;
}
break;
}
case 5:
{
int len=GetLen(L);
cout<<"线性表长度为"<<len<<endl;
break;
}
case 6:
{
int index;
cout<<"请输入你想查看线性表的位置:";
cin>>index;
Student stu;
if(GetElem(L,index,stu)){
cout<<stu.name<<"\t"<<stu.gender<<"\t"<<stu.age<<endl;
}
break;
}
case 7:
{
int index;
cout<<"请输入你想查看线性表哪个位置的前驱:";
cin>>index;
Student stu;
if(PreElem(L,index,stu)){
cout<<stu.name<<"\t"<<stu.gender<<"\t"<<stu.age<<endl;
}
break;
}
case 8:
{
int index;
cout<<"请输入你想查看线性表哪个位置的后继:";
cin>>index;
Student stu;
if(NextElem(L,index,stu)){
cout<<stu.name<<"\t"<<stu.gender<<"\t"<<stu.age<<endl;
}
break;
}
case 9:
{
int index;
cout<<"请输入你想在线性表哪个位置插入元素:";
cin>>index;
string name;
string gender;
int age;
cout<<"请输入学生姓名:";
cin>>name;
cout<<"请输入学生性别(男或女):";
cin>>gender;
cout<<"请输入学生年龄:";
cin>>age;
Student stu;
stu.name=name;
stu.gender=gender;
stu.age=age;
int sta=ListInsert(L,index,stu);
break;
}
case 10:
{
int index;
cout<<"请输入你想删除线性表哪个位置的元素:";
cin>>index;
int sta=ListDelete(L,index);
break;
}
case 11:
{
ShowList(L);
break;
}
default:
return 0;
}
cout<<endl;
}
return 0;
}
如果有错误,还望指正
标签:index,顺序,cout,int,elem,stu,length,数据结构 来源: https://blog.csdn.net/qq_55675216/article/details/123647408