中文提示按照m个猴子,数n个数的方法,输出为大王的猴子是几号,建立一个函数来实现此功能
2.3.1算法描述及实验步骤
主要模块的算法描述
主要函数voidSelect(LinkListhead,intm,intn)
运用单循环链表来实现,尾结点指向头结点,定义一个中间变量i来实现,当i=n时,删除该结点,i重新申请为0,指针继续指下去,直到删除到只剩一个结点,也就是说,该指针的下一个指针还是指向本身。
2.3.2调试过程及实验结果
删除报数为n的结点,直到只剩一个结点,则该结点为最终结果,猴子大王。
2.4.二叉树运算(最近祖先)
题目内容描述:
任务:
求二叉树中指定两个结点共同的最近祖先
2.4.1算法描述及实验步骤
主要模块的算法描述:
先生成一个二叉排序树,用链表指针来存储。
之后运用递归算法求叶子结点,另外申请一个指向二叉树的结构指针来存储叶子结点,生成一个单链表,最后打印出链表。
链接是用叶子结点的右孩子域存放指针。
voidCreateBiTree创建二叉树SearchAncestors查找最近祖先。
算法的改进方法:
上树存储为整型,可以改进定义为其他类型,建立二叉树
2.4.2调试过程及实验结果
创建树
查找
2.5各种排序
题目内容描述:
任务:
用程序实现插入法排序、起泡法改进算法排序;利用插入排序和冒泡法的改进算法,将用户随机输入的一列数按递增的顺序排好。
输入的数据形式为任何一个正整数,大小不限。
输出的形式:
数字大小逐个递增的数列。
2.5.1算法描述及实验步骤
主要模块的算法描述:
直接插入排序的基本思想是:
当插入第i(i≥1)个对象时,前面的V[0],V[1],…,v[i-1]已经排好序。
这时,用v[i]的关键码与v[i-1],v[i-2],…的关键码顺序进行比较,找到插入位置即将v[i]插入,原来位置上的对象向后顺移。
起泡排序的基本思想是:
需反复比较相邻两个数的比较与交换这两种基本操作。
对相邻的两个数进行比较时,如果反面的数大于(或小于)前面的数,将这两个数进行交换,大的数(小的数)往前冒。
2.5.2调试过程及实验结果
直接插入法:
冒泡排序法:
三.课程设计分析与总结
本次课程设计的运行结果在不断的修改中得到完善!
使我对数据结构有了更深的理解,既培养了我运用算法与数据结构的能力,又培养了我对于团队协作集成程序模块及调试能力。
平时学的东西终于在这课程设计充分体现出来!
这次又学到了很多!
四.源程序清单
一:
仓库管理
#include
#include
#include
typedefstructnode
{intNO;
charname[20];
intcount;
structnode*next;
}Storage,*Goods;
voidshowlist1()
{printf("==========storageinformation==========\n");
printf("numbersnamequantity\n");
printf("001naicha100\n");
printf("002huoguo200\n");
printf("003qiaokeli300\n");
}
voidshowlist()
{printf("1.addgoods\n");
printf("2.lookupgoods\n");
printf("3.deletegoods\n");
printf("4.exitsystem\n");
printf("=================================\n");
}
GoodsInputgoods(Goodshead)
{Storage*p,*q;
chara;
intch1,ch3;
char*ch2;
ch2=(char*)malloc(sizeof(char)*20);
head=(Goods)malloc(sizeof(Storage));
p=head;p->next=NULL;
printf("===============inputgoodsinformation==============\n");
do{q=(Storage*)malloc(sizeof(Storage));
printf("goodsnumber");
scanf("%d",&ch1);
q->NO=ch1;
printf("goodsname");
scanf("%s",ch2);
strcpy(q->name,ch2);
printf("goodsquantity");
scanf("%d",&ch3);
q->count=ch3;p->next=q;p=q;p->next=NULL;
getchar();
printf("ifcontinue?
(y/n)");
scanf("%c",&a);
printf("\n");
}while(a=='y'||a=='Y');
returnhead;
}
GoodsInsertgoods(Goodshead)
{Storage*p,*p2;
char*ch2;
intch1,ch3;
ch2=(char*)malloc(sizeof(char)*20);
printf("pleaseinputgoodsnumbersnamequantity\n");
p=head;
while(p->next!
=NULL)
p=p->next;
p2=(Storage*)malloc(sizeof(Storage));
printf("goodsnumbers");
scanf("%d",&ch1);
p2->NO=ch1;
printf("goodsname");
scanf("%s",ch2);
strcpy(p2->name,ch2);
printf("goodsquantity");
scanf("%d",&ch3);
p2->count=ch3;p->next=p2;p2->next=NULL;
returnhead;
}
voidLookupgoods(Goodshead)
{charr,*c2;
Storage*p,*p2;
intc1;
p=p2=head->next;
c2=(char*)malloc(sizeof(char)*20);
getchar();
printf("a.lookupbygoodsnumbers:
\n");
printf("b.kookupbygoodsname:
\n");
printf("pleaseinputaorbtolookup:
\n");
scanf("%c",&r);
if(r=='a')
{printf("pleaseinputgoodsnumbers:
\n");
scanf("%d",&c1);
getchar();
while(p&&p->NO!
=c1)p=p->next;
if(p==NULL)printf("nofindnothisnumber\n");
else{printf("name%s\n",p->name);
printf("quantity%d\n",p->count);
}
}
elseif(r=='b')
{printf("pleaseinputgoodsname:
\n");
scanf("%s",c2);
getchar();
while(p&&strcmp(p2->name,c2)!
=0)
p2=p2->next;
if(p2==NULL)printf("nofind,nothisname\n");
else{printf("numbers%d\n",p2->NO);
printf("quantity%d\n",p2->count);
}
}
else{getchar();
printf("inputwrong!
\n");}
getchar();
}
voidDeletegoods(Goodshead)
{Storage*s,*p=head;
inter;
printf("pleasethenumberswanttodelete");
scanf("%d",&er);
if(p->next==NULL){printf("norecord\n");return;}
else{while(p->next!
=NULL)
{s=p->next;
if(s!
=NULL&&s->NO==er)
{p->next=s->next;break;}
p=p->next;
}
}
}
voidPrintgoods(Goodshead)
{Storage*h=head->next;
printf("**************************\n");
printf("numbersnamequantity\n");
while(h!
=NULL){printf("%d%s\t%d\n",h->NO,h->name,h->count);
h=h->next;}
printf("**************************\n");
}
main(){Goodshead=NULL;
intn,i,j;
showlist1();
head=Inputgoods(head);
system("cls");showlist();Printgoods(head);
for(i=0;;i++){printf("pleaseinputthekeywanttooperate!
\n");
for(j=0;;j++){scanf("%d",&n);
if(n<1||n>5){printf("wrong!
pleaseinputagain!
!
\n");continue;}
elsebreak;
}
if(n==1){head=Insertgoods(head);system("cls");showlist();Printgoods(head);}
if(n==2){Lookupgoods(head);system("cls");showlist();Printgoods(head);}
if(n==3){Deletegoods(head);system("cls");showlist();Printgoods(head);}
if(n==4)break;
}printf("cangkuinformation");return0;
}
二通讯录管理系统
#include
#include
#include
typedefstructnode
{charnum[5];//编号
charname[8];//姓名
charsex[10];//性别
chartel[8];//电话
charaddress[100];//地址
structnode*next;
}ListNode,*Tel;
voidshowlist()
{printf("=================================\n");
printf("1.pleaseinsertthepeople\n");
printf("2.pleaselookupthepeople\n");
printf("3.pleasedeletethepeople\n");
printf("4.sortnumbers\n");
printf("5.exitsystem\n");
printf("=================================\n");
}
TelInputTel(Telhead)
{ListNode*p,*q;
chara;
char*ch1,*ch2,*ch3,*ch4,*ch5;
ch1=(char*)malloc(sizeof(char)*20);
ch2=(char*)malloc(sizeof(char)*20);
ch3=(char*)malloc(sizeof(char)*20);
ch4=(char*)malloc(sizeof(char)*20);
ch5=(char*)malloc(sizeof(char)*20);
head=(Tel)malloc(sizeof(ListNode));
p=head;p->next=NULL;
printf("inputcommunicationinformation\n");
do{q=(ListNode*)malloc(sizeof(ListNode));
printf("numbers:
");scanf("%s",ch1);strcpy(q->num,ch1);
printf("name:
");scanf("%s",ch2);strcpy(q->name,ch2);
printf("sex:
");scanf("%s",ch3);strcpy(q->sex,ch3);
printf("telephone:
");scanf("%s",ch4);strcpy(q->tel,ch4);
printf("address:
");scanf("%s",ch5);strcpy(q->address,ch5);
p->next=q;p=q;p->next=NULL;getchar();
printf("ifcontinue?
(y/n)");scanf("%c",&a);printf("\n");
}while(a=='y'||a=='Y');returnhead;
}
TelInsertTel(Telhead)//插入
{ListNode*p,*p2;
char*ch1,*ch2,*ch3,*ch4,*ch5;
ch1=(char*)malloc(sizeof(char)*20);
ch2=(char*)malloc(sizeof(char)*20);
ch3=(char*)malloc(sizeof(char)*20);
ch4=(char*)malloc(sizeof(char)*20);
ch5=(char*)malloc(sizeof(char)*20);
printf("\n");
p=head;
while(p->next!
=NULL)p=p->next;p2=(ListNode*)malloc(sizeof(ListNode));
printf("numbers:
");scanf("%s",ch1);strcpy(p2->num,ch1);
printf("name:
");scanf("%s",ch2);strcpy(p2->name,ch2);
printf("sex:
");scanf("%s",ch3);strcpy(p2->sex,ch3);
printf("telephone:
");scanf("%s",ch4);strcpy(p2->tel,ch4);
printf("address:
");scanf("%s",ch5);strcpy(p2->address,ch5);
p->next=p2;p2->next=NULL;returnhead;
}
voidLookupTel(Telhead)//查询
{charr,*c1,*c2;
ListNode*p,*p2;
p=p2=head->next;
c1=(char*)malloc(sizeof(char)*20);
c2=(char*)malloc(sizeof(char)*20);
getchar();
printf("a.bynumberlookup\n");printf("b.bynameloopup\n");printf("pleaseinputaorbtolookup\n");
scanf("%c",&r);
if(r=='a'){printf("pleaseinputnumbers\n");
scanf("%d",c1);getchar();
while(p&&strcmp(p->num,c1)!
=0)p=p->next;
if(p==NULL)printf("nofind,nothis