完整版数据结构经典题目及c语言代码.docx

上传人:b****3 文档编号:1076605 上传时间:2022-10-16 格式:DOCX 页数:42 大小:28.44KB
下载 相关 举报
完整版数据结构经典题目及c语言代码.docx_第1页
第1页 / 共42页
完整版数据结构经典题目及c语言代码.docx_第2页
第2页 / 共42页
完整版数据结构经典题目及c语言代码.docx_第3页
第3页 / 共42页
完整版数据结构经典题目及c语言代码.docx_第4页
第4页 / 共42页
完整版数据结构经典题目及c语言代码.docx_第5页
第5页 / 共42页
点击查看更多>>
下载资源
资源描述

完整版数据结构经典题目及c语言代码.docx

《完整版数据结构经典题目及c语言代码.docx》由会员分享,可在线阅读,更多相关《完整版数据结构经典题目及c语言代码.docx(42页珍藏版)》请在冰豆网上搜索。

完整版数据结构经典题目及c语言代码.docx

完整版数据结构经典题目及c语言代码

《数据结构》课程设计题目

(程序实现采用C语言)

题目1:

猴子选王(学时:

3)

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

要求:

m及n要求从键盘输入,存储方式采用向量及链表两种方式实现该问题求解。

//链表

#include

#include

//链表节点

typedefstruct_RingNode

{

intpos;

struct_RingNode*next;

}RingNode,*RingNodePtr;

II创建约瑟夫环,pHead:

链表头指针,count:

链表元素个数

voidCreateRing(RingNodePtrpHead,intcount)

{

RingNodePtrpCurr=NULL,pPrev=NULL;

inti=1;

pPrev=pHead;

while(--count>0)

{

pCurr=(RingNodePtr)malloc(sizeof(RingNode));

i++;

pCurr->pos=i;

pPrev->next=pCurr;

pPrev=pCurr;

}

pCurr->next=pHead;//构成环状链表

}

voidKickFromRing(RingNodePtrpHead,intn)

{

RingNodePtrpCurr,pPrev;

inti=1;//计数

pCurr=pPrev=pHead;

while(pCurr!

=NULL)

{

if(i==n)

{

//踢出环

printf("\n%d",pCurr->pos);//显示出圈循序

pPrev->next=pCurr->next;

free(pCurr);

pCurr=pPrev->next;

i=1;

}pPrev=pCurr;

pCurr=pCurr->next;

if(pPrev==pCurr)

{

//最后一个

//显示出圈循序

printf("\nKingis%d",pCurr->pos);

free(pCurr);

break;

}

i++;

}

}

intmain()

