C++循环队列(例题详解)

循环队列例题:

#include<iostream>
  2 using namespace std;
  3 #define MAX 5
  4
  5 class Stack
  6 {
  7 public:
  8         Stack():rear(0),front(0)
  9         {}
 10         bool IsFull();//为满?
 11         bool IsEmpty();//为空?
 12         bool Input(int);//进
 13         bool output(int&);//出
 14         Stack(Stack&);
 15 public:
 16         int buf[MAX];
 17         int rear;
 18         int front;
 19 };
 20
 21 bool Stack::IsFull()
 22 {
 23         if((rear+1)%MAX==front)
 24                 return true;
 25         return false;
 26 }
 27 bool Stack::IsEmpty()
 28 {
 29         if(rear==front)
 30                 return true;
 31         return false;
 32 }
 33 bool Stack::Input(int i)
 34 {
 35         if(IsFull())
 36                 return false;
 37         buf[rear]=i;
 38         rear=(++rear)%MAX;
 39         return true;
 40 }
 41 bool Stack::output(int & i)
 42 {
 43         if(IsEmpty())
 44                 return false;
 45         i=buf[front];
 46         front=(++front)%MAX;
 47         return true;
 48 }
 49
 50 Stack::Stack(Stack& s)
 51 {
 52         front=0;
 53         rear=0;
 54         *this=s;
 55 }
 56
 57 int main()
 58 {
 59         Stack s;
 60         int i;
 61         for(i=0;i<5;i++)
 62                 if(s.Input(i)==true)
 63                         cout<<i<<endl;
 64                 else
 65                         cout<<"失败"<<endl;
 66         int value;
 67         Stack s1(s);
 68
 69         while(s1.output(value))
 70                 cout<<value<<endl;
 71
 72 }

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

浅风叶落

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值