1、数据结构课程设计报告书北方民族大学课程设计课程名称: 数据结构与算法课程设计 院(部)名 称: 信息与计算科学学院组长姓名: 金龙龙 20080544同组人员姓名: 任杰 20080535马鹏起 20080596赵俞军 20080576赵庆康 20080570设 计 时 间: 2015.6.1-2015.6.24金龙龙 200805442、 一元多项式计算任务:能够按照指数降序排列建立并输出多项式;能够完成两个多项式的相加、相减,并将结果输入;在上交资料中请写明:存储结构、多项式相加的基本过程的算法(可以使用程序流程图) 、源程序、测试数据和结果、算法的时间复杂度、另外可以提出算法的改进方法;
2、存储结构:链表存储 #include#include#include#define null 0typedef struct polynode int coef; int exp; struct polynode *next;node;node *create() node *h,*r,*s; int c,e; h=(node*)malloc(sizeof(node); r=h; printf(coef:); scanf(%d,&c); printf(exp: ); scanf(%d,&e); while(c!=0) s=(node*)malloc(sizeof(node); s-coef=c
3、; s-exp=e; r-next=s; r=s; printf(coef:); scanf(%d,&c); printf(exp: ); scanf(%d,&e); r-next=NULL; return(h);void arrange(node *pa) node *h=pa,*p,*q,*r; for(p=pa;p-next!=NULL;p=p-next); r=p; for(h=pa;h-next!=r;) for(p=h;p-next!=r&p!=r;p=p-next) if(p-next)-exp(p-next-next)-exp) q=p-next-next; p-next-ne
4、xt=q-next; q-next=p-next; p-next=q; r=p; void neipai(node *head) node *p,*q,*r,*Q; p=head; if(head-next-next!=NULL) for(q=p-next;q!=NULL;q=q-next) for(p=q-next,r=q;p!=NULL;) if(q-exp=p-exp) q-coef=q-coef+p-coef; r-next=p-next; Q=p;p=p-next; free(Q); else r=r-next; p=p-next; void insert(node *head,no
5、de *s) node *pre,*p; pre=head; p=pre-next; while(p!=NULL) if(p-exp s-exp) break; pre=p; p=p-next; s-next=p; pre-next=s; node *copyList(node *head) node *l = NULL, *newHead; newHead = (node *) malloc(sizeof(node); newHead-next = NULL; head = head-next; while (head != NULL) l = (node *) malloc(sizeof(
6、node); l-coef = head-coef; l-exp = head-exp; insert(newHead, l); head = head-next; return newHead;void print(node *p) while(p-next!=NULL) p=p-next; printf( %d*x%d,p-coef,p-exp); void polyadd(node *ha, node *hb) node *p,*q,*pre,*temp; int sum; p=ha-next; q=hb-next; pre=ha; while(p!=NULL&q!=NULL) if(p
7、-exp=q-exp) sum=p-coef+q-coef; if(sum!=0) p-coef=sum; pre-next=p;pre=pre-next;p=p-next; temp=q;q=q-next;free(temp); else temp=p-next;free(p);p=temp; temp=q-next;free(q);q=temp; else if(p-expexp) pre-next=p; pre=pre-next; p=p-next; else pre-next=q; pre=pre-next; q=q-next; if(p!=NULL) pre-next=p; else
8、 pre-next=q; void polysub(node *ha,node *hb) node *p,*q,*pre,*temp,*x; int sum; p=ha-next; q=hb-next; x=q; pre=ha; while(x!=null) x-coef=-x-coef; x=x-next; while(p!=NULL&q!=NULL) if(p-exp=q-exp) sum=p-coef+q-coef; if(sum!=0) p-coef=sum; pre-next=p;pre=pre-next;p=p-next; temp=q;q=q-next;free(temp); e
9、lse temp=p-next;free(p);p=temp; temp=q-next;free(q);q=temp; else if(p-expexp) pre-next=p; pre=pre-next; p=p-next; else pre-next=q; pre=pre-next; q=q-next; if(p!=NULL) pre-next=p; else pre-next=q;void main() node *ha,*hb,*hc,*hd; printf(please input the coef and exp of ha:n); ha=create(); arrange(ha)
10、; neipai(ha); hc=copyList(ha); print(ha); printf(n); printf(please input the coef and exp of hb:n); hb=create(); arrange(hb); neipai(hb); hd=copyList(hb); print(hb); printf(n); printf(add is :n); polyadd(ha,hb); print(ha); printf(n); printf(sub is :n); polysub(hc,hd); print(hc); 运行结果赵俞军 200805767、 猴
11、子选大王任务:一堆猴子都有编号,编号是1,2,3 .m ,这群猴子(m个)按照1-m的顺序围坐一圈,从第1开始数,每数到第N个,该猴子就要离开此圈,这样依次下来,直到圈中只剩下最后一只猴子,则该猴子为大王。要求:输入数据:输入m,n m,n 为整数,nm输出形式:中文提示按照m个猴子,数n 个数的方法,输出为大王的猴子是几号 ,建立一个函数来实现此功能 #include #include typedef struct monkey int num; struct monkey *next; Monkey,*LINK; void main() LINK p,head,p2; int i,m,n;
12、printf(Input m:n);scanf(%d,&m);printf(Input n(nm):n);scanf(%d,&n);head=p=p2=(LINK)malloc(sizeof(Monkey); /三个指针指向同一个内存单元for(i=1;inext=p; p2=p; p2-next=head; /把链表的首尾相连p=head; /p指向了第一个结点printf(put the sorted number to the monkey!n); /对猴子进行编号for(i=1;inum=i; /从第一个结点到最后一个结点一次给猴子编号printf( the %d monkey:%dn
13、,p-num,p-num); p=p-next; /循环结束,p指向了最后一个结点i=0; p=head; /再把p指向第一个结点while(1) i+; printf( the %d number monkey shouted out:%dn,p-num,i);/这只猴子报号if(p-next=p) break; /此为while循环的出口if(i=n) /if语句中是删除结点的过程 i=0; printf( the %d number monkey is out the circlen,p-num); /这只猴子被淘汰printf(n); p2-next=p-next; /在此删除结点pp
14、=p2-next; continue; else if(i=n-1) p2=p; /保存将要退出结点的前一个结点(存到p2中)p=p-next; printf( the monkey is the winner :%d,p-num); /这只猴子胜出运行结果:马鹏起 200805968. 建立二叉树,后序、先序遍历( 用递归或非递归的方法都可以)任务:要求能够输入树的各个结点,并能够输出用不同方法遍历的遍历序列;分别建立二叉树存储结构的输入函数、输出后序遍历序列的函数、输出先序遍历序列的函数; # include# include # define bitreptr struct type1
15、/*二叉树及其先序边历*/# define null 0# define len sizeof(bitreptr)bitreptr *bt;int f,g,n;bitreptr /*二叉树结点类型说明*/ char data; bitreptr *lchild,*rchild; ;preorder(bitreptr *bt) /*先序遍历二叉树*/ if(g=1) printf(先序遍历序列为:n); g=g+1; if(bt) printf(%6c,bt-data); preorder(bt-lchild); preorder(bt-rchild); else if(g=2) printf(
16、空树n); bitreptr *crt_bt() /*建立二叉树*/ bitreptr *bt; char ch; if(f=1) printf(输入根结点,#表示结束n); else printf(输入结点,#表示结束n); scanf(n%c,&ch); f=f+1; if(ch=#) bt=null; else bt=(bitreptr *)malloc(len); bt-data=ch; printf(%c 左孩子,bt-data); bt-lchild=crt_bt(); printf(%c 右孩子,bt-data); bt-rchild=crt_bt(); return(bt);p
17、ostorder(bitreptr *bt) /*后序遍历*/ if(n=1) printf(后序遍历序列为:n); n=n+1; if(bt) postorder(bt-lchild); postorder(bt-rchild); printf(%6c,bt-data); else if(n=2) printf(空树n); main()f=1; g=1; n=1; bt=crt_bt(); preorder(bt); printf(n); postorder(bt);printf(n); 运行结果:任杰 200805356、 joseph环 任务:编号是1,2,,n的n个人按照顺时针方向围坐
18、一圈,每个人只有一个密码(正整数)。一开始任选一个正整数作为报数上限值m,从第一个人开始顺时针方向自1开始顺序报数,报到m时停止报数。报m的人出列,将他的密码作为新的m值,从他在顺时针方向的下一个人开始重新从1报数,如此下去,直到所有人全部出列为止。设计一个程序来求出出列顺序。要求:利用单向循环链表存储结构模拟此过程,按照出列的顺序输出各个人的编号。测试数据:m的初值为20,n=7 ,7个人的密码依次为3,1,7,2,4,7,4,首先m=6,则正确的输出是什么?要求:输入数据:建立输入处理输入数据,输入m的初值,n ,输入每个人的密码,建立单循环链表。输出形式:建立一个输出函数,将正确的输出序
19、列#include #include #include #include /* 结构体和函数声明 */typedef struct _node_t int n_num; struct _node_t *next; node_t;node_t *node_t_create(int n);node_t *node_t_get(node_t *pn, int m);/* 功能函数实现 */* * name: node_t_create * params: * n in 输入要构造的链表的个数 * return: * 返回构造成功的环形单向链表指针 * notes: * 构造节点数量为 n 的环形单向
20、链表 * */node_t * node_t_create(int n) node_t *p_ret = NULL; if (0 != n) int n_idx = 1; node_t *p_node = NULL; /* 构造 n 个 node_t */ p_node = (node_t *) malloc(n * sizeof(node_t); if (NULL = p_node) return NULL; else memset(p_node, 0, n * sizeof(node_t); /* 内存空间申请成功 */ p_ret = p_node; for (; n_idx n_num
21、 = n_idx; p_node-next = p_node + 1; p_node = p_node-next; p_node-n_num = n; p_node-next = p_ret; return p_ret;/* * name: main * params: * none * return: * int * notes: * main function */int main() int n, m; node_t *p_list, *p_iter; printf(input m:n); scanf(%d,&m); printf(input n:n); scanf(%d,&n); /*
22、 构造环形单向链表 */ p_list = node_t_create(n); /* Josephus 循环取数 */ p_iter = p_list; m %= n; while (p_iter != p_iter-next) int i = 1; /* 取到第 m-1 个节点 */ for (; i next; /* 输出第 m 个节点的值 */ printf(%dn, p_iter-next-n_num); /* 从链表中删除第 m 个节点 */ p_iter-next = p_iter-next-next; p_iter = p_iter-next; printf(%dn, p_ite
23、r-n_num); /* 释放申请的空间 */ free(p_list);运行结果:赵庆康 200805709、 赫夫曼树的建立 任务 :建立建立最优二叉树函数要求:可以建立函数输入二叉树,并输出其赫夫曼树在上交资料中请写明:存储结构、 基本算法(可以使用程序流程图) 、输入输出、源程序、测试数据和结果、算法的时间复杂度、另外可以提出算法的改进方法;#include #include#include#define LEN sizeof(struct HTnode)int i,l,n,w=0,c,start,a1,a2,f;struct HTnode unsigned int weight; u
24、nsigned int parent,lchild,rchild; *p,*HT;typedef char *Huffmancode;Huffmancode HC;char *cd;select()int k=1,j,flag=0; while(HT+k)-parent!=0) k+; for(j=k+1;jparent!=0) flag=1; if(HT+j)-weight=0) flag=1; if(!flag) if(HT+j)-weightweight) k=j; return(k);main()printf(n赫夫曼树的建立:n); printf(请输入权值(叶子)数目:); sca
25、nf(%d,&l); while(l1) printf(输入错误,请重新输入权值数目:); scanf(%d,&l); if(l=1) printf(n只有一个权值,无须建立赫夫曼树!); else n=2*l-1; HT=(struct HTnode*)malloc(n+1)*LEN); printf(请按对应顺序输入权值(输入一权值,键入一回车):n); for(i=1,p=HT+1;i=l;+i,+p) scanf(%d,&w); while(wweight=w; p-parent=0; p-lchild=0; p-rchild=0; for(i=l+1;iweight=0; p-par
26、ent=0; p-lchild=0; for(i=l+1;iparent=i; a2=select(); (HT+a2)-parent=i; (HT+i)-lchild=a1; (HT+i)-rchild=a2; (HT+i)-weight=(HT+a1)-weight+(HT+a2)-weight; HC=(Huffmancode)malloc(l+1)*sizeof(char *); cd=(char *)malloc(l*sizeof(char); *(cd+(l-1)=0; for(i=1;iparent;f!=0;c=f,f=(HT+f)-parent) if(HT+f)-lchild=c)
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1