《数据结构》课程设计题目(程序实现采用C语言)题目1:猴子选王(学时:3)一堆猴子都有编号,编号是1,2,3 ...m,这群猴子(m个)按照1-m的顺序围坐一圈,从第1开始数,每数到第n个,该猴子就要离开此圈,这样依次下来,直到圈中只剩下最后一只猴子,则该猴子为大王。
要求:m及n要求从键盘输入,存储方式采用向量及链表两种方式实现该问题求解。
//链表#include <stdio.h>#include <stdlib.h>// 链表节点typedef struct _RingNode{int pos;struct _RingNode *next;}RingNode, *RingNodePtr;// 创建约瑟夫环,pHead:链表头指针,count:链表元素个数void CreateRing(RingNodePtr pHead, int count){RingNodePtr pCurr = NULL, pPrev = NULL;int i = 1;pPrev = pHead;while(--count > 0){pCurr = (RingNodePtr)malloc(sizeof(RingNode));i++;pCurr->pos = i;pPrev->next = pCurr;pPrev = pCurr;}pCurr->next = pHead; // 构成环状链表}void KickFromRing(RingNodePtr pHead, int n){RingNodePtr pCurr, pPrev;int i = 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("\nKing is %d", pCurr->pos); // 显示出圈循序free(pCurr);break;}i++;}}int main(){int n = 0, m = 0;RingNodePtr pHead = NULL;printf("M(person count) = ");scanf("%d", &m);printf("N(out number) = ");scanf("%d", &n);if(m <= 0 || n <= 0){printf("Input Error\n");return 0;}// 建立链表pHead = (RingNodePtr)malloc(sizeof(RingNode));pHead->pos = 1;pHead->next = NULL;CreateRing(pHead, m);// 开始出圈printf("\nKick Order: ");KickFromRing(pHead, n);printf("\n");system("pause");return 0;}//数组做:#include<stdio.h>#include<stdlib.h>#include<string.h>void SelectKing(int MonkeyNum, int CallNum);void main(){int MonkeyNum;int CallNum;/* 输入猴子的个数*/printf("Monkey Num = ");scanf("%d", &MonkeyNum);/* 输入M的值*/printf("Call Num = ");scanf("%d", &CallNum);SelectKing(MonkeyNum, CallNum);}void SelectKing(int MonkeyNum, int CallNum){int *Monkeys; // 申请一个数组,表示所有的猴子;int counter = 0; //计数,当计数为猴子个数时表示选到最后一个猴子了;int position = 0; // 位置,数组的下标,轮流遍历数组进行报数;int token = 0; // 令牌,将报数时数到M的猴子砍掉;// 申请猴子个数大小的数组,把桌子摆上。
Monkeys = (int *)malloc(sizeof(int)* MonkeyNum);if (NULL == Monkeys){printf("So many monkeys, system error.\n");return;}// 将数组的所有内容初始化为0,被砍掉的猴子设置为1memset(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("The king is the %d monkey.\n", position+1);}}}// 下一个猴子报数position = (position + 1)%MonkeyNum;}// 释放内存,开头为所有猴子创建的桌子free(Monkeys);return;}题目2:字符逆转(学时:3)从键盘读入一个字符串,把它存入一个链表(每个结点存储1个字符),并按相反的次序将字符串输出到显示屏。
#include <stdio.h>#include <stdlib.h>struct node{struct node *prev;char c;struct node *next;};struct node *input(struct node *top);int main(void){struct node T,*top=&T,*bottom=&T,*p=NULL;T.prev=NULL;T.next=NULL;T.c='\0';bottom=input(top);p=bottom->prev;while(p!=NULL){printf("%c",p->c);p=p->prev;}return 0;}struct node *input(struct node *top){struct node *t;char x;while((x=getchar())!='\n'){top->c=x;t=(struct node *)malloc(sizeof(struct node));top->next=t;t->prev=top;t->next=NULL;t->c='\0';top=top->next;}return top;}题目3:工资核算(学时:3)设有一个单位的人员工资有如下信息:name、department、 base pay、allowance、total。
现从键盘输入一组人员工资数据并将它们存储到名为paydata的文件中;再从paydata取出工资数据并给每个人的base pay增加100元,增加后将工资数据显示于屏幕(每行1人)。
#include<stdio.h>#include<stdlib.h>#define SIZE 2#define LENTH sizeof(struct stuff)struct stuff{char name[100];char department[100];int basepay;int allowance;int total;}stuff[SIZE];main(){FILE *fp;int i;printf("Please enter name department basepay allowance:\n");for(i=0;i<SIZE;i++)scanf("%s %s %f %f",&stuff[i].name,&stuff[i].department,&stuff[i].basepay,&stuff[i].allowance );if((fp=fopen("paydata.dat","wb"))==NULL){printf("Can't open file\n");return 0;}for(i=0;i<SIZE;i++)if(fwrite(&stuff[i],LENTH,1,fp)!=1)printf("文件写入出错\n");fclose(fp);if((fp=fopen("paydata.dat","rb"))==NULL){printf("Can't open file\n");}printf("修改过后的结果:\n");for(i=0;i<SIZE;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);return 0;}题目4:满足条件的有序表生成(学时:3)已知三个有序表A、B、C,它们皆由同一类元素构成,现要求对于表A作以下运算而获得有序表D:排出A中所有的既在B中又在C中出现的元素。