Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ConsoleApplication9.cpp : 定義主控台應用程式的進入點。
- //
- #include "stdafx.h"
- #include <iostream>
- using namespace std;
- #define MAX 5
- class queue{
- public:
- void enqueue(int);
- void show(void);
- void dequeue(void);
- private:
- int cq[MAX];
- int start=MAX-1;
- int end = MAX - 1;
- bool tag=0;
- };
- void queue::show(void)
- {
- cout << "\nQueue list: ";
- if (tag == 0)
- {
- for (int i = (start + 1) % MAX; i != (end + 1) % MAX; i = (i + 1) % MAX)
- {
- cout << cq[i];
- }
- }
- else
- {
- int i = (start + 1) % MAX;
- cout << cq[i];
- i = (i + 1) % MAX;
- for (; i != (end + 1) % MAX; i = (i + 1) % MAX)
- {
- cout << cq[i];
- }
- }
- cout << endl;
- for (int i = 0; i < MAX; i++)
- {
- cout << cq[i];
- if (i == start)
- cout << " <-start";
- if (i == end)
- cout << " <-end";
- cout << endl;
- }
- }
- void queue::enqueue(int in)
- {
- if (start == end && tag == 1)
- cout << "\nQueue is full when inserting " << in << "!\n";
- else{
- end = ( end+1 ) % MAX;
- cout << "\ninsert "<<in<<"\n";
- cq[end]=in;
- if (start == end)
- tag = 1;
- }
- }
- void queue::dequeue(void)
- {
- if (start == end&&tag == 0)
- {
- cout << "\nQueue is empty!\n";
- }
- else
- {
- start = (start + 1) % MAX;
- cout << "\nDeque " << cq[start] << endl;
- tag = 0;
- }
- }
- int main()
- {
- queue qu1;
- for (int i = 0; i < 3; i++)
- {
- qu1.enqueue(i+1);
- qu1.show();
- }
- for (int i = 0; i < 2; i++)
- {
- qu1.dequeue();
- qu1.show();
- }
- for(int i = 0; i < 5; i++)
- {
- qu1.enqueue(i + 4);
- qu1.show();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement