資源簡介
約瑟夫問題(C語言實現),以后會逐步添加其他實驗
代碼片段和文件信息
?#include
?#include
?/*定義常量*/
?#define?TRUE?1
?#define?FALSE?0
?#define?OK?1
?#define?ERROR?0
?#define?null?0
?#define?INFEASIBLE?-1
?#define?OVERFLOW?-2?
?/*定義類型*/
?typedef?int?Status;
?typedef?int?ElemType;
?/*定義節結點類型及雙向循環鏈表的存儲結構*/
?typedef?struct?link{
?????ElemType?data;
?????ElemType?number;
?????struct?link?*prior;
?????struct?link?*next;
?}link*linkList;
?Status?initList(linkList?*L){/*初始化鏈表*/
?????*L=(linkList)malloc(sizeof(link));
?????if(*L){
?????????(*L)->next=(*L)->prior=*L;
?????????(*L)->data=null;
?????????return?OK;
?????}
?????else?return?OVERFLOW;
?}
??Status?destroyList(linkList?*L){/*銷毀鏈表并將鏈表返回?*/
?????linkList?qp=(*L)->next;
?????while(p!=*L){/*返回表頭時結束??*/
?????????q=p->next;????????????????????????????????
?????????free(p);
?????????p=q;
?????}
?????free(*L);
?????*L=NULL;???????????????????????????????????????
?????return?OK;
?}
?Status?isEmpty(linkList?L){/*判斷鏈表是否為空用return返回TRUE或FALSE*/
?????return?((L->next==L&&L->prior==L));
?}
?Status?insertFirst(linkList?LElemType?data1ElemType?number1){
?????/*插入元素并作為第一個結點*/
?????linkList?newlink=(linkList)malloc(sizeof(link));
?????newlink->data=data1;
?????newlink->number=number1;
?????if(!isEmpty(L)){
?????????newlink->prior=L;
?????????newlink->next=L->ne
評論
共有 條評論