哲學家就餐問題(C)


          /*為對原創者尊重,以下無刪除章節!
          經??吹交貜驼f,這個程序不能運行,還有怎么怎么..貼上來目的不是為了看結果。
          主要是方便大家理解思路過過程---
          家寶 */

          /*愿結伴共探編程之樂 作者:孤峰*/

          /*題目:一群哲學家圍坐在一個圓桌,手上持有密碼m,并從1開始編了號取初值m,哲學家從1開始報數, 報到m的哲學家停止吃飯,退出圓桌,求哲學家退出的順序。要求:n和初值m由完家輸入.手上的密碼隨機產生.最后要打印出編號對應的密碼,輸出哲學家離開的相后順序?

          分析:可用循環鏈表實現,鏈表數據類型為結構體,記錄編號和相應密碼,另外設標志哲學家報數的變量mouth, 它的值和哲學家嘴上報的數相等,則如果mouth和m相等,該哲學家就應該離開離開前取他的密碼交給m,同時將他的編號放另一單鏈表numbsave保存。注意編號要從numbsave的最后節點插入。當循環鏈表指向自身時停止比較,這個哲學家即是最后離開的一個.依次打印出numbsave中的數即為按編號哲學家離開的先后順序。

          */?????????????????

          #include "stdio.h"
          #include "conio.h"
          #include "stdlib.h"

          struct philosopher??????????? /*哲學家就餐結構體*/
          ?{ int number;????????????? /*編號*/
          ?? int password;
          ?? int mouth;???? /*嘴上報的數*/
          ???? struct? philosopher *next;
          ?};
          struct philosopher *phead,*pend,*pp;

          struct numbsave??????????????????? /*存放離開順序*/
          ?{ int numsave;
          ?? struct numbsave *next;
          ?};
          struct numbsave *top=NULL,*numbnew,*numbthis;

          void main(void)
          {?char *p,d;
          ?int? b=1,k,n,m,mouthm=1;
          ?clrscr(); gotoxy(9,8);
          ?printf("please input n m:");
          ?scanf("%d%d",&n,&m);??????? /*n為哲學家人數,m為初始密碼*/
          ?phead=(struct philosopher *)malloc(sizeof(struct philosopher));
          ?pend=phead;phead->mouth=1;
          ?for(b=1;b<=n-1;b++)???????????????? ?????????????? /*給哲學家分配隨機密碼*/
          ?{pend->number=b;
          ? k=random(20);????????? /*k為0<k<20之間的數*/
          ? while(k<=0)
          ? k=random(20);
          ? pend->password=k;
          ? pp=(struct philosopher *)malloc(sizeof(struct philosopher));
          ? pend->next=pp; pend=pp;
          ?}
          ?pend->number=b;???? ?????????? /*最后一位哲學家*/
          ?k=random(20); while(k<=0) k=random(20); pend->password=k; pend->next=phead; /*形成循環鏈表*/
          printf("\n\tphilosopher number correspondence password as followed:\n\t");
          pp=phead;
          for(b=1;b<=n;b++)
          ?{printf("%d:%d\t",pp->number,pp->password);
          ?pp=pp->next;
          ?}

          while(pend->next!=pend)
          ?{if(phead->mouth==m)?????????????????? ???/*如果嘴上報數和m相等,意味著一個人要走了*/
          ??{pp=phead;
          ?? phead->next->mouth=1;?mouthm=1;??/*下一位哲學家從一開始報,mm用于將順序報出數的交給嘴巴*/
          ?? phead=pend->next=phead->next; ???/*兩個指針一定要相鄰*/
          ?? numbnew=(struct numbsave*)malloc(sizeof(struct numbsave));
          ?? m=pp->password;???????????? ???/*修改m的值為離開哲學家的password*/
          ?? numbnew->numsave=pp->number;
          ?? if(top==NULL)?{top=numbnew; top->next=NULL;} /*離開的哲學家的編號存入numbsave的最后節點*/
          ?? else? { numbthis=top;
          ??while(numbthis->next!=NULL)? numbthis=numbthis->next;
          ?? numbthis->next=numbnew;?numbnew->next=NULL;
          ???}
          ?? free(pp);
          ??}
          ?? else {pend=pend->next;
          ??????? phead=phead->next;????????? ???/*讓phead指向下一個*/
          ??????? mouthm++;
          ??????? phead->mouth=mouthm;?????????? ???/*嘴巴說我該報mouthm*/
          ??????? }
          ?}???????/*打印離桌順序*/
          printf("\n\tphilosopher away from cookdesk in the follow queue:\n\t");
          while(top!=NULL)
          ?{ printf("%d? ",top->numsave);
          ?top=top->next;
          ?}
          printf("%d? ",pend->number);?????/*這個千萬別忘了,他是運氣最好的一位*/
          printf("\n\tpress any key to go back......");
          while(!kbhit()) ;
          }

          posted on 2007-03-18 07:55 金家寶 閱讀(485) 評論(0)  編輯  收藏 所屬分類: 數據結構和算法


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 峨山| 康马县| 东乡| 阿荣旗| 富阳市| 周至县| 漠河县| 曲水县| 平山县| 新绛县| 睢宁县| 含山县| 来安县| 屏东市| 葫芦岛市| 兰溪市| 贵溪市| 大悟县| 乐安县| 建平县| 久治县| 盐边县| 界首市| 特克斯县| 鹤庆县| 民县| 西乌| 沂南县| 永昌县| 南靖县| 金川县| 翼城县| 三台县| 历史| 临沂市| 巫山县| 宁强县| 巴林左旗| 六枝特区| 娱乐| 二手房|