| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1944 人关注过本帖
标题:循环队列
只看楼主 加入收藏
鹿晗
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2015-6-29
结帖率:0
收藏
 问题点数:0 回复次数:1 
循环队列
#include <stdio.h>
#include <malloc.h>
#define MAXQSIZE  5
typedef char QElemType;
typedef struct {
   QElemType *base;
   int front;
   int rear;
}SqQueue;

int InitQueue(SqQueue &Q) {
    Q.base=(QElemType*)malloc(MAXQSIZE*sizeof(SqQueue));
    if(!Q.base) return 0;
    Q.front=Q.rear=0;
    return 1;     
}

int QueueLength(SqQueue Q) {
    return (Q.rear-Q.front+MAXQSIZE)%MAXQSIZE;
}

int EnQueue(SqQueue &Q,QElemType e) {
    if((Q.rear+1)% MAXQSIZE ==Q.front) return 0;
    Q.base[Q.rear]=e;
    Q.rear=(Q.rear+1)%MAXQSIZE;
    return 1;
}
int DeQueue(SqQueue &Q,QElemType &e) {
    if(Q.front==Q.rear) return 0;
    e=Q.base[Q.front];  
    Q.front=(Q.front+1)% MAXQSIZE;
    return 1;  
}
void DispQueue(SqQueue Q){
    int i,j;  
    j=QueueLength(Q);
    if(j==0) printf("该队列为空队列!!\n");
    for(i=1;i<=j;i++){
        printf("%c",Q.base[Q.front]);  
        Q.front=(Q.front+1)% MAXQSIZE;
    }
}

void main(){
    int k;
    QElemType e;
    SqQueue Q;
    InitQueue(Q);
    DispQueue(Q);
    EnQueue(Q,'A');
    EnQueue(Q,'B');
    EnQueue(Q,'C');
    EnQueue(Q,'D');
    printf("对列为:");  
    DispQueue(Q);
    printf("\n");
    printf("长度为:");
    k=QueueLength(Q);
    printf("%d",k);
    printf("\n");
    DeQueue(Q,e);
    DeQueue(Q,e);
    DeQueue(Q,e);
    DeQueue(Q,e);
    DispQueue(Q);
    EnQueue(Q,'E');
    EnQueue(Q,'F');
    printf("对列为:");  
    DispQueue(Q);
    printf("\n");
}   

求解为什么总是无法运行= =
2015-11-06 15:52
森林之王1111
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2015-11-12
收藏
得分:0 
可以啊,我运行你的可以啊!
2015-11-13 20:57
快速回复:循环队列
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.014148 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved