课题数据结构课程设计报告书Word文档下载推荐.docx

上传人:b****5 文档编号:21268794 上传时间:2023-01-29 格式:DOCX 页数:24 大小:136.91KB
下载 相关 举报
课题数据结构课程设计报告书Word文档下载推荐.docx_第1页
第1页 / 共24页
课题数据结构课程设计报告书Word文档下载推荐.docx_第2页
第2页 / 共24页
课题数据结构课程设计报告书Word文档下载推荐.docx_第3页
第3页 / 共24页
课题数据结构课程设计报告书Word文档下载推荐.docx_第4页
第4页 / 共24页
课题数据结构课程设计报告书Word文档下载推荐.docx_第5页
第5页 / 共24页
点击查看更多>>
下载资源
资源描述

课题数据结构课程设计报告书Word文档下载推荐.docx

《课题数据结构课程设计报告书Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《课题数据结构课程设计报告书Word文档下载推荐.docx(24页珍藏版)》请在冰豆网上搜索。

课题数据结构课程设计报告书Word文档下载推荐.docx

while(c!

=0)

{

s=(node*)malloc(sizeof(node));

s->

coef=c;

exp=e;

r->

next=s;

r=s;

}

next=NULL;

return(h);

}

voidarrange(node*pa)

{

node*h=pa,*p,*q,*r;

for(p=pa;

p->

next!

=NULL;

p=p->

next);

r=p;

for(h=pa;

h->

=r;

{

for(p=h;

=r&

&

p!

next)

if((p->

next)->

exp>

(p->

next->

exp)

q=p->

next;

p->

next=q->

q->

next=p->

next=q;

}

}

voidneipai(node*head)

{node*p,*q,*r,*Q;

p=head;

if(head->

=NULL)

for(q=p->

q!

q=q->

next)

for(p=q->

next,r=q;

if(q->

exp==p->

coef=q->

coef+p->

coef;

Q=p;

free(Q);

else

r=r->

p=p->

}}

voidinsert(node*head,node*s)

node*pre,*p;

pre=head;

p=pre->

while(p!

if(p->

exp>

exp)break;

pre=p;

next=p;

pre->

node*copyList(node*head)

node*l=NULL,*newHead;

newHead=(node*)malloc(sizeof(node));

newHead->

next=NULL;

head=head->

while(head!

=NULL)

l=(node*)malloc(sizeof(node));

l->

coef=head->

exp=head->

exp;

insert(newHead,l);

returnnewHead;

voidprint(node*p)

while(p->

=NULL)

%d*x^%d"

p->

coef,p->

exp);

voidpolyadd(node*ha,node*hb)

node*p,*q,*pre,*temp;

intsum;

p=ha->

q=hb->

pre=ha;

=NULL&

exp==q->

sum=p->

coef+q->

if(sum!

coef=sum;

pre=pre->

temp=q;

free(temp);

temp=p->

free(p);

p=temp;

temp=q->

free(q);

q=temp;

elseif(p->

exp<

q->

pre=pre->

else

q=q->

if(p!

voidpolysub(node*ha,node*hb)

node*p,*q,*pre,*temp,*x;

x=q;

while(x!

=null)

{x->

coef=-x->

x=x->

exp)

voidmain()

node*ha,*hb,*hc,*hd;

pleaseinputthecoefandexpofha:

\n"

ha=create();

arrange(ha);

neipai(ha);

hc=copyList(ha);

print(ha);

pleaseinputthecoefandexpofhb:

hb=create();

arrange(hb);

neipai(hb);

hd=copyList(hb);

print(hb);

addis:

polyadd(ha,hb);

subis:

polysub(hc,hd);

print(hc);

运行结果

7、猴子选大王

一堆猴子都有编号,编号是1,2,3...m,这群猴子(m个)按照1-m的顺序围坐一圈,从第1开始数,每数到第N个,该猴子就要离开此圈,这样依次下来,直到圈中只剩下最后一只猴子,则该猴子为大王。

要求:

输入数据:

输入m,nm,n为整数,n<

m

输出形式:

中文提示按照m个猴子,数n个数的方法,输出为大王的猴子是几号,建立一个函数来实现此功能

#include<

typedefstructmonkey

intnum;

structmonkey*next;

}Monkey,*LINK;

LINKp,head,p2;

inti,m,n;

Inputm:

m);

Inputn(n<

m):

n);

head=p=p2=(LINK)malloc(sizeof(Monkey));

//三个指针指向同一个内存单元

for(i=1;

i<

m;

i++)

p=(LINK)malloc(sizeof(Monkey));

p2->

p2=p;

next=head;

//把链表的首尾相连

p=head;

//p指向了第一个结点

putthesortednumbertothemonkey!

//对猴子进行编号

=m;

num=i;

//从第一个结点到最后一个结点一次给猴子编号

the%dmonkey:

%d\n"

num,p->

num);

}//循环结束,p指向了最后一个结点

i=0;

//再把p指向第一个结点

while

(1)

i++;

the%dnumbermonkeyshoutedout:

num,i);

