版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、数据结构编程实例1 顺序表的基本操作#define LEN 100 typedef struct sqlist int aLEN;int length;void init(struct sqlist *sq) /* 初始化 */ int i;for (i=0;i<LEN;i+) sq->ai=0;sq->length=0;void creat(struct sqlist *sq) /* 建顺序表 */ int i;printf("please input length"); scanf("%d",&sq->length);
2、printf("please input %d numsn",sq->length); for (i=1; i<=sq->length;i+) scanf("%d",&sq->ai);void print(struct sqlist *sq) /* 输出顺序表 */ int i;for (i=1; i<=sq->length;i+)printf(" %d",sq->ai);printf("n");void insert(struct sqlist *sq,int p
3、os, int x) /* 顺序表插入元素 */ int i;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,int pos) /* 顺序表删除元素 */int i,x;x=sq->apos;for (i=pos+1;i<=sq->length;i+) sq->ai-1=sq->ai;sq->length=sq->length-1;return
4、(x);main() int position,x;struct sqlist *list;struct sqlist slist;int xz=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(&quo
5、t;7.printn");printf("0.exitn");printf("please input your choice");scanf("%d",&xz);switch(xz)case 1:init(list);break;case 2:creat(list);break;and value(x)");case 3:printf("pleast input inset position(pos)scanf("%d%d",&position,&x);if
6、(position<1|position>list->length+1|list->length>=LEN)printf("position errorn");else insert(list,position,x);break;case 4:printf("pleast input delete position(pos)");scanf("%d",&position);if (position<1|position>list->length|list->length=0
7、) printf("position errorn");elseprintf("delete position=%d,delete data=%dn",position,delete(list,position);break;case 5:;case 6:;case 7:print(list);break;case 0:exit(0); 2 三种方法建立链表#include <stdio.h>typedef struct nodeint data;struct node *link;NODE;NODE *creat1()/* 按输入数据的顺序
8、建立链表,输入数据通过个数控制 */ 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;i<=n;i+)p=(NODE*) malloc (sizeof (NODE); scanf("%d",&p->data);if (i=1) h=p;else last->link=
9、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=
10、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->da
11、ta=data; p->link=NULL;if (h->link=NULL) h->link=p;elser=h; q=r->link;while (p->data>q->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.zhengxujia
12、nlianbiaon");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=cr
13、eat3();break;case 0: return;p=h;while (p)printf("%5d",p->data);p=p->link;printf("nn");while (x);3 试写出逆转线性单链表的算法要逆转一个线性单链表,只需从头指针指向的结点开始扫描该链表,在扫描过程中改变各结点的指针(由指向后件改为指向原来的前件) 即可。Struct node/*ET 位数据元素类型 */ET d;struct node *next;invlst(head)struct node *head ; struct node *p, *q
14、, *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;AH禾口 BH。试写出CH 的有序线性单链4 设有两个有序线性单链表,头指针分别为 将两个有序线性单链表合并为一个头指针为 表的算法,要求去掉重复元素。Struct node/*ET 位数据元素类型 */ET d;struct node *next;#include “stdio.h”mglst1(ah,bh,ch)struct
15、 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;else if (d!=k->d)p=(struct
16、 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;
17、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”s
18、truct btnodeET d;struct btnode *lchild;struct btnode *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 (b<q->d)if (q->lchild!=NULL) q=q->lchild
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2023年驻马店辅警招聘考试题库及答案详解一套
- 2023年福州辅警招聘考试真题带答案详解(完整版)
- 2024年咸阳辅警协警招聘考试真题附答案详解(研优卷)
- 2024年大同辅警招聘考试真题及完整答案详解1套
- 2023年福建辅警协警招聘考试备考题库及答案详解(各地真题)
- 2024年内江辅警招聘考试题库附答案详解(能力提升)
- 2023年石家庄辅警协警招聘考试备考题库参考答案详解
- 2024年宜宾辅警协警招聘考试真题及完整答案详解
- 2025年江西省名校学术联盟高二化学第一学期期末综合测试试题含解析
- 北京语言大学《经济法1》2024-2025学年第一学期期末试卷
- 2025年宁东基地管委会公开招聘党建指导员考试笔试备考试题及答案解析
- 2025宁都县源盛公用事业投资发展有限公司招聘员工9人笔试考试备考题库及答案解析
- 中远海运集团介绍
- 阳城消防比武活动方案
- 基于stm32的老人健康监测系统设计
- 2025年基层社会治理教育培训考试试题及答案
- 2025版农药中毒常见症状及护理指导培训
- 人教版(2024)三年级上册数学称重大挑战课件
- 2025辽宁沈阳市和平区招聘社区工作者61人考试参考试题及答案解析
- 2025年山东钢铁集团有限公司社会招聘(4人)考试参考试题及答案解析
- 静物摄影基础知识培训课件
评论
0/150
提交评论