国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

特殊環形隊列基本操作

2019-11-11 04:58:09
字體:
來源:轉載
供稿:網友
/*  問題:對于環形隊列如果知道隊頭指針和隊列中元素的個數。設計出這種  環形隊列的基本操作。  分析:        隊尾指針rear=(front+count)%MaxSize        隊空條件:count==0.        隊滿條件:count==MaxSize。*/#include <stdio.h>#include <stdlib.h>#define MaxSize 5typedef char ElemType;typedef struct{    ElemType data[MaxSize];//存放隊列中的元素    int front;//定義隊頭指針    int count;//定義元素個數}QuType;//定義順序隊的類型void InitQueue(QuType *&q)//初始化順序隊{    q = (QuType *)malloc(sizeof(QuType));    q->front= 0;    q->count = 0;}void DestroyQueue(QuType *&q)//銷毀順序隊{    free(q);}bool QueueEmpty(QuType *q)//判斷順序隊是否為空{    return (q->count==0);}bool enQueue(QuType *&q,ElemType e)//入隊{    int rear;    if(q->count==MaxSize)//隊滿上溢出        return false;    else    {        rear=(q->front+q->count)%MaxSize;//求隊尾位置        rear = (rear + 1)%MaxSize;        q->data[rear]=e;        q->count++;        return true;    }}bool deQueue(QuType *&q,ElemType &e)//出隊{    if(q->count==0)//對空下溢出        return false;    else    {        q->front = (q->front + 1)%MaxSize;        e = q->data[q->front];        q->count--;        return true ;    }}int main(){	ElemType e;	QuType *q;	PRintf("環形隊列基本運算如下:/n");	printf("  (1)初始化隊列q/n");	InitQueue(q);	printf("  (2)依次進隊列元素a,b,c/n");	if (!enQueue(q,'a')) printf("/t提示:隊滿,不能進隊/n");	if (!enQueue(q,'b')) printf("/t提示:隊滿,不能進隊/n");	if (!enQueue(q,'c')) printf("/t提示:隊滿,不能進隊/n");	printf("  (3)隊列為%s/n",(QueueEmpty(q)?"空":"非空"));	if (deQueue(q,e)==0)		printf("隊空,不能出隊/n");	else		printf("  (4)出隊一個元素%c/n",e);	printf("  (5)依次進隊列元素d,e,f/n");	if (!enQueue(q,'d')) printf("/t提示:隊滿,不能進隊/n");	if (!enQueue(q,'e')) printf("/t提示:隊滿,不能進隊/n");	if (!enQueue(q,'f')) printf("/t提示:隊滿,不能進隊/n");	printf("  (6)出隊列序列:");	while (!QueueEmpty(q))	{	deQueue(q,e);		printf("%c ",e);	}	printf("/n");	printf("  (7)釋放隊列/n");	DestroyQueue(q);    return 0;}

運行結果:


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 江山市| 神木县| 原平市| 延吉市| 邵武市| 顺昌县| 鄄城县| 阿瓦提县| 皮山县| 宁都县| 天镇县| 新沂市| 南澳县| 六枝特区| 龙口市| 芜湖县| 紫云| 黄石市| 灵璧县| 盐城市| 隆尧县| 黔东| 满城县| 平利县| 邯郸市| 富蕴县| 理塘县| 米脂县| 海淀区| 绍兴县| 太谷县| 定日县| 叶城县| 长葛市| 宁阳县| 石泉县| 松阳县| 肥乡县| 怀宁县| 财经| 浦东新区|