[计算机]数据结构实验代码

上传人:豆浆 文档编号:33406998 上传时间:2018-02-15 格式:DOC 页数:10 大小:49KB
返回 下载 相关 举报
[计算机]数据结构实验代码_第1页
第1页 / 共10页
[计算机]数据结构实验代码_第2页
第2页 / 共10页
[计算机]数据结构实验代码_第3页
第3页 / 共10页
[计算机]数据结构实验代码_第4页
第4页 / 共10页
[计算机]数据结构实验代码_第5页
第5页 / 共10页
点击查看更多>>
资源描述

《[计算机]数据结构实验代码》由会员分享,可在线阅读,更多相关《[计算机]数据结构实验代码(10页珍藏版)》请在金锄头文库上搜索。

1、 约瑟夫问题的数组实现!*#include #include #define OK 1#define size 8int Joseph(int m,int k)int i,j=size;int p=m;int asize;for(i=0;i#include#define null 0#define ok 1#define error 0#define overflow -2typedef int Status;typedef int elemtype;typedef struct cnodeelemtype data;struct cnode *next;cnode;cnode *monkey

2、;/声明全局变量/Status Create_clist(cnode *clist,int n)cnode *p,*q;int i;clist=null;for(i=n;i=1;i-)p=(cnode *)malloc(sizeof(cnode);if(p=null)return overflow;p-data=i;p-next=clist;clist=p;if(i=n)q=p;q-next=clist;monkey=clist;return ok;/建立循环链表/Status Monkey(cnode *clist,int n)/建立满足条件的函数/int i,m=13;cnode *p,*

3、q;if(!Create_clist(clist,n)/建立循环链表/return error;p=monkey;lab: if(!(p-next=p)/不是只剩一个结点/while(m)for(i=1;inext;q=p-next;p-next=q-next;p=p-next;free(q);m-;/找出满足条件的结点并删除/m=13;goto lab;if(p-next=p)printf(猴王是 %d 号,p-data);void main()int m,n;cnode *clist;clist=null;printf(n 请输入猴子的个数 n:);scanf(%d,/初始化猴子的个数/C

4、reate_clist(clist,n);/建立循环链表/Monkey(clist,n);狐狸打兔子线性表形式*#include#include#define ok 1#define overflow -2typedef int Status;/线性表为整型/typedef int Elemtype;#define LIST_INIT_SIZE 20 /表示线性表初始空间分配量,可为 5 或 20/#define maxsize 1000 /表示进表次数 ,宏定义使次数可。1000 或 500/typedef struct Elemtype *elem;/存储空间基址/int length;/

5、当前长度/int listsize;/当前分配存储量 /sqlist;Status Initlist_sq(sqlist *L)(*L).elem=(Elemtype *)malloc(LIST_INIT_SIZE*sizeof(Elemtype);if(!(*L).elem) return overflow;/存储失败/(*L).length=0;/空表长度为 0/(*L).listsize=LIST_INIT_SIZE;/初始存储量/return ok;Status rabbit(sqlist *L)/构造狐狸逮兔子函数/int i, current=0;for(i=0;i#include

6、#define OK 1#define OVERFLOW -2#define NULL 0#define maxsize 1000/表示进表次数 ,宏定义使次数可。1000 或 500/typedef int Status;/线性表为整型/typedef int Elemtype;/定义数据类型 /typedef struct lnodeElemtype data;/指针指向数据域/struct lnode *next;/指证域/lnode;lnode *rabbit;/声明全局变量 /Status Initlist_sq(lnode *clist,int n)/用单链表实现/lnode *p

7、,*q;int i;clist=NULL;for(i=n;i=1;i-)p=(lnode *)malloc(sizeof(lnode);if(p=NULL)return OVERFLOW ;p-data=i;/将每个结点的数据域设为 i,使每个结点对应一个号码/p-next=clist;clist=p;/if(i=n)q=p;q-next=clist;/将链表的尾结点的指针域设为头结点,建立循环链表 /rabbit=clist;/将头结点的地址赋给全局变量 rabbit/return OK;Status Rabbit(lnode *clist,int n)/建立满足条件的 Rabbit 函数/

8、int i=1,j=2,k=2;lnode *r;r=rabbit;lab:if(inext;/每轮找出表示进洞结点的地址/k-;r-data=0;/将进洞结点的数据域设为 0/k=+j;i+;goto lab;for(i=1;idata!=0)printf(兔子在%d 洞中n,rabbit-data);/ 输出未进洞的数据号码 /rabbit=rabbit-next; elserabbit=rabbit-next; return OK;void main()lnode *clist; int n;printf(兔子设了几个洞 n);scanf(%d,Initlist_sq(clist,n);

9、/创建循环链表/Rabbit(clist,n);/ getch();#include#define add 43#define subs 45#define mult 42#define div 47#define MAXSIZE 100typedef struct int stkdataMAXSIZE;int top;STKzone;typedef STKzone *STK;typedef enumtrue=1,false=0 bool;typedef enumok,error status;STKzone expSTKzone;STK expSTK;STK initSTK(STKzone

10、*stack_zone)STK p;p=stack_zone;p-top=0;status push(int *term,STK pstk)if(pstk-top=MAXSIZE)return error;pstk-stkdatapstk-top=*term;(pstk-top)+;return ok;bool emptySTK(STK pstk)return(pstk-top=0);status pop(int *pdata,STK pstk)if(emptySTK(pstk)return error;(pstk-top)-;*pdata=pstk-stkdatapstk-top;retur

11、n ok;void synerror()printf(n 表达式语法错!);exit();int eval(char tag,int a1,int a2)switch(tag) case add:return(a1+a2); case subs:return(a1-a2);case mult:return(a1*a2);case div:return(a1/a2);main() char c;int opd1,opd2,temp,c1;expSTK=initSTK(printf(n 后置表达式:);while(c=getchar()!=n)if(c= ) continue;if(c47)&(c

12、#include #define OVERFLOW -2typedef structint arrive;int treat;QNODE;typedef struct nodeQNODE data;struct node *next;LNODE;LNODE *front,*rear;QNODE curr,temp;char Fname120;FILE *fp;void EnQueue(LNODE *hpt,LNODE *tpt,QNODE e)LNODE *p=(LNODE *)malloc(sizeof(LNODE);if(!p) exit(OVERFLOW);p-data=e;p-next

13、=NULL;if(*hpt=NULL) *tpt=*hpt=p;else *tpt=(*tpt)-next=p;int DeQueue(LNODE *hpt,LNODE *tpt,QNODE *cp)LNODE *p=*hpt;if(*hpt=NULL) return 1;*cp=(*hpt)-data;*hpt=(*hpt)-next;if(*hpt=NULL) *tpt=NULL;free(p);return 0;void main() int dwait=0,clock=0,wait=0,count=0,have=0,finish;printf(n enter file name:);s

14、canf(%s,Fname);fp=fopen(Fname,r);if(!fp)printf(cannot open file %s,Fname);return;front=NULL;rear=NULL;have=fscanf(fp,%d%s,doif(front=NULL&have=2)dwait+=temp.arrive-clock;clock=temp.arrive;EnQueue(have=fscanf(fp,%d%d,count+;DeQueue(wait+=clock-curr.arrive;finish=clock+curr.arrive;while(have=2&temp.arrive=finish)EnQueue(have=fscanf(fp,%d%d,clock=finish;while(have=2|front!=NULL);printf(结果:业务员等待时间%d 客户平均等待时间 %fn,dwait,(double)wait/count);printf(模拟总时间: %d,n 客户人数:%d,n 总等待时间:%dn,clock,count,wait);

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 行业资料 > 其它行业文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号