您的位置:首页 > 理论基础 > 数据结构算法

数据结构实验1-顺序表-各种操作

2015-09-27 17:08 405 查看
要求:

创建两个顺序表实现两个顺序表的合并以及扩容,以及单个顺序表的查找,插入,删除,排序,清空,销毁,查找前驱以及后继等等。

(有错会继续改的)

以及程序的完备性,健壮性,可读性等等

经过这次实验感觉我写的最多的不是程序而是告诉程序执行者下一步该怎么做怎么选择。。。。。(一种上学期c语言课程设计的赶脚)

#include <iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#define MAXSIZE 100
#define INCREMENT 20
using namespace std;
typedef int status;
char str[10];
int n1,n2,rec;
typedef struct
{
int *elem;
int len;
int lise;
}sqlist;
sqlist sq1,sq2,sq3;
status initlist(sqlist &l)
{
l.elem=(int *)malloc(sizeof(sqlist)*MAXSIZE);
if(!l.elem)
return 0;
l.len=0;
l.lise=MAXSIZE;
return 1;
}
void getsq(int n,sqlist &l)//获取顺序表中元素
{
for(int i=1;i<=n;++i)
{
scanf("%d",&l.elem[i]);
l.len++;
}
}
void destorylist(sqlist &l)//销毁顺序表
{
if(l.elem)
delete []l.elem;
}
void clearlist(sqlist &l)//清空顺序表
{
l.len=0;
}
status listempty(sqlist &l)//判断顺序表是否为空
{
if(l.len==0)
{
return true;
}
return false;
}
status listlength(sqlist l)//获取非空顺序表中元素个数
{
return l.len;
}
status getelem(sqlist l,int i,int &e)//获取顺序表中第i个元素
{
if(i<=0||i>l.len)
{
return -1;
}
e=l.elem[i];
return 1;
}
status locatelem(sqlist l,int e)//找顺序表中与e相等的第一个数的位置
{
for(int i=1;i<=l.len;++i)
{
if(l.elem[i]==e)
return i;
}
return -1;
}
status priorelem(sqlist l,int cur,int &pre)//在顺序表中寻找指定值的前驱
{
for(int i=1;i<=l.len;++i)
{
if(l.elem[i]==cur)
{
pre=l.elem[i-1];
return 1;
}
}
return -1;
}
status nextelem(sqlist l,int cur,int &next)//在顺序表中寻找指定值的后继
{
for(int i=1;i<=l.len;++i)
{
if(l.elem[i]==cur)
{
next=l.elem[i+1];
return 1;
}
}
return -1;
}
void listinsert(sqlist &l,int i,int e)//在顺序表第i个位置插入指定值
{
for(int j=l.len;j>=i;j--)
{
l.elem[j+1]=l.elem[j];
}
l.elem[i]=e;
l.len++;
}
status listdelete(sqlist &l,int i,int &e)//删除顺序表第i个元素并返回该元素
{
if(i<=0||i>l.len)
return -1;
e=l.elem[i];
for(int j=i+1;j<=l.len;++j)
{
l.elem[j-1]=l.elem[j];
}
l.len--;
return 1;
}
void traverselist(sqlist l)//遍历顺序表并输出元素
{
for(int i=1;i<=l.len;++i)
{
if(i==1)
printf("%d",l.elem[i]);
else
printf(" %d",l.elem[i]);
}
printf("\n");
}
void sortlist(sqlist &l)
{
for(int i=1;i<l.len;++i)
{
for(int j=i+1;j<=l.len;++j)
{
if(l.elem[i]>l.elem[j])
{
int tem=l.elem[i];
l.elem[i]=l.elem[j];
l.elem[j]=tem;
}
}
}
printf("\n");
}
status kuorong(sqlist &l)
{
int c;
printf("若选择扩容则输入1否则输入0\n");
scanf("%d",&c);
if(c==1)
{
int *newbase=(int *)realloc(l.elem,(l.lise+INCREMENT)*sizeof(sqlist));
if(!newbase)
{
printf("扩容失败\n");
return 0;
}
return 1;
}
else
return 0;
}
status mergelist(sqlist l1,sqlist l2,sqlist &l3)
{
if(l3.lise<l1.len+l2.len)
{
printf("L3空间不足\n");
if(!kuorong(sq3))
return 0;
}
int k=1,i=1,j=1;
l3.len=0;
while(i<=l1.len&&j<=l2.len)
{
if(l1.elem[i]<=l2.elem[j])
{
l3.elem[k++]=l1.elem[i];
l3.len++;
i++;
}
else
{
l3.elem[k++]=l2.elem[j];
j++;
l3.len++;
}
}
while(i<=l1.len)
{
l3.elem[k++]=l1.elem[i++];
l3.len++;
}
while(j<=l2.len)
{
l3.elem[k++]=l2.elem[j++];
l3.len++;
}
return 1;
}
void choose()
{
int ch,x,y,z;
system("cls");
printf("***************************\n");
printf("请选择您所要进行的操作\n");
printf("1.排序\n2.判断顺序表是否为空\n3.顺序表中元素个数\n");
printf("4.寻找表中第i个元素\n5.寻找指定值及前驱后继\n6.插入\n7.删除\n");
printf("8.合并顺序表\n9.遍历输出\n10.清空\n11.销毁\n\n");
scanf("%d",&ch);
if(ch==1)
{
int c;
system("cls");
printf("若您想对第一个顺序表进行排序输入1对第二个输入2\n");
scanf("%d",&c);
if(c==1)
{
sortlist(sq1);
traverselist(sq1);
}
else if(c==2)
{
sortlist(sq2);
traverselist(sq2);
}
else
printf("输入有误\n");
system("pause");
}
else if(ch==2)
{
system("cls");
int c;
printf("若您想判断第一个顺序表是否为空输入1判断第二个输入2\n");
while(scanf("%d",&c)&&(c!=1&&c!=2))
{
printf("输入有误请重新输入\n");
}
if(c==1)
{
if(listempty(sq1))
printf("顺序表1为空\n");
else
printf("顺序表1不为空\n");
}
else
{
if(listempty(sq2))
printf("顺序表2为空\n");
else
printf("顺序表2不为空\n");
}
system("pause");
}
else if(ch==3)
{
system("cls");
int c;
printf("若您想查找第一个顺序表元素个数输入1查找第二个输入2\n");
while(scanf("%d",&c)&&(c!=1&&c!=2))
{
printf("输入有误请重新输入\n");
}
if(c==1)
printf("第一个顺序表中元素个数为%d\n",listlength(sq1));
else
printf("第二个顺序表中元素个数为%d\n",listlength(sq2));
system("pause");
}
else if(ch==4)
{
system("cls");
int c,d,e;
printf("若您想查找第一个顺序表中元素输入1查找第二个输入2\n");
while(scanf("%d",&c)&&(c!=1&&c!=2))
{
printf("输入有误请重新输入\n");
}
if(c==1)
{
printf("请输入您所要查找元素的位置\n");
scanf("%d",&d);
int tem=getelem(sq1,d,e);
if(tem==-1)
printf("输入所要查找元素位置错误\n");
else
printf("您所要查找的顺序表%d中第%d个元素是%d\n",c,d,e);
}
else
{
printf("请输入您所要查找元素的位置\n");
scanf("%d",&d);
int tem=getelem(sq2,d,e);
if(tem==-1)
printf("输入所要查找元素位置错误\n");
else
printf("您所要查找的顺序表%d中第%d个元素是%d\n",c,d,e);
}
system("pause");
}
else if(ch==5)
{
system("cls");
int c,d,e,f;
printf("若您想查找第一个顺序表指定值输入1查找第二个输入2\n");
while(scanf("%d",&c)&&(c!=1&&c!=2))
printf("输入有误请重新输入\n");
if(c==1)
{
printf("若您想查找指定值位置输入2,查找指定值前驱输入1,查找指定值后继输入3\n");
scanf("%d",&d);
if(d==1)
{
printf("请输入指定值\n");
scanf("%d",&f);
int tem=priorelem(sq1,f,e);
if(tem==-1)
printf("指定值输入有误\n");
else
printf("%d在顺序表%d中的前驱是%d\n",f,c,e);
}
else if(d==2)
{
printf("请输入指定值\n");
scanf("%d",&f);
int tem=locatelem(sq1,f);
if(tem==-1)
printf("指定值输入有误\n");
else
printf("%d在顺序表%d中的位置是%d\n",f,c,tem);
}
else if(d==3)
{
printf("请输入指定值\n");
scanf("%d",&f);
int tem=nextelem(sq1,f,e);
if(tem==-1)
printf("指定值输入有误\n");
else
printf("%d在顺序表%d中的后继是%d\n",f,c,e);
}
}
else if(c==2)
{
printf("若您想查找指定值位置输入2,查找指定值前驱输入1,查找指定值后继输入3\n");
scanf("%d",&d);
if(d==1)
{
printf("请输入指定值\n");
scanf("%d",&f);
int tem=priorelem(sq2,f,e);
if(tem==-1)
printf("指定值输入有误\n");
else
printf("%d在顺序表%d中的前驱是%d\n",f,c,e);
}
else if(d==2)
{
printf("请输入指定值\n");
scanf("%d",&f);
int tem=locatelem(sq2,f);
if(tem==-1)
printf("指定值输入有误\n");
else
printf("%d在顺序表%d中的位置是%d\n",f,c,tem);
}
else if(d==3)
{
printf("请输入指定值\n");
scanf("%d",&f);
int tem=nextelem(sq2,f,e);
if(tem==-1)
printf("指定值输入有误\n");
else
printf("%d在顺序表%d中的后继是%d\n",f,c,e);
}
}
system("pause");
}
else if(ch==6)
{
system("cls");
int c,d,e,f;
printf("若您想插入第一个顺序表输入1插入第二个输入2\n");
while(scanf("%d",&c)&&(c!=1&&c!=2))
printf("输入有误请重新输入\n");
if(c==1)
{
printf("请输入您所想插入的值以及位置\n");
scanf("%d%d",&e,&d);
listinsert(sq1,d,e);
traverselist(sq1);
}
else if(c==2)
{
printf("请输入您所想插入的值以及位置\n");
scanf("%d%d",&e,&d);
listinsert(sq2,d,e);
traverselist(sq2);
}
system("pause");
}
else if(ch==7)
{
system("cls");
int c,d,e,f;
printf("若您想删除第一个顺序表中元素输入1删除第二个表中元素输入2\n");
while(scanf("%d",&c)&&(c!=1&&c!=2))
printf("输入有误请重新输入\n");
if(c==1)
{
printf("请输入您所想删除元素的位置\n");
scanf("%d",&d);
int tem=listdelete(sq1,d,e);
if(tem==-1)
printf("位置信息输入有误\n");
else
{
printf("您所想删除的位置%d处的元素是%d\n",d,e);
traverselist(sq1);
}
}
else if(c==2)
{
printf("请输入您所想删除元素的位置\n");
scanf("%d",&d);
int tem=listdelete(sq2,d,e);
if(tem==-1)
printf("位置信息输入有误\n");
else
{
printf("您所想删除的位置%d处的元素是%d\n",d,e);
traverselist(sq2);
}
}
system("pause");
}
else if(ch==8)
{
system("cls");
if(!mergelist(sq1,sq2,sq3))
printf("合并失败\n");
traverselist(sq3);
system("pause");
}
else if(ch==9)
{
system("cls");
int c,d,e,f;
printf("若您想输出第一个顺序表输入1输出第二个表输入2\n");
while(scanf("%d",&c)&&(c!=1&&c!=2))
printf("输入有误请重新输入\n");
if(c==1)
traverselist(sq1);
else
traverselist(sq2);
system("pause");
}
else if(ch==10)
{
system("cls");
int c;
printf("若您想对第一个顺序表进行清空输入1对第二个输入2\n");
scanf("%d",&c);
if(c==1)
clearlist(sq1);
else if(c==2)
clearlist(sq2);
else
printf("输入有误\n");
system("pause");
}
else if(ch==11)
{
system("cls");
int c;
printf("若您想对第一个顺序表进行销毁输入1对第二个输入2\n");
while(scanf("%d",&c)&&(c!=1&&c!=2))
{
printf("输入有误请重新输入\n");
}
if(c==1)
destorylist(sq1);
else
destorylist(sq2);
system("pause");
}
}
int main()
{
int mark;
while(1)
{
system("cls");
printf("------欢迎进入系统------\n");
printf("请输入您所要创建的第一个顺序表的元素个数\n");
scanf("%d",&n1);
while(!initlist(sq1))
{
printf("创建出错,麻烦重新输入当前顺序表元素个数\n");
scanf("%d",&n1);
}
printf("请输入该顺序表的元素\n");
getsq(n1,sq1);
printf("请输入您所要创建的第二个顺序表的元素个数\n");
scanf("%d",&n2);
while(!initlist(sq2))
{
printf("创建出错,麻烦重新输入当前顺序表元素个数\n");
scanf("%d",&n2);
}
printf("请输入该顺序表的元素\n");
getsq(n2,sq2);
initlist(sq3);
while(1)
{
int c;
choose();
printf("若您想现在退出请输入0否则输入1\n");
scanf("%d",&c);
if(!c)
break;
}
printf("若您想现在退出系统请输入0否则输入1\n");
scanf("%d",&mark);
if(!mark)
break;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: