数据结构课程设计报告书.docx
《数据结构课程设计报告书.docx》由会员分享,可在线阅读,更多相关《数据结构课程设计报告书.docx(25页珍藏版)》请在冰豆网上搜索。
数据结构课程设计报告书
北方民族大学课程设计
课程名称:
数据结构与算法课程设计
院(部)名称:
信息与计算科学学院
组长姓名:
金龙龙20080544
同组人员姓名:
任杰20080535
马鹏起20080596
赵俞军20080576
赵庆康20080570
设计时间:
2015.6.1---2015.6.24
金龙龙20080544
2、一元多项式计算
任务:
能够按照指数降序排列建立并输出多项式;
能够完成两个多项式的相加、相减,并将结果输入;
在上交资料中请写明:
存储结构、多项式相加的基本过程的算法(可以使用程序流程图)、源程序、测试数据和结果、算法的时间复杂度、另外可以提出算法的改进方法;
存储结构:
链表存储
#include
#include
#include
#definenull0
typedefstructpolynode
{
intcoef;
intexp;
structpolynode*next;
}node;
node*create()
{
node*h,*r,*s;
intc,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;
s->exp=e;
r->next=s;
r=s;
printf("coef:
");
scanf("%d",&c);
printf("exp:
");
scanf("%d",&e);
}
r->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->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->next=q->next;
q->next=p->next;
p->next=q;
}
r=p;
}
}
voidneipai(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;
}
}}
voidinsert(node*head,node*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(node));
l->coef=head->coef;
l->exp=head->exp;
insert(newHead,l);
head=head->next;
}
returnnewHead;
}
voidprint(node*p)
{
while(p->next!
=NULL)
{
p=p->next;
printf("%d*x^%d",p->coef,p->exp);
}
}
voidpolyadd(node*ha,node*hb)
{
node*p,*q,*pre,*temp;
intsum;
p=ha->next;
q=hb->next;
pre=ha;
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);
}
else
{
temp=p->next;free(p);p=temp;
temp=q->next;free(q);q=temp;
}
}
elseif(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;
}
voidpolysub(node*ha,node*hb)
{
node*p,*q,*pre,*temp,*x;
intsum;
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);
}
else
{
temp=p->next;free(p);p=temp;
temp=q->next;free(q);q=temp;
}
}
elseif(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;
}
voidmain()
{
node*ha,*hb,*hc,*hd;
printf("pleaseinputthecoefandexpofha:
\n");
ha=create();
arrange(ha);
neipai(ha);
hc=copyList(ha);
print(ha);
printf("\n");
printf("pleaseinputthecoefandexpofhb:
\n");
hb=create();
arrange(hb);
neipai(hb);
hd=copyList(hb);
print(hb);
printf("\n");
printf("addis:
\n");
polyadd(ha,hb);
print(ha);
printf("\n");
printf("subis:
\n");
polysub(hc,hd);
print(hc);
}
运行结果
赵俞军20080576
7、猴子选大王
任务:
一堆猴子都有编号,编号是1,2,3...m,这群猴子(m个)按照1-m的顺序围坐一圈,从第1开始数,每数到第N个,该猴子就要离开此圈,这样依次下来,直到圈中只剩下最后一只猴子,则该猴子为大王。
要求:
输入数据:
输入m,nm,n为整数,n输出形式:
中文提示按照m个猴子,数n个数的方法,输出为大王的猴子是几号,建立一个函数来实现此功能
#include
#include
typedefstructmonkey
{
intnum;
structmonkey*next;
}Monkey,*LINK;
voidmain()
{
LINKp,head,p2;
inti,m,n;
printf("Inputm:
\n");
scanf("%d",&m);
printf("Inputn(n\n");
scanf("%d",&n);
head=p=p2=(LINK)malloc(sizeof(Monkey));//三个指针指向同一个内存单元
for(i=1;i{
p=(LINK)malloc(sizeof(Monkey));
p2->next=p;
p2=p;
}
p2->next=head;//把链表的首尾相连
p=head;//p指向了第一个结点
printf("putthesortednumbertothemonkey!
\n");//对猴子进行编号
for(i=1;i<=m;i++)
{
p->num=i;//从第一个结点到最后一个结点一次给猴子编号
printf("the%dmonkey:
%d\n",p->num,p->num);
p=p->next;
}//循环结束,p指向了最后一个结点
i=0;
p=head;//再把p指向第一个结点
while
(1)
{
i++;
printf("the%dnumbermonkeyshoutedout:
%d\n",p->num,i);//这只猴子报号
if(p->next==p)
break;//此为while循环的出口
if(i==n)//if语句中是删除结点的过程
{
i=0;
printf("the%dnumbermonkeyisoutthecircle\n",p->num);//这只猴子被淘汰
printf("\n");
p2->next=p->next;//在此删除结点p
p=p2->next;
continue;
}
else
{
if(i==n-1)
p2=p;//保存将要退出结点的前一个结点(存到p2中)
p=p->next;
}
}
printf("themonkeyisthewinner:
%d",p->num);//这只猴子胜出
}
运行结果:
马鹏起20080596
8.建立二叉树,后序、先序遍历(用递归或非递归的方法都可以)
任务:
要求能够输入树的各个结点,并能够输出用不同方法遍历的遍历序列;分别建立二叉树存储结构的输入函数、输出后序遍历序列的函数、输出先序遍历序列的函数;
#include
#include
#definebitreptrstructtype1/*二叉树及其先序边历*/
#definenull0
#definelensizeof(bitreptr)
bitreptr*bt;
intf,g,n;
bitreptr/*二叉树结点类型说明*/
{
chardata;
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);
}
elseif(g==2)printf("空树\n");
}
bitreptr*crt_bt()/*建立二叉树*/
{
bitreptr*bt;
charch;
if(f==1)printf("输入根结点,#表示结束\n");
elseprintf("输入结点,#表示结束\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);}
postorder(bitreptr*bt)/*后序遍历*/
{
if(n==1)printf("后序遍历序列为:
\n");
n=n+1;
if(bt)
{
postorder(bt->lchild);
postorder(bt->rchild);
printf("%6c",bt->data);
}
elseif(n==2)printf("空树\n");
}
main()
{f=1;
g=1;
n=1;
bt=crt_bt();
preorder(bt);
printf("\n");
postorder(bt);
printf("\n");
}
运行结果:
任杰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,输入每个人的密码,建立单循环链表。
输出形式:
建立一个输出函数,将正确的输出序列
#include
#include
#include
#include
/*结构体和函数声明*/
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;
else
memset(p_node,0,n*sizeof(node_t));
/*内存空间申请成功*/
p_ret=p_node;
for(;n_idx{
p_node->n_num=n_idx;
p_node->next=p_node+1;
p_node=p_node->next;
}
p_node->n_num=n;
p_node->next=p_ret;
}
returnp_ret;
}
/*
*name:
main
*params:
*none
*return:
*int
*notes:
*mainfunction
*/
intmain()
{
intn,m;
node_t*p_list,*p_iter;
printf("inputm:
\n");
scanf("%d",&m);
printf("inputn:
\n");
scanf("%d",&n);
/*构造环形单向链表*/
p_list=node_t_create(n);
/*Josephus循环取数*/
p_iter=p_list;
m%=n;
while(p_iter!
=p_iter->next)
{
inti=1;
/*取到第m-1个节点*/
for(;i{
p_iter=p_iter->next;
}
/*输出第m个节点的值*/
printf("%d\n",p_iter->next->n_num);
/*从链表中删除第m个节点*/
p_iter->next=p_iter->next->next;
p_iter=p_iter->next;
}
printf("%d\n",p_iter->n_num);
/*释放申请的空间*/
free(p_list);
}
运行结果:
赵庆康20080570
9、赫夫曼树的建立
任务:
建立建立最优二叉树函数
要求:
可以建立函数输入二叉树,并输出其赫夫曼树
在上交资料中请写明:
存储结构、基本算法(可以使用程序流程图)、输入输出、源程序、测试数据和结果、算法的时间复杂度、另外可以提出算法的改进方法;
#include
#include
#include
#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)->parent!
=0)flag=1;
if((HT+j)->weight==0)flag=1;
if(!
flag){if((HT+j)->weight<(HT+k)->weight)k=j;}
}
return(k);
}
main()
{printf("\n赫夫曼树的建立:
\n");
printf("请输入权值(叶子)数目:
");
scanf("%d",&l);
while(l<1){printf("输入错误,请重新输入权值数目:
");scanf("%d",&l);}
if(l==1)printf("\n只有一个权值,无须建立赫夫曼树!
");
else{n=2*l-1;
HT=(structHTnode*)malloc((n+1)*LEN);
printf("请按对应顺序输入权值(输入一权值,键入一回车):
\n");
for(i=1,p=HT+1;i<=l;++i,++p)
{scanf("%d",&w);
while(w<=0){printf("权值错,重新输入此权值:
");scanf("%d",&w);}
p->weight=w;p->parent=0;
p->lchild=0;p->rchild=0;
}
for(i=l+1;i<=n;++i,++p)
{p->weight=0;p->parent=0;
p->lchild=0;
}
for(i=l+1;i<=n;++i)
{a1=select();(HT+a1)->parent=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;i<=l;++i)
{start=l-1;
for(c=i,f=(HT+i)->parent;f!
=0;c=f,f=(HT+f)->parent)
if((HT+f)->lchild==c)