//这只猴子报号

if(p->

next==p)

break;

//此为while循环的出口

if(i==n)//if语句中是删除结点的过程

the%dnumbermonkeyisoutthecircle\n"

//这只猴子被淘汰

//在此删除结点p

p=p2->

continue;

else

if(i==n-1)

//保存将要退出结点的前一个结点(存到p2中)

themonkeyisthewinner:

//这只猴子胜出

运行结果:

马鹏起20080596

8.建立二叉树,后序、先序遍历(用递归或非递归的方法都可以)

要求能够输入树的各个结点,并能够输出用不同方法遍历的遍历序列;

分别建立二叉树存储结构的输入函数、输出后序遍历序列的函数、输出先序遍历序列的函数;

#include<

#definebitreptrstructtype1/*二叉树及其先序边历*/

#definenull0

#definelensizeof(bitreptr)

bitreptr*bt;

intf,g,n;

bitreptr/*二叉树结点类型说明*/

chardata;

bitreptr*lchild,*rchild;

};

preorder(bitreptr*bt)/*先序遍历二叉树*/

if(g==1)printf("

先序遍历序列为:

g=g+1;

if(bt)

%6c"

bt->

data);

preorder(bt->

lchild);

rchild);

elseif(g==2)printf("

空树\n"

bitreptr*crt_bt()/*建立二叉树*/

charch;

if(f==1)printf("

输入根结点,#表示结束\n"

elseprintf("

输入结点,#表示结束\n"

\n%c"

ch);

f=f+1;

if(ch=='

#'

)bt=null;

bt=(bitreptr*)malloc(len);

bt->

data=ch;

%c左孩子"

lchild=crt_bt();

%c右孩子"

rchild=crt_bt();

return(bt);

postorder(bitreptr*bt)/*后序遍历*/

if(n==1)printf("

后序遍历序列为:

n=n+1;

postorder(bt->

elseif(n==2)printf("

main()

{f=1;

g=1;

n=1;

bt=crt_bt();

preorder(bt);

postorder(bt);

任杰20080535

6、joseph环

编号是1,2,……,n的n个人按照顺时针方向围坐一圈,每个人只有一个密码(正整数)。

一开始任选一个正整数作为报数上限值m,从第一个人开始顺时针方向自1开始顺序报数,报到m时停止报数。

报m的人出列,将他的密码作为新的m值,从他在顺时针方向的下一个人开始重新从1报数,如此下去,直到所有人全部出列为止。

设计一个程序来求出出列顺序。

利用单向循环链表存储结构模拟此过程,按照出列的顺序输出各个人的编号。

测试数据:

m的初值为20,n=7,7个人的密码依次为3,1,7,2,4,7,4,首先m=6,则正确的输出是什么?

建立输入处理输入数据,输入m的初值,n,输入每个人的密码,建立单循环链表。

建立一个输出函数,将正确的输出序列

string.h>

/*结构体和函数声明*/

typedefstruct_node_t

intn_num;

struct_node_t*next;

}node_t;

node_t*node_t_create(intn);

node_t*node_t_get(node_t**pn,intm);

/*功能函数实现*/

/*

*name:

node_t_create

*params:

*n[in]输入要构造的链表的个数

*return:

*返回构造成功的环形单向链表指针

*notes:

*构造节点数量为n的环形单向链表

*

*/

node_t*node_t_create(intn)

node_t*p_ret=NULL;

if(0!

=n)

intn_idx=1;

node_t*p_node=NULL;

/*构造n个node_t*/

p_node=(node_t*)malloc(n*sizeof(node_t));

if(NULL==p_node)

returnNULL;

memset(p_node,0,n*sizeof(node_t));

/*内存空间申请成功*/

p_ret=p_node;

for(;

n_idx<

n;

n_idx++)

p_node->

n_num=n_idx;

next=p_node+1;

p_node=p_node->

n_num=n;

next=p_ret;

returnp_ret;

main

*none

*int

*mainfunction

intmain()

intn,m;

node_t*p_list,*p_iter;

inputm:

inputn:

/*构造环形单向链表*/

p_list=node_t_create(n);

/*Josephus循环取数*/

p_iter=p_list;

m%=n;

while(p_iter!

=p_iter->

inti=1;

/*取到第m-1个节点*/

i<

m-1;

i++)

p_iter=p_iter->

/*输出第m个节点的值*/

p_iter->

n_num);

/*从链表中删除第m个节点*/

p_iter->

next=p_iter->

/*释放申请的空间*/

free(p_list);

9、赫夫曼树的建立

任务:

建立建立最优二叉树函数

可以建立函数输入二叉树,并输出其赫夫曼树

存储结构、基本算法(可以使用程序流程图)、输入输出、源程序、测试数据和结果、算法的时间复杂度、另外可以提出算法的改进方法;

#defineLENsizeof(structHTnode)

inti,l,n,w=0,c,start,a1,a2,f;

structHTnode{unsignedintweight;

unsignedintparent,lchild,rchild;

}*p,*HT;

typedefchar**Huffmancode;

HuffmancodeHC;

char*cd;

select()

{intk=1,j,flag=0;

while((HT+k)->

parent!

=0)k++;

for(j=k+1;

j<

=n;

j++,flag=0)

{if((HT+j)->

=0)flag=1;

if((HT+j)->

weight==0)flag=1;

if(!

flag){if((HT+j)->

weight<

(HT+k)->

weight)k=j;

return(k);

{printf("

\n赫夫曼树的建立:

请输入权值(叶子)数目:

l);

while(l<

1){printf("

输入错误,请重新输入权值数目:

scanf("

if(l==1)printf("

\n只有一个权值,无须建立赫夫曼树!

else{n=2*l-1;

HT=(structHTnode*)malloc((n+1)*LEN);

请按对应顺序输入权值(输入一权值,键入一回车):

for(i=1,p=HT+1;

=l;

++i,++p)

{scanf("

w);

while(w<

=0){printf("

权值错,重新输入此权值:

weight=w;

parent=0;

lchild=0;

rchild=0;

for(i=l+1;

{p->

weight=0;

++i)

{a1=select();

(HT+a1)->

parent=i;

a2=select();

(HT+a2)->

(HT+i)->

lchild=a1;

rchild=a2;

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;

{start=l-1;

for(c=i,f=(HT+i)->

parent;

f!

=0;

c=f,f=(HT+f)->

parent)

if((HT+f)->

lchild==c)*(cd+(--start))='

0'

else*(cd+(--start))='

1'

*(HC+i)=(char*)malloc((l-start)*sizeof(char));

strcpy(*(HC+i),(cd+start));

\n对应的二进制赫夫曼编码为:

\n

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 表格模板 > 调查报告

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1