实验二:
作者说:
我觉得我写的这个有点复杂了,程序很长,是有关顺序表的操作,大家如果有需要的话,可以参考一下思路,嘿嘿嘿。
一、菜单示例:
二、实验内容:
(1)实验目的
通过该实验,深入理解顺序表的逻辑结构、物理结构等概念,掌握顺序表基本操作的编程实现,注意顺序表插入、删除等操作过程中数据元素的移动现象,培养学生编写程序时,要考虑程序的健壮性,全面考虑问题,熟练掌握通过函数参数返回函数结果的办法。
(2)实验内容
编程实现顺序表下教材第二章定义的线性表的基本操作,并根据已经实现的基本操作,实现两个非递减有序的线性表的合并,注意,合并时,如果有重复的元素,请保留一个。
(3)实验要求
(a)求前驱是指,输入一个元素值(而不是位置),求该元素在顺序表中的直接前驱元素值。求后继是指:输入一个元素值(而不是位置),求该元素在顺序表中的直接后继元素值;
(b)为了方便修改数据元素的类型,请使用类型重定义,可以方便修改线性表中的数据元素的类型;
(c)大部分函数的返回结果应是函数执行是否成功的一种状态,执行成功了,才返回具体的结果值;
(d)对每个功能进行测试时,要求把不合法的情况也测试一下。具体见下面的测试用例;
(e)采用菜单形式对应各个操作,使其编成一个完整的小软件。
三、代码实现:
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
using namespace std;
#define list_init_size 100
#define listincrement 10
//定义全局变量。
int chushiA=0;//没有进行初始化操作之前,进行其他操作,其他功能无法正常进行。
int chushiB=0;
typedef int ElemType;
typedef struct{
ElemType *elem;//存储空间基址。
int length;//当前长度。
int listsize;//当前分配的存储容量(以sizeof(Element)为单位)。
}SqList;
int chushihua_a(SqList &xian_a);//1.声明初始化线性表A的函数。
int chushihua_b(SqList &xian_b);//1.声明初始化线性表B的函数。
int xiao_hui(SqList &xian_a,SqList &xian_b);//2.销毁线性表函数。
int qing_kong(SqList &xian_a,SqList &xian_b);//3.清空线性表的函数。
int pan_kong(SqList xian_a,SqList xian_b);//4.判断是否为空的函数。
int biao_length(SqList xian_a,SqList xian_b);//5.求线性表长度的函数。
int qiu_yuansu(SqList &xian_a,SqList &xian_b);//6.获取线性表中指定位置的元素。
int qiu_weizhi(SqList &xian_a,SqList &xian_b);//7.获取元素的位置。
int qian_qu(SqList &xian_a,SqList &xian_b);//8.求前驱函数。
int hou_ji(SqList &xian_a,SqList &xian_b);//9.求后继函数。
int cha_new(SqList &xian_a,SqList &xian_b);//10.指定位置插入元素。
int shan_wei(SqList &xian_a,SqList &xian_b);//11.删除指定位置的元素。
int intput_biao(SqList &xian_a,SqList &xian_b);//12.显示线性表的函数。
int he_bing(SqList &xian_a,SqList &xian_b,SqList &xian_c);//13.合并两个非递减有序的线性表。
int main()
{
cout<<endl;
cout<<"☆☆☆欢迎使用线性表小程序!☆☆☆"<<endl;
cout<<"author---Henan University.software engineering.李思佳"<<endl;
int n=1;
while(n)
{
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<<"12---显示线性表"<<endl;
cout<<"13---合并两个非递减有序的线性表"<<endl;
cout<<"☆☆☆退出,输入一个负数!☆☆☆"<<endl;
cout<<endl;
int select;
cout<<"请输入操作代码:";
cin>>select;
switch(select)
{
case 1:
system("cls");
SqList xian_a;
SqList xian_b;
chushihua_a(xian_a);
chushihua_b(xian_b);
cout<<endl;
break;
case 2:
system("cls");
xiao_hui(xian_a,xian_b);
cout<<endl;
break;
case 3:
system("cls");
qing_kong(xian_a,xian_b);
cout<<endl;
break;
case 4:
system("cls");
pan_kong(xian_a,xian_b);
cout<<endl;
break;
case 5:
system("cls");
biao_length(xian_a,xian_b);
cout<<endl;
break;
case 6:
system("cls");
qiu_yuansu(xian_a,xian_b);
cout<<endl;
break;
case 7:
system("cls");
qiu_weizhi(xian_a,xian_b);
cout<<endl;
break;
case 8:
system("cls");
qian_qu(xian_a,xian_b);
cout<<endl;
break;
case 9:
system("cls");
hou_ji(xian_a,xian_b);
cout<<endl;
break;
case 10:
system("cls");
cha_new(xian_a,xian_b);
cout<<endl;
break;
case 11:
system("cls");
shan_wei(xian_a,xian_b);
cout<<endl;
break;
case 12:
system("cls");
intput_biao(xian_a,xian_b);
cout<<endl;
break;
case 13:
system("cls");
SqList xian_c;
he_bing(xian_a,xian_b,xian_c);
cout<<endl;
break;
default:
system("cls");
if(select<0)
{
n=0;
cout<<"☆☆☆已经退出程序,欢迎下次使用!☆☆☆"<<endl;
break;
}
}
}
}
//1.初始化顺序表A。
int chushihua_a(SqList &xian_a)
{
xian_a.elem=(ElemType *)malloc(list_init_size*sizeof(ElemType));
if(!xian_a.elem)
{
cout<<"线性表A存储分配失败,请您重新操作!"<<endl;
}
else
{
xian_a.length=0;
chushiA=1;
xian_a.listsize=list_init_size;
cout<<"线性表A的存储空间分配成功!"<<endl;
cout<<"请输入A中的元素,不要输入重复元素,以负数结束:"<<endl;
int again=1;
do{
again=1;
int a;
while(scanf("%d",&a)>0&&getchar()!='\n')
{
xian_a.length++;
if(xian_a.length==1)
{
xian_a.elem[0]=a;
}
else
{
for(int i=0;i<xian_a.length-1;i++)
{
if(a!=xian_a.elem[i])
{
xian_a.elem[xian_a.length-1]=a;
}
else
{
cout<<"输入元素中有重复值,请重新输入A:"<<endl;
xian_a.length=0;
again=0;
break;
}
}
}
}
}while(again==0);
}
}
//1.初始化线性表B。
int chushihua_b(SqList &xian_b)
{
cout<<endl;
xian_b.elem=(ElemType *)malloc(list_init_size*sizeof(ElemType));
if(!xian_b.elem)
{
cout<<"线性表B存储分配失败,请您重新操作!"<<endl;
}
else
{
xian_b.length=0;
xian_b.listsize=list_init_size;
chushiB=1;
cout<<"线性表B的存储空间分配成功!"<<endl;
cout<<"请输入B中的元素,不要输入重复元素,以负数结束:"<<endl;
int again=1;
do{
again=1;
int a;
while(scanf("%d",&a)>0&&getchar()!='\n')
{
xian_b.length++;
if(xian_b.length==1)
{
xian_b.elem[0]=a;
}
else
{
for(int i=0;i<xian_b.length-1;i++)
{
if(a!=xian_b.elem[i])
{
xian_b.elem[xian_b.length-1]=a;
}
else
{
cout<<"您输入的元素中有重复值,请重新输入B:"<<endl;
xian_b.length=0;
again=0;
break;
}
}
}
}
}while(again==0);
}
}
//2.销毁线性表。
int xiao_hui(SqList &xian_a,SqList &xian_b)
{
if(chushiA==0&&chushiB==0)
cout<<"!!!线性表A,B还未初始化,请先初始化!!!"<<endl;
else
{
int i;
cout<<"1----销毁链表A"<<endl;
cout<<"2----销毁链表B"<<endl;
cout<<"请选择:";
cin>>i;
if(i==1)
{
free(xian_a.elem);
chushiA=0;
cout<<"链表A销毁成功!"<<endl;
}
else if(i==2)
{
free(xian_b.elem);
chushiB=0;
cout<<"链表B销毁成功!"<<endl;
}
else
{
system("cls");
cout<<"您的选择有误!"<<endl;
xiao_hui(xian_a,xian_b);
}
}
}
//3.清空线性表。
int qing_kong(SqList &xian_a,SqList &xian_b)
{
if(chushiA==0&&chushiB==0)
cout<<"!!!线性表A、B还未初始化,请先初始化!!!"<<endl;
else
{
xian_a.length=0;
xian_b.length=0;
cout<<"线性表A已清空!"<<endl;
cout<<"线性表B已清空!"<<endl;
}
}
//4.判断线性表是否为空。
int pan_kong(SqList xian_a,SqList xian_b)
{
if(chushiA==0)
cout<<"!!!线性表A还未初始化,请先初始化!!!"<<endl;
else
{
if(xian_a.length==0)
cout<<"线性表A为空!"<<endl;
else
cout<<"线性表A不为空!"<<endl;
}
if(chushiB==0)
cout<<"!!!线性表B还未初始化,请先初始化!!!"<<endl;
else
{
if(xian_b.length==0)
cout<<"线性表B为空!"<<endl;
else
cout<<"线性表B不为空!"<<endl;
}
}
// 5.求线性表长度.
int biao_length(SqList xian_a,SqList xian_b)
{
if(chushiA==0)
cout<<"!!!线性表A还未初始化,请先初始化!!!"<<endl;
else
cout<<"线性表A的长度为:"<<xian_a.length<<endl;
if(chushiB==0)
cout<<"!!!线性表B还未初始化,请先初始化!!!"<<endl;
else
cout<<"线性表B的长度为:"<<xian_b.length<<endl;
}
//6.获取线性表中指定位置的元素。
int qiu_yuansu(SqList &xian_a,SqList &xian_b)
{
if(chushiA==0&&chushiB==0)
cout<<"线性表A、B还未初始化,请先初始化!"<<endl;
else
{
int xuanze;
int i;
cout<<"1----线性表A"<<endl;
cout<<"2----线性表B"<<endl;
cout<<"请选择要操作的线性表:";
cin>>xuanze;
if(xuanze==1)
{
cout<<"请输入线性表A中某一位置:";
cin>>i;
if(i>0&&i<=xian_a.length)
cout<<"您查询的元素为:"<<xian_a.elem[i-1]<<endl;
else
cout<<"您输入的位置不在线性表A范围内!"<<endl;
}
else if(xuanze==2)
{
cout<<"请输入线性表B中某一位置:";
cin>>i;
if(i>0&&i<=xian_b.length)
cout<<"您查询的元素为:"<<xian_b.elem[i-1]<<endl;
else
cout<<"您输入的位置不在线性表B范围内!"<<endl;
}
else
{
cout<<"请正确选择要操作的线性表!"<<endl;
cout<<endl;
qiu_yuansu(xian_a,xian_b);
}
}
}
//7.获取元素的位置。
int qiu_weizhi(SqList &xian_a, SqList &xian_b)
{
if(chushiA==0&&chushiB==0)
cout<<"线性表A、B还未初始化,请先初始化!"<<endl;
else
{
cout<<"1----线性表A"<<endl;
cout<<"2----线性表B"<<endl;
cout<<"请选择要操作的线性表:";
int xuanze;
cin>>xuanze;
int yuansu;
int jishu=0;
if(xuanze==1)
{
cout<<"请输入您要查询的元素:";
cin>>yuansu;
for(int i=0;i<xian_a.length;i++)
{
jishu++;
if(xian_a.elem[i]==yuansu)
{
cout<<"您查询的元素位置是:"<<i+1<<endl;
jishu=0;
break;
}
}
if(jishu==xian_a.length)
cout<<"您所输入的元素不在线性表A里!"<<endl;
}
else if(xuanze==2)
{
cout<<"请输入您要查询的元素:";
cin>>yuansu;
for(int i=0;i<xian_b.length;i++)
{
jishu++;
if(xian_b.elem[i]==yuansu)
{
cout<<"您查询的元素位置是:"<<i+1<<endl;
jishu=0;
break;
}
}
if(jishu==xian_b.length)
cout<<"您所输入的元素不在线性表A里!"<<endl;
}
else
{
cout<<"请正确选择要操作的线性表!"<<endl;
cout<<endl;
qiu_weizhi(xian_a,xian_b);
}
}
}
//8.求前驱函数。
int qian_qu(SqList &xian_a,SqList &xian_b)
{
if(chushiA==0&&chushiB==0)
cout<<"线性表A、B还未初始化,请先初始化!"<<endl;
else
{
int xuanze;
int yuansu;
int jishu=1;
cout<<"1----线性表A"<<endl;
cout<<"2----线性表B"<<endl;
cout<<"请选择要操作的线性表:";
cin>>xuanze;
if(xuanze==1)
{
cout<<"请输入您要查询的元素:";
cin>>yuansu;
if(yuansu==xian_a.elem[0])
cout<<"第一个元素没有前驱!"<<endl;
else
{
for(int i=1;i<xian_a.length;i++)
{
if(yuansu==xian_a.elem[i])
{
cout<<"您查询元素的前驱为:"<<xian_a.elem[i-1]<<endl;
break;
}
jishu++;
}
if(jishu==xian_a.length)
cout<<"该元素不存在!"<<endl;
}
}
else if(xuanze==2)
{
cout<<"请输入您要查询的元素:";
cin>>yuansu;
if(yuansu==xian_b.elem[0])
cout<<"第一个元素没有前驱!"<<endl;
else
{
for(int i=1;i<xian_b.length;i++)
{
if(yuansu==xian_b.elem[i])
{
cout<<"您查询元素的前驱为:"<<xian_b.elem[i-1]<<endl;
break;
}
jishu++;
}
if(jishu==xian_b.length)
cout<<"该元素不存在!"<<endl;
}
}
else
{
cout<<"请正确选择要操作的线性表!"<<endl;
cout<<endl;
qian_qu(xian_a,xian_b);
}
}
}
//9.求后继函数。
int hou_ji(SqList &xian_a,SqList &xian_b)
{
if(chushiA==0&&chushiB==0)
cout<<"线性表A、B还未初始化,请先初始化!"<<endl;
else
{
int xuanze;
int yuansu;
int jishu=0;
cout<<"1----线性表A"<<endl;
cout<<"2----线性表B"<<endl;
cout<<"请选择要操作的线性表:";
cin>>xuanze;
if(xuanze==1)
{
cout<<"请输入您要查询的元素:";
cin>>yuansu;
if(yuansu==xian_a.elem[xian_a.length-1])
cout<<"最后一个元素没有后继!"<<endl;
else
{
for(int i=0;i<xian_a.length;i++)
{
if(yuansu==xian_a.elem[i])
{
cout<<"您查询元素的后继为:"<<xian_a.elem[i+1]<<endl;
break;
}
jishu++;
}
if(jishu==xian_a.length)
cout<<"该元素不存在!"<<endl;
}
}
else if(xuanze==2)
{
cout<<"请输入您要查询的元素:";
cin>>yuansu;
if(yuansu==xian_b.elem[xian_b.length-1])
cout<<"最后一个元素没有后继!"<<endl;
else
{
for(int i=0;i<xian_b.length;i++)
{
if(yuansu==xian_b.elem[i])
{
cout<<"查询元素的后继为:"<<xian_b.elem[i+1]<<endl;
break;
}
jishu++;
}
if(jishu==xian_b.length)
cout<<"该元素不存在!"<<endl;
}
}
else
{
cout<<"请正确选择要操作的线性表!"<<endl;
cout<<endl;
hou_ji(xian_a,xian_b);
}
}
}
//10.指定位置插入元素。
int cha_new(SqList &xian_a,SqList &xian_b)
{
if(chushiA==0&&chushiB==0)
cout<<"线性表A、B还未初始化,请先初始化!"<<endl;
else
{
int xuanze;
int weizhi;
int yuansu;
cout<<"1----线性表A"<<endl;
cout<<"2----线性表B"<<endl;
cout<<"请选择要操作的线性表:";
cin>>xuanze;
if(xuanze==1)
{
cout<<"请输入要插入的位置(范围1-"<<xian_a.length+1<<"):";
cin>>weizhi;
if(weizhi>=1&&weizhi<=xian_a.length+1)
{
if(xian_a.length>=xian_a.listsize)
{
cout<<"存储空间已满,正在为您重新分配存储空间!"<<endl;
ElemType *newbase=(ElemType*)malloc((xian_a.listsize+listincrement)*sizeof(ElemType));
if(!newbase)
{
cout<<"重新分配内存空间失败!"<<endl;
return 0;
}
else
{
cout<<"重新分配内存空间成功!"<<endl;
xian_a.elem=newbase;
xian_a.listsize+=listincrement;
}
}
cout<<"请输入您要插入的元素:";
cin>>yuansu;
int *q;
q=&xian_a.elem[weizhi-1];
for(int *p=&(xian_a.elem[xian_a.length-1]);p>=q;p--)
{
*(p+1)=*p;
}
*q=yuansu;
xian_a.length+=1;
cout<<"插入成功!"<<endl;
return 0;
}
else
cout<<"您输入的位置不在正确范围内!"<<endl;
}
else if(xuanze==2)
{
cout<<"请输入要插入的位置(范围1-"<<xian_b.length+1<<"):";
cin>>weizhi;
if(weizhi>=1&&weizhi<=xian_b.length+1)
{
if(xian_b.length>=xian_b.listsize)
{
cout<<"存储空间已满,正在为您重新分配存储空间!"<<endl;
ElemType *newbase=(ElemType*)malloc((xian_b.listsize+listincrement)*sizeof(ElemType));
if(!newbase)
{
cout<<"重新分配内存空间失败!"<<endl;
return 0;
}
else
{
xian_b.elem=newbase;
xian_b.listsize+=listincrement;
}
}
cout<<"请输入您要插入的元素:";
cin>>yuansu;
int *q=&(xian_b.elem[weizhi-1]);
for(int *p=&(xian_b.elem[xian_b.length-1]);p>=q;p--)
{
*(p+1)=*p;
}
*q=yuansu;
xian_b.length+=1;
cout<<"插入成功!"<<endl;
return 0;
}
else
cout<<"您输入的位置不在正确范围内!"<<endl;
}
else
{
cout<<"请正确选择要操作的线性表!"<<endl;
cout<<endl;
cha_new(xian_a,xian_b);
}
}
}
//11.删除指定位置的元素。
int shan_wei(SqList &xian_a,SqList &xian_b)
{
if(chushiA==0&&chushiB==0)
cout<<"线性表A、B还未初始化,请先初始化!"<<endl;
else
{
int xuanze;
int weizhi;
int yuansu;
cout<<"1----线性表A"<<endl;
cout<<"2----线性表B"<<endl;
cout<<"请选择要操作的线性表:";
cin>>xuanze;
if(xuanze==1)
{
cout<<"请选择你要删除的元素的位置(范围1-"<<xian_a.length<<"):";
cin>>weizhi;
if(weizhi>=1&&weizhi<=xian_a.length)
{
int *q=&(xian_a.elem[weizhi-1]);
yuansu=*q;
for(int *p=&(xian_a.elem[xian_a.length-1]);p>q;q++)
{
*q=*(q+1);
}
--xian_a.length;
cout<<"删除元素"<<yuansu<<"成功!"<<endl;
return 0;
}
else
cout<<"您输入的位置不在正确范围内!"<<endl;
}
else if(xuanze==2)
{
cout<<"请选择你要删除的元素的位置(范围1-"<<xian_b.length<<"):";
cin>>weizhi;
if(weizhi>=1&&weizhi<=xian_b.length)
{
int *q=&(xian_b.elem[weizhi-1]);
yuansu=*q;
for(int *p=&(xian_b.elem[xian_b.length-1]);p>q;q++)
{
*q=*(q+1);
}
--xian_b.length;
cout<<"删除元素"<<yuansu<<"成功!"<<endl;
return 0;
}
else
cout<<"您输入的位置不在正确范围内!"<<endl;
}
else
{
cout<<"请正确选择要操作的线性表!"<<endl;
cout<<endl;
shan_wei(xian_a,xian_b);
}
}
}
//12.显示线性表的函数。
int intput_biao(SqList &xian_a,SqList &xian_b)
{
if(chushiA==0&&chushiB==0)
cout<<"线性表A、B还未初始化,请先初始化!"<<endl;
else
{
cout<<"线性表A:";
for(int i=0;i<=xian_a.length-1;i++)
{
cout<<xian_a.elem[i]<<" ";
}
cout<<endl;
cout<<"线性表B:";
for(int i=0;i<=xian_b.length-1;i++)
{
cout<<xian_b.elem[i]<<" ";
}
cout<<endl;
return 0;
}
}
//13.合并两个非递减有序的线性表。
int he_bing(SqList &xian_a,SqList &xian_b,SqList &xian_c)
{
if(chushiA==0&&chushiB==0)
cout<<"线性表A、B还未初始化,请先初始化!"<<endl;
else
{
xian_c.elem=(ElemType *)malloc(list_init_size*sizeof(ElemType));
if(!xian_c.elem)
cout<<"合并后的线性表C存储分配失败,请重新操作!"<<endl;
else
{
cout<<"线性表C的内存空间分配成功!"<<endl;
cout<<"线性表合并后:";
xian_c.listsize=xian_c.length=xian_a.length+xian_b.length;
ElemType *pa=&xian_a.elem[0];
ElemType *pb=&xian_b.elem[0];
ElemType *pc=&xian_c.elem[0];
ElemType *pa_end=&xian_a.elem[xian_a.length-1];
ElemType *pb_end=&xian_b.elem[xian_b.length-1];
while(pa<=pa_end&&pb<=pb_end)//合并线性表A、B。
{
if(*pa<=*pb)
{
*pc=*pa;
pa++;
pc++;
}
else
{
*pc=*pb;
pb++;
pc++;
}
}
while(pa<=pa_end)//把表A中的剩余元素插入C。
{
*pc=*pa;
pa++;
pc++;
}
while(pb<=pb_end)//把表B中的剩余元素插入C。
{
*pc=*pb;
pb++;
pc++;
}
for(int i=0;i<=xian_c.length-1;i++)//查重,去重复值。
{
if(xian_c.elem[i]==xian_c.elem[i+1])
{
for(int j=i+1;j<=xian_c.length-1;j++)
{
xian_c.elem[j]=xian_c.elem[j+1];
}
xian_c.length--;
}
}
for(int i=0;i<=xian_c.length-1;i++)
{
cout<<xian_c.elem[i]<<" ";
}
cout<<endl;
}
}
}