循环队列例题:
#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 }