定义一个顺序表结构的学生成绩表结构体
typedef struct{
int score[MAXSIZE]; //学生成绩
int *elem; //存储空间的基地址
int length; //学生成绩表中当前记录个数
}Stu;
本文解决以下编程问题:
1、编写函数,创建一个顺序表(数据自拟);
2、编写函数,在顺序表的指定位置插入一个元素;
3、编写函数,在顺序表的指定位置删除一个元素;
4、编写函数,将两个有序顺序表合并成一个新的有序顺序表;
一、各函数具体代码如下:
写在前面:由于题目作答无法将顺序表内的元素显示在控制台上,故预先造两个函数——
int InitList(Stu &L)、 void PrintList(Stu &L);
int InitList(Stu &L) //初始化数据表
{
L.elem=new int[MAXSIZE];
if(!L.elem) exit(OVERFLOW);
L.length=0;
return OK;
}
void PrintList(Stu &L) //打印当前数据
{
cout<<"当前顺序表所有元素:";
for (int i = 0; i<L.length; i++)
{
cout<<L.score[i]<<" ";
}
cout<<endl;
}
编写函数,创建一个顺序表(数据自拟);
void CreateList(Stu &L,int n)
{
if(n<1||n>MAXSIZE) cout<<"创建失败!请输入合法长度!"<<endl;
cout<<"长度合法,请输入各项数据!"<<endl;
for(int i=0;i<n;i++)
{
cin>>L.score[i];
L.length++;
}
cout<<"创建成功!"<<endl;
PrintList(L);
}
编写函数,在顺序表的指定位置插入一个元素;
void ListInsert(Stu &L) //在顺序表的指定位置插入一个元素
{
int i,newscore;
cout<<"请输入插入位置:";
cin>>i;
while((i<1)||(i>L.length+1)) //判断插入位置是否合法
{
cout<<"插入位置无效!请重新输入插入位置:";
cin>>i;
}
if(L.length==MAXSIZE) //判断存储空间是否已满
{
cout<<"已达到最大存储空间,无法继续执行插入操作!"<<endl;
}
cout<<"请输入所要插入的元素:";
cin>>newscore;
for(int j=L.length;j>=i;j--)
L.score[j]=L.score[j-1]; //所要插入的位置及其之后的元素逆着后移一位
L.score[i-1]=newscore; //将要插入的数据e放入第i个位置
++L.length; //表长+1
cout<<"插入成功!!!"<<endl;
PrintList(L);
}
编写函数,在顺序表的指定位置删除一个元素;
void ListDelete(Stu &L) //在顺序表的指定位置删除一个元素
{
int i;
cout<<"请输入删除位置:";
cin>>i;
while((i<1)||(i>L.length)) //判断删除位置是否合法
{
cout<<"删除位置无效!请重新输入删除位置:";
cin>>i;
}
for(int j=i;j<=L.length-1;j++)
L.score[j-1]=L.score[j]; //被删除元素之后的元素顺着前移一位
--L.length; //表长-1
cout<<"删除成功!!!"<<endl;
PrintList(L);
}
编写函数,将两个有序顺序表合并成一个新的有序顺序表;
void MergeList_Stu(Stu &L,Stu &M) //将两个有序顺序表合并成一个新的有序顺序表
{
int i,j,temp;
Stu W;
InitList(W);
W.length=L.length+M.length; //新表长度为待合并两表长度之和
for(i=0;i<L.length;i++) //依次将两表中的数据录入新表中
{
W.score[i]=L.score[i];
}
i=0;
for(int j=L.length;j<W.length;j++)
{
W.score[j]=M.score[i];
i++;
}
//以下用选择排序对新表进行排序
for(i=0;i<W.length-1;i++)
{
for(j=i+1;j<W.length;j++)
{
if(W.score[i]>W.score[j])
{
temp=W.score[i];
W.score[i]=W.score[j];
W.score[j]=temp;
}
}
}
PrintList(W);
}
二、完整代码(含各函数和main)
#include <iostream.h>
#define MAXSIZE 100
#define ERROR 0
#define OK 1
#define OVERFLOW -2
typedef struct{
int score[MAXSIZE]; //学生成绩
int *elem; //存储空间的基地址
int length; //学生成绩表中当前记录个数
}Stu;
int InitList(Stu &L); //初始化数据表
void PrintList(Stu &L); //实现功能后,打印出当前数据以实现用户交互
void CreateList(Stu &L,int n); //创建一个顺序表
void ListInsert(Stu &L); //在顺序表的指定位置插入一个元素
void ListDelete(Stu &L); //在顺序表的指定位置删除一个元素
void MergeList_Stu(Stu &L,Stu &M); //将两个有序顺序表合并成一个新的有序顺序表
int main(int argc, char *argv[])
{
int Listlength,SecondListlength;
Stu L;
InitList(L);
cout<<"欲创建一个学生成绩顺序表,请输入表长:"<<endl;
cin>>Listlength;
CreateList(L,Listlength);
ListInsert(L);
ListDelete(L);
cout<<"欲再次创建一个顺序表,请根据操作说明助推程序进行!"<<endl;
Stu M;
InitList(M);
cout<<"请输入第二个顺序表表长:"<<endl;
cin>>SecondListlength;
CreateList(M,SecondListlength);
MergeList_Stu(L,M);
delete[]L.elem; //释放L存储空间
delete[]M.elem; //释放M存储空间
return 0;
}
int InitList(Stu &L) //初始化数据表
{
L.elem=new int[MAXSIZE];
if(!L.elem) exit(OVERFLOW);
L.length=0;
return OK;
}
void PrintList(Stu &L) //实现功能后,打印出当前数据以实现用户交互
{
cout<<"当前顺序表所有元素:";
for (int i = 0; i<L.length; i++)
{
cout<<L.score[i]<<" ";
}
cout<<endl;
}
void CreateList(Stu &L,int n)
{
if(n<1||n>MAXSIZE) cout<<"创建失败!请输入合法长度!"<<endl;
cout<<"长度合法,请输入各项数据!"<<endl;
for(int i=0;i<n;i++)
{
cin>>L.score[i];
L.length++;
}
cout<<"创建成功!"<<endl;
PrintList(L);
}
void ListInsert(Stu &L) //在顺序表的指定位置插入一个元素
{
int i,newscore;
cout<<"请输入插入位置:";
cin>>i;
while((i<1)||(i>L.length+1)) //判断插入位置是否合法
{
cout<<"插入位置无效!请重新输入插入位置:";
cin>>i;
}
if(L.length==MAXSIZE) //判断存储空间是否已满
{
cout<<"已达到最大存储空间,无法继续执行插入操作!"<<endl;
}
cout<<"请输入所要插入的元素:";
cin>>newscore;
for(int j=L.length;j>=i;j--)
L.score[j]=L.score[j-1]; //所要插入的位置及其之后的元素逆着后移一位
L.score[i-1]=newscore; //将要插入的数据e放入第i个位置
++L.length; //表长+1
cout<<"插入成功!!!"<<endl;
PrintList(L);
}
void ListDelete(Stu &L) //在顺序表的指定位置删除一个元素
{
int i;
cout<<"请输入删除位置:";
cin>>i;
while((i<1)||(i>L.length)) //判断删除位置是否合法
{
cout<<"删除位置无效!请重新输入删除位置:";
cin>>i;
}
for(int j=i;j<=L.length-1;j++)
L.score[j-1]=L.score[j]; //被删除元素之后的元素顺着前移一位
--L.length; //表长-1
cout<<"删除成功!!!"<<endl;
PrintList(L);
}
void MergeList_Stu(Stu &L,Stu &M) //将两个有序顺序表合并成一个新的有序顺序表
{
int i,j,temp;
Stu W;
InitList(W);
W.length=L.length+M.length; //新表长度为待合并两表长度之和
for(i=0;i<L.length;i++) //依次将两表中的数据录入新表中
{
W.score[i]=L.score[i];
}
i=0;
for(int j=L.length;j<W.length;j++)
{
W.score[j]=M.score[i];
i++;
}
//以下用选择排序对新表进行排序
for(i=0;i<W.length-1;i++)
{
for(j=i+1;j<W.length;j++)
{
if(W.score[i]>W.score[j])
{
temp=W.score[i];
W.score[i]=W.score[j];
W.score[j]=temp;
}
}
}
PrintList(W);
}
执行效果如下:
分享完毕~
欢迎小伙伴们在文下评论和私信喔~