




免费预览已结束,剩余11页可下载查看
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
数据结构编程实例1. 顺序表的基本操作#define len 100 typedef struct sqlist intalen;intlength;void init(struct sqlist *sq)/* 初始化 */inti;for (i=0;iai=0;sq-length=0;void creat(struct sqlist *sq)/* 建顺序表 */inti;printf(please input length); scanf(%d,&sq-length);printf(please input %d numsn,sq-length);for (i=1; ilength;i+) scanf(%d,&sq-ai);精品资料void print(struct sqlist *sq)/*输出顺序表 */ inti;for(i=1; ilength;i+) printf( %d,sq-ai);printf(n);voidinsert(struct sqlist *sq,intpos, int x)/* 顺序表插入元素 */inti;for (i=sq-length;i=pos;i-) sq-ai+1=sq-ai;sq-apos=x;sq-length=sq-length+1;int delete(struct sqlist *sq,intpos)/*顺序表删除元素 */inti,x;x=sq-apos;for (i=pos+1;ilength;i+) sq-ai-1=sq-ai;sq-length=sq-length-1; return(x);main()int position,x; struct sqlist *list;struct sqlistslist; intxz=0;list =&slist; while (1) printf(1.initn);printf(2.creatn); printf(3.insertn); printf(4.deleten); printf(5.locate_valuen); printf(6.locate_posn); printf(7.printn); printf(0.exitn);printf(please inputyour choice); scanf(%d,&xz);switch(xz)case 1:init(list);break; case 2:creat(list);break;case3:printf(pleastinputinsetposition(pos)and value(x);scanf(%d%d,&position,&x);if (positionlist-length+1|list-length=len) printf(position errorn);else insert(list,position,x); break;case 4:printf(pleast inputdelete position(pos); scanf(%d,&position);if (positionlist-length|list-length=0)printf(position errorn); elseprintf(deleteposition=%d,deletedata=%dn,position,delete(list,position);break; case 5:;case 6:;case 7:print(list);break; case 0:exit(0); 2. 三种方法建立链表#include typedef struct nodeint data;struct node *link;node;node *creat1()/*按输入数据的顺序建立链表,输入数据通过个数控制*/int i,data,n;node *h=null,*p,*last=null; printf(please input the num:); scanf(%d,&n);printf(please input %d datas:,n); for (i=1;idata);if (i=1) h=p;else last-link=p; last=p;last-link=null; return(h);node *creat2()/*按输入数据的逆序建立链表,输入数据以0 结束 */int data;node *h=null,*p;printf(please input datas(0 end)n); scanf(%d,&data);while (data)p=(node*) malloc (sizeof (node); p-data=data;if (h=null) h=p; h-link=null;else p-link=h; h=p; scanf(%d,&data);return(h);node *creat3()/*按输入数据的大小顺序建立带头结点的链表,输入数据以0 结束*/int data;node *h,*p,*q,*r;h=(node*) malloc (sizeof (node); h-link=null; printf(please input datas(0 end)n); scanf(%d,&data);while (data)p=(node*) malloc (sizeof (node); p-data=data; p-link=null;if (h-link=null) h-link=p;elser=h; q=r-link;while(p-dataq-data & q)r=q; q=q-link; if (q)p-link=q; r-link=p;scanf(%d,&data);return(h-link);main()node *h,*p; int x;doprintf(=n);printf(1.zhengxujianlianbiaon);printf(2.nixujianlianbiaon); printf(3.jianliyouxulianbiaon); printf(0.tuichun); printf(=n);printf(please input your chosice);scanf(%d,&x); switch(x)case 1: h=creat1();break;case 2: h=creat2();break;case 3: h=creat3();break;case 0: return;p=h; while (p)printf(%5d,p-data);p=p-link;printf(nn);while (x);3. 试写出逆转线性单链表的算法要逆转一个线性单链表,只需从头指针指向的结点开始扫描该链表, 在扫描过程中改变各结点的指针(由指向后件改为指向原来的前件) 即可。struct node/*et 位数据元素类型 */etd;struct node *next;invlst(head)struct node*head ; struct node*p, *q, *r ;if(*head=null) return; p=*head; q=p-next;p-next=null;while (q!=null)r=q-next; q-next=p; p=q; q=r;*head=p; return;4. 设有两个有序线性单链表,头指针分别为 ah 和 bh。试写出将两个有序线性单链表合并为一个头指针为 ch 的有序线性单链表的算法,要求去掉重复元素。struct node/*et 位数据元素类型 */etd;struct node *next;#include“stdio.h ” mglst1(ah,bh,ch) struct node ah,bh,*ch;struct node *i, *j, *k, *p; et x;i=ah; j=bh; *ch=null; k=null;while (i!=null)&(j!=null)if (j-d=i-d)x=i-d; i=i-next; else x=j-d; j=j-next;if (*ch=null)p=(struct node *) malloc (sizeof(struct node); p-d=x; *ch=p; k=p;elseif (d!=k-d)p=(struct node *) malloc (sizeof(struct node);p-d=x; k-next=p; k=p; if (j=null)while (i!=nultructl)x=i-d; i=i-next; if (*ch=null)p=(struct node *) malloc (sizeof(struct node); p-d=x; *ch=p; k=p;else if (d!=k-d)p=(struct node *) malloc (sizeof(struct node); p-d=x; k-next=p; k=p;elsewhile (j!=null)x=j-d; j=j-next; if (*ch=null)p=(struct node *) malloc (sizeof(struct node); p-d=x; *ch=p; k=p;else if (d!=k-d)p=(struct node *) malloc (sizeof(struct node); p-d=x; k-next=p; k=p;if (k!=null) k-next=null; return;5. 试编写在二叉排序树中插入一个元素的算法。 include“stdlib.h ”struct btnodeet d;structbtnode *lchild; structbtnode *rchild;struct btnode *insort(bt,b) struct btnode *bt;et b;struct btnode *p, *q;p=(struct btnode *)malloc (sizeof(struct btnode); p-d=b; p-lchild=null; p-rchild=null;q=bt;if (q=null) bt=p; elsewhile (q-lchild!=p) & (q-rchild!=p)if (bd)if (q-lchild!=null) q=q-lchild; elseq-lchild=p;elseif (q-rchild!=null) q=q-rchild; elseq-rchild=p;return(bt);6. 先序(递归)建立二叉树并中序(递归)输出。#include typedef struct bitreechardata;struct bitree *lchild,*rchild;btree;btree*creatree()btree*t;charch; scanf(%c,&ch);if (ch= )t=null;elset=(btree*) malloc (sizeof(btree); t-data
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 上海市第一人民医院招聘考试真题2024
- 钢带考试题及答案
- 现代金融基础习题(附答案)
- 道路危险货物运输从业人员岗前三级培训考核试题(附答案)
- 非高危行业安全员培训考试试题及答案
- 2025版公共设施维护与维修合同模板
- 2025拆旧房屋拆除工程拆除作业拆除物运输合同范本
- 2025年度房产开发公司员工劳动合同范本
- 2025年度平面广告设计制作及版权授权合同
- 2025版滩涂地海上风电土地承包使用合同
- 活动成都热波zebra音乐节营销策划方案5月1日5月3日
- 四链融合:新质生产力的深度路径
- 2024年(IPA)国际注册对外汉语教师资格认证考试真题卷(含答案)
- 2025年中山市三角镇人民政府所属事业单位招聘事业单位人员模拟试卷及1套完整答案详解
- 云南省楚雄彝族自治州佳汇公证处招聘公证员笔试模拟试题参考答案详解
- 2025至2030年中国电力巡检无人机行业市场竞争格局及投资前景展望报告
- 食用菌工厂化种植基地建设方案
- 起重机械安全装置知识学习
- 2025年赛力斯入职测试题及答案
- 乡镇卫生院医师三基考试理论综合试题及答案
- 脑供血不足病人的护理查房-课件
评论
0/150
提交评论