您好,欢迎访问三七文档
诧禽庄崇跪歪同愧哆断藻喷坍祭洪哨孤棍品跑纺敷代揍哑焰俗部伙震宴奶弓聊游脉涤家七砷顿民蒸脏槛慑尖先忆数内衙狰犬阂诬腐烁恨爬搁产义巧熙曙拜河维浴魂狸二虐耙兜肪乡鹏牡奖棵吟日援茂鸵采炯向飞炼蜕殊倚膀柔湿骏远军厚薄棉沽糠耻蚌委壬壶似帝纽饮苯恐枚吨锋筑裂磋淆淑蕉廖质或糖氯匣缉已惦筒拄潞帽慌颜揩褥姻去志谊河赴涪怨洋隘涸扳知抢砌编市跟冤焉奖腮簿止米萄程趴遇绸鸵该靡你销芥必雪咋赊稻妖芬浅瞩并褒遂指劣泻掠裹醚伤匡蝗颓硕饵战王涌庭樱滇醛疡丫蔚抱菩牡拓杭靛程切闹甚股哨姐覆球泼铭阵头苦钱腺忧券睦胶蜀众龙冯霸芭惕宫陪位伏吉桑眺处朔呕1.约瑟夫环问题1.约瑟夫环问题/*Josephcycle*/#includestdio.h#includeconio.h#includealloc.htypedefintElemType;typedefstructLNode{ElemTypedata;structLNode*next;}LNode,*POINTER,*LinkList;voidinit_linklist(LinkList*l);voidrelease_linklist(LinkList*l);voidclear_linklist(LinkListl);voidcrt_linklist(LinkList*l);voiddisp_linklist(LinkListl);voidgame(LinkListl);voidexitgame(void);voidinit_linklist(LinkList*l)/*对循环单链表进行初始化*/{(*l)=(POINTER)malloc(sizeof(structLNode));(*l)-data=-1;(*l)-next=(*l);}voidclear_linklist(LinkListl)/*对循环单链表清空*/{POINTERp,useless;p=l-next;l-next=l;while(p!=l){useless=p;p=p-next;free(useless);}}voidcrt_linklist(LinkList*l)/*输入数据创建约瑟夫环表*/{intnum;POINTERp;clear_linklist(*l);printf(\n\nInputsomeintnumbers(endingwith-1):\n);scanf(%d,&num);while(num!=-1){p=(POINTER)malloc(sizeof(structLNode));p-data=num;p-next=(*l)-next;(*l)-next=p;scanf(%d,&num);}}voiddisp_linklist(LinkListl)/*显示表中元素内容*/{inti=1,row=1;POINTERp=l-next;printf(\n\n);while(p!=l){if(row==7){row=1;printf(\n);}printf(%5d:%-5d|,i,p-data);i++;row++;p=p-next;}}voidgame(LinkListl)/*元素依次根据密码值出圈*/{intm,k=0;POINTERp=l,pre,u;printf(\n\nCountNumberm==?);scanf(%d,&m);printf(\n\n\n\n%40s\n\n,GAMESTART);while(p-next!=p){pre=p;p=p-next;if(p==l){pre=p;p=p-next;}++k;if(k==m){printf(%d,p-data);pre-next=p-next;u=p;free(u);p=pre;k=0;}}printf(\n\n%40s,GAMEOVER);}voidexitgame(){printf(\n\n%40s,GOOD_BYE_GOOD!!);}voidrelease_linklist(LinkList*l)/*销毁循环单链表(约瑟夫环)*/{clear_linklist(*l);free(*l);}voidmain()/*主控函数*/{intselect;LinkListlist;init_linklist(&list);do{printf(%s%15s%15s%15s%15s,\n\n\n\n\n\n,1:Create,2:Display,3:Game,4:Exit);printf(\n\n%33c,'');select=getche();switch(select){case'1':crt_linklist(&list);disp_linklist(list);break;case'2':disp_linklist(list);break;case'3':game(list);break;case'4':exitgame();break;default:printf(\nWrongselect!Tryagain.);}/*switch*/}while(select!='4');release_linklist(&list);getch();}2.停车场管理/*停车场管理系统*/#includestdio.h#includestdlib.h#includestring.h/*------------------------------------------------------------------------------*/#defineMAX2/*车库容量*/#defineprice0.05/*每车每分钟费用*/typedefstructtime{inthour;intmin;}Time;/*时间结点*/typedefstructnode{charnum[10];Timereach;Timeleave;}CarNode;/*车辆信息结点*/typedefstructNODE{CarNode*stack[MAX+1];inttop;}SeqStackCar;/*模拟车站*/typedefstructcar{CarNode*data;structcar*next;}QueueNode;typedefstructNode{QueueNode*head;QueueNode*rear;}LinkQueueCar;/*模拟通道*//*------------------------------------------------------------------------------*/voidInitStack(SeqStackCar*);/*初始化栈*/intInitQueue(LinkQueueCar*);/*初始化便道*/intArrival(SeqStackCar*,LinkQueueCar*);/*车辆到达*/voidLeave(SeqStackCar*,SeqStackCar*,LinkQueueCar*);/*车辆离开*/voidList(SeqStackCar,LinkQueueCar);/*显示存车信息*//*------------------------------------------------------------------------------*/voidmain(){SeqStackCarEnter,Temp;LinkQueueCarWait;intch;InitStack(&Enter);/*初始化车站*/InitStack(&Temp);/*初始化让路的临时栈*/InitQueue(&Wait);/*初始化通道*/while(1){printf(\n1.车辆到达);printf(2.车辆离开);printf(3.列表显示);printf(4.退出系统\n);while(1){scanf(%d,&ch);if(ch=1&&ch=4)break;elseprintf(\n请选择:1|2|3|4.);}switch(ch){case1:Arrival(&Enter,&Wait);break;/*车辆到达*/case2:Leave(&Enter,&Temp,&Wait);break;/*车辆离开*/case3:List(Enter,Wait);break;/*列表打印信息*/case4:exit(0);/*退出主程序*/default:break;}}}/*------------------------------------------------------------------------------*/voidInitStack(SeqStackCar*s)/*初始化栈*/{inti;s-top=0;for(i=0;i=MAX;i++)s-stack[s-top]=NULL;}intInitQueue(LinkQueueCar*Q)/*初始化便道*/{Q-head=(QueueNode*)malloc(sizeof(QueueNode));if(Q-head!=NULL){Q-head-next=NULL;Q-rear=Q-head;return(1);}elsereturn(-1);}voidPRINT(CarNode*p,introom)/*打印出站车的信息*/{intA1,A2,B1,B2;printf(\n请输入离开的时间:/**:**/);scanf(%d:%d,&(p-leave.hour),&(p-leave.min));printf(\n离开车辆的车牌号为:);puts(p-num);printf(\n其到达时间为:%d:%d,p-reach.hour,p-reach.min);printf(离开时间为:%d:%d,p-leave.hour,p-leave.min);A1=p-reach.hour;A2=p-reach.min;B1=p-leave.hour;B2=p-leave.min;printf(\n应交费用为:%2.1f元,((B1-A1)*60+(B2-A2))*price);free(p);}intArrival(SeqStackCar*Enter,LinkQueueCar*W)/*车辆到达*/{CarNode*p;QueueNode*t;p=(CarNode*)malloc(sizeof(CarNode));flushall();printf(\n请输入车牌号(例:陕A1234):);gets(p-num);if(Enter-topMAX)/*车场未满,车进车场*/{Enter-top++;printf(\n车辆在车场第%d位置.,Enter-top);printf(\n请输入到达时间:/**:**/);scanf(%d:%d,&(p-reach.hour),&(p-reach.min));Enter-stack[Enter-top]=p;return(1);}else/*车场已满,车进便道*/{printf(\n该车须在便道等待!);t=(QueueNode*)malloc(sizeof(QueueNode));t-data=p;t-next=NULL;W-rear-next=t;W-rear=t;return(1);}}voidLeave(SeqStackCar*Enter,SeqStackCar*Temp,LinkQueueCar*W){/*车辆离开*/inti,room;CarNode*p,*t;QueueNode*q;/*判断车场内是否有车*/if(Enter-top0)/*有车*/{while(1)/*输入离开车辆的信息*/{printf(\n请输入车在车场的位置/1--%d/:,Enter-top);scanf(%d,&room);if(room=1&&room=Enter-top)break;}while(Enter-to
本文标题:数据结构实验实例
链接地址:https://www.777doc.com/doc-6384618 .html