c++实现顺序循环队列结构,简易队列

实验项目4:队列的入队与出队。

1、实验目的

(1)了解普通顺序队的基本运算。

(2)掌握循环队列的入队及出队算法。

2、实验内容

  利用循环队列的内容,实现队列的EnQueue与DeQueue算法。

 

#include <iostream>

using namespace std;
#define MAX_SIZE 100
typedef int QElemType;
typedef int Status;
typedef struct {
    QElemType *base;
    int front;
    int rear;
}SqQueue;
/**
* Initialization of queues
*/
Status InitQueue(SqQueue &Q){
    Q.base = new QElemType[MAX_SIZE];
    if(!Q.base){
        cout<<"分配数组失败";
        return -1;
    }
    Q.front = Q.rear = 0;
    return 1;
}
/**
* Enter queues
*/
Status EnQueue(SqQueue &Q,QElemType e){
    if((Q.rear+1)%MAX_SIZE==Q.front){cout<<"queue full!"<<endl;return -1;}
    Q.base[Q.rear] = e;
    Q.rear = (Q.rear+1)%MAX_SIZE;
    return 1;
}
/**
* return queues length
*/
int QueueLnegth(SqQueue Q){
    return (Q.rear-Q.front+MAX_SIZE)%MAX_SIZE;
}
/**
* Quit queues
*/
Status DeQueue(SqQueue &Q,QElemType &e){
    if((Q.front==Q.rear)){return -1;}
    e = Q.base[Q.front];
    Q.front = (Q.front+1)%MAX_SIZE;
    return 1;
}

void displayQueue(SqQueue &Q){
    for(int i=Q.front;i<Q.rear-Q.front;i++){
        cout<<Q.base[i]<<endl;
    }
}

int main()
{
    SqQueue Q1;
    InitQueue(Q1);
    int a;
    for(int i=0;i<10;i++)
        EnQueue(Q1,i+1);
    DeQueue(Q1,a);
    displayQueue(Q1);
    return 0;
}

 

转载于:https://my.oschina.net/lafter/blog/901751

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值