1、j;Prin tf(%d n,ai);2.2代码:INSERT(L,i,b)。void In Sert(L in klist &L,i nt i,elemtype x)if(!L)L=(Li nklist)malloc(sizeof(L no de);(*L).data=x;(*L). next=NULL;if(i=1)S=(Li nklist)malloc(sizeof(L no de); s-data=x;S-n ext=L;L=s;P=L;j=1;WhiIe(P&jn ext;if(pji-1) return error;n ext=p-P-n ext=s;2.3代码:typedef i
2、nt elemtypetypedef StrUCt IinknOdeelemtype data;StrUCt IinknOde *n ext;no detype;no detype *create()elemtype d;n OdetyPe h=NULL,*s,*t;int i=1;建立单链表:while(1)输入第%d个结点数据域,i);SCan f(%dd);if(d=O)break;h=(no detype *)malloc(sizeof( no detype); h-data=d;h-n ext=NULL;t=h;s=(no detype *)malloc(sizeof( no det
3、ype);t- t=s;return h;void Sat (no detype *h,i nt a)no detype *p=h;WhiIe(P!=NULL)ap-data+;void mai n()int aN+1,i;N;ai=0;no detype *head;head=create();sat(head,a);候选人:); for(i=1;=N;i+) Printf(%3d Printf(n 得票数 nfor(i=1;,ai);4.实验小结线性表是最简单的、最常用的一种数据结构,是实现其他数据结构的基础实验二 栈与队列1.实验要求1.1 了解栈和队列的特性,以便灵活运用。1.2熟练掌
4、握栈和有关队列的各种操作和应用。2.实验内容2.1设一个算术表达式包括圆括号,方括号和花括号三种括号,编写一个算 法判断其中的括号是否匹配。3.实验代码2.1代码:#i ncludestri ng.h#defi ne NULL 0typedef StrUCt listChar str;StrUCt list *n ext;list;void PUSh(Char,list *);int POP(Char.list *);void deal(char *str);mai n(void)Char str20;n请输入一个算式:ngets(str);deal(str);正确!getchar();ret
5、urn 0;void deal(char *str)list *L;L=(IiSt *)malloc(sizeof(list);错误!exit(-2);L- next=NULL;while(*str)if(*str=(*str=) push(*str,L);) if(pop(*str,L) puts(错误请检查! puts(按回车键退出 getchar();sM+;if(L- next)PUtSe错误,请检查!puts(按任意键退出void PUSh(Char c,list *L)list *p;P=(IiSt *)malloc(sizeof(list);p)p-str=c;n ext=L-n
6、 ext=p;#define CheCk(S) if(L-next-str=s)p=l-next;next=p-free(p);return(0); int POP(Char c,list *L) next=NULL)return 1;SWitCh(C)case:check() break;CaSeCheCk(T) break;return 1;栈和队列是最基础的一种数据结构之一,为实现其他数据结构的奠定基石实验三树1.1掌握二叉树,二叉树排序数的概念和存储方法。1.2掌握二叉树的遍历算法。1.3熟练掌握编写实现树的各种运算的算法。2.1编写程序,求二叉树的结点数和叶子数。2.2编写递归算法,
7、求二叉树中以元素值为 X的结点为根的子数的深度2.3编写程序,实现二叉树的先序,中序,后序遍历,并求其深度。StrUCt no deChar data;StrUCt node *lchild,*rchild;bno de;typedef StrUCt node *bli nk;bli nk CreatObli nk bt;Char ch;ch=getchar();if(ch= ) return(NULL);bt=(struct node *)malloc(sizeof(b no de); bt-data=ch;bt-lchild=creat();rchild=creat();return bt
8、;int n=0 ,n 1=0;void PreOrder(bli nk bt)if (bt)n+;if(bt-lchild=NULL&rchild=NULL) n 1+;PreOrder(bt-lchild);rchild);bli nk root;root=creat();PreOrder(root);此二叉数的接点数有:%dn,n);此二叉数的叶子数有:,n1);int get_deep(bitree T,i nt x) _if(T-data=x),get_deep(T); exit 1;lchild)get_deep(T-lchild,x); if(T-rchild)get_deep(
9、T-rchild,x); _int get_depth(bitree T)T)return 0; m=get_depth(T- n=get_depth(T-return(m n?m:n )+1;bli nk Creato%c,bt-data);void ino rder(bli nk bt)if(bt)ino rder(bt-void postorder(bli nk bt)postorder(bt-int max(i nt x,i nt y)if(xy)return x;return y;int depth(bli nk bt)retur n 1+max(depth(bt-lchild),d
10、epth(bt-rchild);else return 0;VOid mai n()按先序排列:Pri ntf( n按中序排列:ino rder(root);按后序排列:Postorder(root);此二叉数的深度是:depth=%dn,depth(root);通过本章学习实验,对树有了初步的认识。树就是一种非线性的数据结构, 描述了客观世界中事物之间的层次关系。这种结构有着广泛的应用,一切具 有层次关系的问题都可以用树来表示。实验四图1.1熟悉图的各种存储方法。1.2掌握遍历图的递归和非递归的算法。1.3理解图的有关算法。2.1写出将一个无向图的邻接矩阵转换成邻接表的算法2.2以邻接表作存
11、储结构,给出拓扑排序算法的实现。VOid mattolist(int a,adjlist b,int n) /*n 为图的结点个数 */n;i+)bi.firstarc=NULL; /* 邻接表置空 */i+) /*逐行进行 */for(j=n-1;j=0;j-)if(aij!=0)p=(arcnodetp *)malloc(sizeof(arcnodetp);/* 产生邻接点 */ p-adjvex=j;n extare=bi.firstare;bi.firstarc=p; typedef StrUCt VeX nodeVerteXTyPe vertex;int in;/*增加一个入度域*/
12、AreCNOdeTP * fristarc;AdjListv num;typedef StrUCt graphAdjLiSt adjlist;int VeX nu m,arc num;GraphTp;Top_SOrt(GraPh TP g)LStaCkTP *p;/*建立入度为O的顶点栈S*/int m,i,v;ini tStack(S);g.vex nu m;if(g.adjlisti.i n=0)*if(w 的入度=0)*/PuSh(S,&v);/*W 入 S 栈*/m=0;whlie(!EmptyStack(S)Pop(S,&v)S 出栈-v,v);/* 输出 v*/m+;p=g.adj
13、listi.fristarc;/*P=图 g 中顶点 V 的第一个邻接点 */=NULL)/p 存在(g.adjlistp-adjvex.in)-;/*P 的入度-*/ if(g.adjlistp-adjvex.i n=0)*if(p 的入度=0)*/PUSh(S,p-adjvex);/*P 入 S 栈*/p=p-nextarc;/*P=图g中的顶点V的下一个邻接点*/if(m#include VString.h#defi ne MAXNUM 20int input(int *);/* 输入数据 */int SearCh(int *,int,int);/* 查找插入位置 */void plug
14、(int *,int,int);/* 插入数据 */void main (VOid)int dataMAXNUM,m;int in Sert=1;m=i nput(data);I nput the in Sert nu m:,data);inSert=SearCh(data,1,m);/*返回插入位置 */plug(data,i nsert,m);for(insert=1;inserthigh) return low;/*没有找到插入数据,返回low*/ elsemid*/mid=(low+high)2;if(*(data+mid)=*data) retun mid;/* 找到插入数据,返回e
15、lse if(*(data+mid)SearCh(data,low,high);void plug(i nt *data,i nt in sert,i nt m)int i;for(i=m;ii nsert;i-)*(data+i+1)=*(data+i);(data+in Sert)=*data#in elude VCOni o.h#include #definr N 18 /* 元素个数 */#defi nr Block num 3 /* 分块数 */typedef StrUCt in dextermint key;/*最大关键字*/int addr;/*块的起始地址*/index; /*
16、索引表数据类型*/index * CreateList(int data,int n)/* 建索引表 */in dex *p;int m,j,k;m=n/BlockNum;/*分为BlockNum块,每块有 m个元素*/p=(i ndex *)malloc(BlockNum *sizeof(i ndex);for(k=O;kkey=dat am*k;addr=m*k;for(j=m*k;m*k+m;j+)if(datajkey)key=dataj;/* 块的最大关键字 */return p;int BlockSearch(index *list,int rectab,int n,int m,i
17、nt k)/* 分块查找 */int low=0,high=m-1,mid,i;int b=n/m;/*每块有b个元素*/WhiIe(IOWV=high)* 块间折半查找 */if(list+mid)-key=k)high=mid+1;else low=mid+1;if(lowaddr;=(list+low)-adder+b-1 &rectabi!=k;i+);if(iaddr+b-1)return i;else return -1;return -1;in t recordN=22,12,13,8,9,20,33,42,44,38,24,48,60,58,74,49,86,53;in dex *list;please in PUt key:SCa nf(key);IiSt=CreateLiSt(record,N);data postion id %dn,BlockSearch(list,record,N,BlockNum,key);通过本章的学习,对排序有较高层次的理解与认识,从平时的练习中可以 看出排序是数据处理中经常用到的重要运算。 有序的顺序表可以采用查找 效率较高的折半查找法,而无序的顺序表只能用效率较低的顺序查找法。
copyright@ 2008-2023 冰点文库 网站版权所有
经营许可证编号:鄂ICP备19020893号-2