Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "Stack.h"
- #include <iostream>
- using namespace std;
- class Queue
- {
- Stack<int> s1, s2;
- int size;
- public:
- Queue()
- {
- size = 0;
- }
- bool Push(int data)
- {
- bool ok = s1.Push_Stack(data);
- if (!ok)
- {
- return false;
- }
- else
- {
- return true;
- }
- }
- bool Pop(int &a)
- {
- if (s1.isEmpty())
- return false;
- int temp;
- while (s1.Size_Stack() != 1)
- {
- s1.Pop_Stack(temp);
- s2.Push_Stack(temp);
- }
- s1.Pop_Stack(a);
- while (s2.Size_Stack() != 0)
- {
- s2.Pop_Stack(temp);
- s1.Push_Stack(temp);
- }
- return true;
- }
- // Lấy đỉnh của Queue
- bool Top(int &a)
- {
- if (s1.isEmpty())
- return false;
- int temp;
- while (s1.Size_Stack() != 1)
- {
- s1.Pop_Stack(temp);
- s2.Push_Stack(temp);
- }
- s1.Peak_Stack(a);
- while (s2.Size_Stack() != 0)
- {
- s2.Pop_Stack(temp);
- s1.Push_Stack(temp);
- }
- return true;
- }
- int Size()
- {
- return s1.Size_Stack();
- }
- bool isEmpty()
- {
- return Size() == 0;
- }
- void Display()
- {
- cout << "\tQueue: ";
- s1.Display_Items();
- }
- };
- int main()
- {
- Queue Q;
- int choice = -1;
- int n, temp;
- bool ok;
- while (choice != 0)
- {
- cout << "Nhap lua chon cho queue: " << endl;
- cout << "0. Thoat khoi chuong trinh." << endl;
- cout << "1. Push." << endl;
- cout << "2. Pop." << endl;
- cout << "3. Top." << endl;
- cout << "4. Size. " << endl;
- cout << "5. Check empty." << endl;
- cout << "6. Display items." << endl;
- cin >> choice;
- switch (choice)
- {
- case 0:
- system("pause");
- return 0;
- case 1:
- cout << "\tNhap gia tri can push vao queue: ";
- cin >> n;
- ok = Q.Push(n);
- if (ok)
- {
- cout << "\t";
- cout << n << " da duoc push vao." << endl;
- }
- else
- {
- cout << "Khong the push!!!" << endl << endl;
- }
- break;
- case 2:
- ok = Q.Pop(temp);
- if (!ok)
- cout << "\tQueue trong." << endl;
- else
- cout << "\t" << temp << " duoc pop ra khoi queue. " << endl;
- break;
- case 3:
- ok = Q.Top(temp);
- if (!ok)
- cout << "\tQueue trong." << endl;
- else
- cout << "\t" << temp << " la top cua queue. " << endl;
- break;
- case 4:
- cout << "\tKich thuoc cua queue la: " << Q.Size() << endl;
- break;
- case 5:
- ok = Q.isEmpty();
- if (!ok)
- cout << "\tQueue khong rong." << endl;
- else
- cout << "\tQueue rong!!!" << endl;
- break;
- case 6:
- ok = Q.isEmpty();
- if (ok)
- cout << "\tQueue rong!!!" << endl;
- else
- Q.Display();
- break;
- default:
- break;
- }
- }
- system("pause");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement