数据结构实验一(1)
来源:互联网 发布:linux 通配符 编辑:程序博客网 时间:2024/05/21 09:00
1)顺序表的操作① 输入一组整型元素序列,建立线性表的顺序存储结构。② 实现该线性表的遍历。③ 在该顺序表中查找某一元素,查找成功显示查找元素,否则显示查找失败。④ 在该顺序表中删除或插入指定元素。⑤ 建立两个按值递增有序的顺序表,将他们合并成一个按值递增有序的顺序表。<pre name="code" class="html">#include<stdio.h>#include<stdlib.h>typedef struct{ int *a; int length; int listsize;}sq_list;void initList(sq_list &p){int n;scanf("%d",&n);p.length=n;p.listsize=100; p.a=(int*)malloc(p.listsize*sizeof(int)); //开辟最大存储空间 if (!p.a) exit(0); for (int i=1;i<=n;i++) scanf("%d",&p.a[i]);}void search(sq_list p,int e){int i=1;while (p.a[i]!=e&&i<=p.length){i++;}if (i>p.length)printf("无此值\n");else printf("%d\n",i);}void bianli(sq_list p) //遍历一遍,输出数值{int i; for (i=1;i<=p.length;i++)printf("%d ",p.a[i]); printf("\n");}void insert(sq_list &p,int i,int e){ int k; if (i<1||i>p.length+1) exit(0); if (p.length>=p.listsize) { p.a=(int*)realloc(p.a,(p.length+p.listsize)*(sizeof(int))); p.listsize+=p.length; if (!p.a) exit(0); } p.length++; for (k=p.length-1;k>=i;k--) p.a[k+1]=p.a[k]; p.a[i]=e;}void dele(sq_list &p,int i,int &e){ int k; if (i<1||i>p.length) exit(0); e=p.a[i]; printf("%d\n",e); for (k=i;k<p.length;k++) p.a[k]=p.a[k+1]; p.length--;}void hebing(sq_list p,sq_list q){ sq_list m;int c=0,k,i,j; m.length=p.length+q.length;m.listsize+=2*(p.length+q.length);m.a=(int*)malloc(m.listsize*sizeof(int));for (i=1;i<=p.length;i++)for (j=1;j<=q.length;j++)if (p.a[i]==q.a[j])c++;c=m.length-c;k=c;i=p.length;j=q.length; while (i>0&&j>0){ if (p.a[i]>q.a[j]) { m.a[c--]=p.a[i--]; } if (p.a[i]<q.a[j]) { m.a[c--]=q.a[j--]; } if (p.a[i]==q.a[j]) { m.a[c--]=p.a[i--]; j--; }} while (i>0) { m.a[c--]=p.a[i--]; } while (j>0) { m.a[c--]=q.a[j--]; } for (i=1;i<=k;i++) printf("%d ",m.a[i]); printf("\n");}int main(){ sq_list p,q; int i,e; initList(p);bianli(p);scanf("%d",&e);search(p,e);scanf("%d %d",&i,&e); insert(p,i,e);bianli(p);scanf("%d",&i);dele(p,i,e);bianli(p);initList(p);initList(q);hebing(p,q);return 0;}
代码如果收什么错误的话,欢迎积极评论
0 0
- 《数据结构》实验一 (1)
- 数据结构实验一(1)
- 数据结构实验一(第1题)
- 《数据结构》实验一 (2)
- 《数据结构》实验一 (3)
- 数据结构实验一(2)
- 数据结构实验报告(一)
- 《数据结构》实验一 实验报告
- 数据结构 实验一实验报告
- 数据结构实验一 实验报告
- 实验报告--数据结构实验一
- 数据结构实验一(实验报告)
- 《数据结构实验一》实验报告
- 数据结构和算法分析实验(一)
- 单链表操作(数据结构实验一)
- 数据结构(实验一)第2题
- 数据结构实验一(第3题)
- 数据结构课程上机实验题(一)
- HDU 5524 Subtrees
- python 文件读写查找、替换相关简单操作
- BestCoder Round #61 (div.2) HDU5523 Game
- git 服务器搭建 for linux
- HDU-5512 Pagodas(GCD)
- 数据结构实验一(1)
- Http Chunked Transfer Coding
- javaScript对象详解
- Java生产者消费者例子尝试
- HDU 5510 Bazinga(思维)
- 通过rsync+inotify实现服务器之间数据的实时备份
- Java学习心得(1)——强类型语言和布尔类型
- Ubuntu 14.04 安装Firefox的Flash插件
- 使用osm2pgsql工具将osm数据导入postgis中(图解)