数据结构实验课(顺序表中的基本操作和实现)
2019-09-17 08:32 木木王韦 阅读(505) 评论(0) 收藏 举报通过线性表的顺序表示和实现,通过顺序表完成学生信息的存储和修改工作。
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
#define OK 1
#define ERROR 0
#define OVERFLOW -1
#define MAXSIZE 1010//顺序表可达到的最大长度
typedef char status;//定义类型
typedef struct{
int num;
string name;
int score;
}elemtype;
typedef struct{//顺序表的储存结构
elemtype *elem;//储存空间的基地址
int length;//当前长度 }SqList;//顺序表的结构类型为SqList
//初始化·
int init(SqList &L)
{//构造一个空的顺序表L
L.elem=new elemtype[MAXSIZE];//为顺序表分配一个大小为MAXSIZE的数组空间
if(!L.elem) exit(OVERFLOW);//储存分配失败,退出
L.length=0;//空表长度为零
return OK;
}
//取值
int GetElem(SqList L,int i,elemtype &e)
{
if(i<1||i>L.length) return ERROR;//判断i值是否合理,若不合理,返回ERROR
e=L.elem[i-1];//elem[i-1]单元储存第i个数据元素
return OK;
}
//查找
int LocteElem(SqList L,int e)
{//在顺序表L中查找值为e的数据元素,返回其序号
for(int i=0;i<L.length;i++)
{
if(L.elem[i].score==e) return i+1;//查找成功,返回序号i+1
}
return ERROR;//查找失败,返回ERROR
}
//插入
int ListInsert(SqList &L,int i,elemtype e)
{//在顺序表L中第i个位置插入新的元素e,i值的合法范围是1<=i<=L.length+1
if((i<1)||(i>L.length+1)) return ERROR;//i值不合法
if(L.length==MAXSIZE) return ERROR;//当前储存空间已满
for(int j=L.length-1;j>=i-1;j--)
{
L.elem[j+1]=L.elem[j];//插入位置及之后的元素后移
L.elem[j+1].num++;
}
L.elem[i-1]=e;
++L.length;
return OK;
}
//删除
int ListDelete(SqList &L,int i){//在顺序表L中删除第i个元素,i值的合法范围是1<=i<=L.length
if((i<1)||(i>L.length)) return ERROR;//i值不合法
for(int j=i;j<=L.length-1;j++)
{
L.elem[j]=L.elem[j+1];//被删除元素之后的元素前移
L.elem[j].num--;
}
--L.length;
return OK;
}
int main()
{
int n,m;//输入信息学生的人数和操作的序号
cout<<"/****************Please enter the number of the operation to be performed******************/"<<endl;//请输入要进行的操作的编号
cout<<"1->Establish"<<endl;//建立
cout<<"2->Input"<<endl;//输入
cout<<"3->Get elem"<<endl;//取值
cout<<"4->Search"<<endl;//查找
cout<<"5->Insert"<<endl;//插入
cout<<"6->Delete"<<endl;//删除
cout<<"7->Output"<<endl;//输出
cout<<"0->Exit\n"<<endl;//退出
while(1)
{
elemtype g;
SqList L;
cout<<"Please enter the number of the operation to be performed:"<<endl;
cin>>m;
if(!m)
break;
if(m==1)
{
if(init(L))
cout<<"You've succeeded in creating a sequence table!"<<endl;
else
cout<<"You didn't succeed in creating a sequence table!"<<endl;
}
if(m==2)
{
cout<<"please input the number of student:";
cin>>n;
cout<<"please input the massage of the student:"<<endl;
string na;
int score;
L.elem=new elemtype[MAXSIZE];
for(int i=0;i<n;i++)
{
cin>>na>>score;
L.elem[i].num=i+1;
L.elem[i].name=na;
L.elem[i].score=score;
L.length++;
}
}
if(m==3)
{
int ID;
cout<<"Please enter the location of the values in the sequence table:";
cin>>ID;
if(GetElem(L,ID,g))
{
cout<<"The search was successful!"<<endl;
cout<<"The element at position "<<ID<<" in the sequence student is "<<g.name<<endl;
}
else
cout<<"Search failed, location out of range!"<<endl;
}
if(m==4)
{
cout<<"Please enter the score you want to find:"<<endl;
int x;
cin>>x;
if(LocteElem(L,x))
cout<<"The score you're looking for exists. The student number is "<<LocteElem(L,x)<<"."<<endl;
else
cout<<"The score you are looking for does not exist. "<<endl;
}
if(m==5)
{
cout<<"Please enter the location and information you need to insert:";
int i,score;
string name;
cin>>i>>name>>score;
g.name=name;
g.num=i;
g.score=score;
if(ListInsert(L,i,g))
cout<<"The insertion was successful!"<<endl;
else
cout<<"Insertion failed!"<<endl;
}
if(m==6)
{
cout<<"Please enter the location to delete:";
int i;
cin>>i;
if(ListDelete(L,i-1))
cout<<"Delete successfully!"<<endl;
else
cout<<"Delete failed!"<<endl;
}
if(m==7)
{
cout<<"Current Sequence Table Readout:"<<endl;
for(int i=0;i<L.length;i++)
cout<<"the number:"<<L.elem[i].num<<" the name of student:"<<L.elem[i].name<<" the score of student:"<<L.elem[i].score<<endl;
}
}
return 0;
}