{

intn=0,m=0;

RingNodePtrpHead=NULL;

printf("M(personcount)=");scanf("%d",&m);

printf("N(outnumber)=");scanf("%d",&n);

if(m<=0||n<=0)

{

printf("InputError'n");

return0;

//建立链表pHead=(RingNodePtr)malloc(sizeof(RingNode));

pHead->pos=1;

pHead->next=NULL;

CreateRing(pHead,m);

//开始出圈

printf("\nKickOrder:

");

KickFromRing(pHead,n);

printf("\n");

system("pause");

return0;

}

//数组做:

#include

#include

#include

voidSelectKing(intMonkeyNum,intCallNum);

voidmain()

{

intMonkeyNum;

intCallNum;

printf("MonkeyNum=”);

scanf("%d",&MonkeyNum);

/*输入M的值*/

printf("CallNum=");

scanf("%d",&CallNum);

SelectKing(MonkeyNum,CallNum);

}

voidSelectKing(intMonkeyNum,intCallNum)

{

int*Monkeys;//申请一个数组,表示所有的猴子;

intcounter=0;//计数,当计数为猴子个数时表示选到最后一个猴子了;

intposition=0;//位置,数组的下标,轮流遍历数组进行报数;

inttoken=0;//令牌,将报数时数到M的猴子砍掉;

//申请猴子个数大小的数组,把桌子摆上。

Monkeys=(int*)malloc(sizeof(int)*MonkeyNum);if(NULL==Monkeys)

{

printf("Somanymonkeys,systemerror.'n");return;

}

//将数组的所有内容初始化为0,被砍掉的猴子设置为1

memset(Monkeys,0,sizeof(int)*MonkeyNum);

//循环,直到选中大王

while(counter!

=MonkeyNum)

{

//如果这个位置的猴子之前没有砍掉,那么报数有效

if(Monkeys[position]==0)

{

token++;//成功报数一个,令牌+1,继续报数直到等于M

//如果报数到M,那么将这个猴子砍去

if(token==CallNum)

{

Monkeys[position]=1;//设置为1,表示砍去

counter++;//计数增加

token=0;//设置为0,下次重新报数

//如果是最后一个猴子,把它的位置打印,这个就是大王了

if(counter==MonkeyNum)

{

printf("Thekingisthe%dmonkey.\n",position+1);

}

}

}

//下一个猴子报数

position=(position+1)%MonkeyNum;

//释放内存,开头为所有猴子创建的桌子

free(Monkeys);

return;

}

题目2:

字符逆转(学时:

3)

,并按相反的

从键盘读入一个字符串,把它存入一个链表(每个结点存储1个字符)次序将字符串输出到显示屏。

#include

#include

structnode

{

structnode*prev;

charc;

structnode*next;

};

structnode*input(structnode*top);

intmain(void)

{

structnodeT,*top=&T,*bottom=&T,*p=NULL;

T.prev=NULL;

T.next=NULL;

T.c='\O:

bottom=input(top);

p=bottom->prev;

while(p!

=NULL)

{

printf("%c",p->c);

p=p->prev;

}

return0;

}

structnode*input(structnode*top)

{

structnode*t;

charx;

while((x=getchar())!

='\n')

{

top->c=x;

t=(structnode*)malloc(sizeof(structnode));

top->next=t;

t->prev=top;

t->next=NULL;

t->c='\0';

top=top->next;

returntop;

}

题目3:

工资核算(学时:

3)

设有一个单位的人员工资有如下信息:

namedepartment、basepay、

allowanee、total。

现从键盘输入一组人员工资数据并将它们存储到名为paydata的文件中;再从paydata取出工资数据并给每个人的basepay增加

100兀,增加后将工资数据显示于屏幕(每行1人)。

#include

#include

#defineSIZE2

#defineLENTHsizeof(structstuff)

structstuff

{

charname[100];

chardepartment[100];

intbasepay;

intallowanee;

inttotal;

}stuff[SIZE];

main()

{

FILE*fp;

inti;

printf("Pleaseenternamedepartmentbasepayallowanee:

\n”);for(i=0;i

if((fp=fopen("paydata.dat","wb"))==NULL)

{

printf("Can'topenfile\n");

return0;

}

for(i=0;i

if(fwrite(&stuff[i],LENTH,1,fp)!

=1)

printf("文件写入出错\n");

fclose(fp);

if((fp=fopen("paydata.dat","rb"))==NULL)

{

printf("Can'topenfile\n");

}

printf("修改过后的结果:

\n");

for(i=0;i

{

fread(&stuff[i],LENTH,1,fp);

stuff[i].total=stuff[i].basepay+100+stuff[i].allowance;

printf("%-10s%-10s%f%f%f\n",stuff[i].name,stuff[i].department,stuff[i].basepay+100,stuff[i].

allowance,stuff[i].total);

}

fclose(fp);

return0;

题目4:

满足条件的有序表生成(学时:

3)

已知三个有序表ABC,它们皆由同一类元素构成,现要求对于表以下运算而获得有序表D:

排出A中所有的既在B中又在C中出现的元素外该任务要求具有建立有序表功能以及输出有序表到屏幕的功能。

#include

voidmain()

{

inta[7],b[5],c[6],d[7];

inti,j,k,t,m;

printf("\nPleaseenter7numbersofA:

");

for(i=0;i<7;i++)

scanf("%d",&a[i]);

for(j=0;j<6;j++)

for(i=0;i<6-j;

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

当前位置:首页 > IT计算机 > 电脑基础知识

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

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