封装一个循环队列类

#include <iostream>

using namespace std;

#define MAXSIZE 4


class circular_queue{
public:
    void enqueue(int num);
    int dequeue();
    void show();
    int size();
    bool full();
    bool empty();
private:
    int data[MAXSIZE];
    int front = 0;
    int end = 0;
};

void circular_queue::enqueue(int num)   //入队
{
    if(size()==MAXSIZE-1)
    {
        cout<<"队已满"<<endl;
        return ;
    }
    data[end] = num;
    end = (end+1) % MAXSIZE;
}

int circular_queue::dequeue()        //出队
{
    if(empty())
    {
        cout<<"队已空"<<endl;
        return -1;
    }
    int key = data[front];
    front = (front+1) % MAXSIZE;
    return key;
}

void circular_queue::show()      //展示队
{
    for(int i=front;i!=size();i=(i+1)%MAXSIZE)
    {
        cout<<data[i]<<' ';
    }
    cout<<endl;
}

int circular_queue::size()       //队长
{
   return (end+MAXSIZE-front)%MAXSIZE;
}

bool circular_queue::full()      //判满
{
    return (end+1) % MAXSIZE==front;

}

bool circular_queue::empty()     //判空
{
    return front==end;
}

int main()
{
    circular_queue q;
    cout << "*****************************************************" << endl;
    cout << "**********************1、入队*************************" << endl;
    cout << "**********************2、出队*************************" << endl;
    cout << "**********************3、展示队***********************" << endl;
    cout << "**********************4、队长*************************" << endl;
    cout << "**********************5、判断队空**********************" << endl;
    cout << "**********************6、判断队满**********************" << endl;
    cout << "**********************7、退出*************************" << endl;
    cout << "*****************************************************" << endl;
    while(1){

        short key = 0;
        cout<<"input your choice>>>";
        cin>>key;


        switch(key){
        case 1:
            cout<<"请输入入队元素>>>";
            int a;
            cin>>a;
            q.enqueue(a);
            break;
        case 2:
            cout<<"出队元素:"<<q.dequeue()<<endl;
            break;
        case 3:
            q.show();
            break;
        case 4:
            cout<<"队长:"<<q.size()<<endl;
            break;
        case 5:
            cout<<boolalpha<<q.empty()<<endl;
            break;
        case 6:
            cout<<boolalpha<<q.full()<<endl;
            break;
        case 7:
            return 0;
            break;
        default:
            cout<<"输入错误"<<endl;
        }

    }

    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值