封装一个循环顺序队列类

head.h

#ifndef QUEUE_H
#define QUEUE_H

#include <iostream>
#include <cstring>
#define MAXSIZE 5

using namespace std;

class Stack
{
private:
    int data[MAXSIZE];   //创建一个数组储存队列
    int front;           //定义一个头指针
    int rear;            //定义一个尾指针

public:
    Stack(){front = rear = 0;}  //清空队列
    void pushqueue(int e);      //入队
    int popqueue();             //出队
    void show();                //展示
    int lengthqueue();          //求队列长度
    bool fullqueue();            //判满
    bool emptyqueue();           //判空
};

#endif // QUEUE_H

main.cpp

#include"queue.h"


int main()
{
    Stack s1;

    s1.pushqueue(10);
    s1.pushqueue(20);
    s1.pushqueue(30);
    s1.pushqueue(40);

    s1.show();

    s1.popqueue();

    s1.show();

    cout<<s1.fullqueue()<<endl;

    cout<<s1.emptyqueue()<<endl;

    cout<<"该队列长度size = "<<s1.lengthqueue()<<endl;
    return 0;
}

test.cpp


#include"queue.h"


void Stack::pushqueue(int e)
{
    data[rear] = e;           //入队头插
    rear = (rear+1)%MAXSIZE;   //尾指针加一
}

int Stack::popqueue()
{
    front = (front + 1)%MAXSIZE;  //尾删
    return 1;
}

void Stack::show()
{
    for(int i=front;i!=rear;i=(i+1)%MAXSIZE)   //循环展示
    {
        cout<<data[i]<<" ";
    }
    cout<<endl;
}

bool Stack::fullqueue()       //判满
{
    return front == (rear + 1)%MAXSIZE;  //队列尾加一等于头表明队列为满
                                      //返回1为满,0为非满
}

bool Stack::emptyqueue()       //判空
{
    return front == rear;       //队列头尾相等,表明队列为空
                                //返回1为空,0为非空
}

int Stack::lengthqueue()       //求队列长度
{
    return((MAXSIZE+rear-front)%MAXSIZE);
}

运行结果:

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值