Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- class queue {
- public:
- int* a, r, size;
- queue() {
- r = 0;
- cout << "\nEnter size of the Queue\n";
- cin >> size;
- cout << "\n\n";
- a = new int[size];
- }
- int isempty();
- int isfull();
- int push();
- int pop();
- int display();
- };
- /**
- * Check if the array is empty.
- */
- int queue::isempty() {
- if (r == -1)
- return 1;
- return 0;
- }
- /**
- * Check if the array is full.
- */
- int queue::isfull() {
- if (r == (size))
- return 1;
- return 0;
- }
- /**
- * Insert a value to the array.
- */
- int queue::push() {
- if (isfull()) {
- cout << "\nQueue Overflow\n\n";
- return 0;
- }
- else {
- cout << "\nEnter an element: ";
- int val;
- cin >> val;
- a[r] = val;
- cout << val << " has been successfully added \n\n";
- r++;
- display();
- }
- }
- /**
- * Remove the last element of the array.
- */
- int queue::pop() {
- if (isempty()) {
- cout << "\n\nQueue Overflow\n";
- return 0;
- }
- cout << a[r-1] << " has been removed.";
- for (int i = 0; i <= r; i++) {
- a[i] = a[i];
- }
- r--;
- display();
- cout << "Element deleted sucessfully\n";
- }
- /**
- * Print all the items inside the array.
- */
- int queue::display() {
- if (isempty()) {
- cout << "\n\n\nQueue is empty -- No Element to display\n";
- return 0;
- }
- else {
- cout << "\n==================================\n";
- cout << "Items in Queue:\n";
- cout << "==================================\n";
- for (int i = 0; i < r; i++) {
- cout << "[" << i << "] | " << a[i] << "\n";
- cout << "----------------------------------\n";
- }
- cout << "\n";
- }
- }
- /**
- * Main function.
- */
- int main() {
- int i = 0, s;
- queue q;
- while (i != 1) {
- cout << "\n********* MENU *******\n";
- cout << "\n1. Push\n2. Pop\n3. Display\n4. Exit\n";
- cout << "\n********* MENU *******\n";
- cout << "Enter your option: ";
- cin >> s;
- switch (s) {
- case 1:
- q.push();
- break;
- case 2:
- q.pop();
- break;
- case 3:
- q.display();
- break;
- case 4:
- i = 1;
- break;
- default:
- cout << "\nEnter correct option\n|";
- break;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement