Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Implementation of two stacks in one array...
- #include <iostream>
- #include <stdlib.h>
- using namespace std;
- int SIZE, topA, topB, *arr, N1, N2;
- void stacks_initiate() {
- topA = -1;
- topB = SIZE;
- arr = new int[SIZE];
- }
- void push_A(int);
- void push_B(int);
- int pop_A();
- int pop_B();
- int top_A();
- int top_B();
- void show_A();
- void show_B();
- void menu();
- int main() {
- cout << "Stack size 01 : "; cin >> N1;
- cout << "Stack size 02 : "; cin >> N2;
- SIZE = N1 + N2;
- stacks_initiate();
- menu();
- return 0;
- }
- void push_A(int x) {
- if (topB == topA + 1)
- cout << "\nStack overflow. [ARRAY is full]" << endl;
- else if (topA == N1 - 1) {
- cout << "\nStack(A) overflow. It seems you can still push into the other stack from the menu.\n";
- }
- else {
- arr[++topA] = x;
- cout << "\nData " << x << " has been pushed into stack A." << endl;
- }
- }
- void push_B(int x) {
- if (topB == topA + 1)
- cout << "\nStack overflow. [ARRAY is full]" << endl;
- else if (topB == SIZE - N2) { // or simply top == N1 could be used..
- cout << "\nStack(B) overflow. It seems you can still push into the other stack from the menu.\n";
- }
- else {
- arr[--topB] = x;
- cout << "\nData " << x << " has been pushed into stack B." << endl;
- }
- }
- int pop_A() {
- if (topA == -1)
- cout << "\nStack underflow. [0 element in stack A]" << endl;
- else {
- int x = top_A();
- arr[topA--] = NULL;
- return x;
- }
- }
- int pop_B() {
- if (topB == SIZE)
- cout << "\nStack underflow. [0 element in stack B]" << endl;
- else {
- int x = top_B();
- arr[topB++] = NULL;
- return x;
- }
- }
- int top_A() {
- return arr[topA];
- }
- int top_B() {
- return arr[topB];
- }
- void show_A() {
- if (topA == -1) cout << "\nStack A is empty." << endl;
- else {
- cout << "\nStack A : ";
- for (int i = topA; i >= 0; i--)
- {
- cout << arr[i] << " ";
- }
- cout << endl;
- }
- }
- void show_B() {
- if (topB == SIZE) cout << "\nStack B is empty." << endl;
- else {
- cout << "\nStack B : ";
- for (int i = topB; i < SIZE; i++)
- {
- cout << arr[i] << " ";
- }
- cout << endl;
- }
- }
- void menu() {
- cout << "\n01. Push into A.\n02. Push into B.\n03. Pop from A.\n04. Pop from B.\n05. Get top from A.\n06. Get top from B.\n07. Show A.\n08. Show B.\n09. EXIT." << endl;
- cout << "\nChoice : "; int ch; int x;
- cin >> ch;
- switch (ch) {
- case 1: cout << "\nWhat do you want to push into A? Push : ";
- cin >> x;
- push_A(x); menu();
- break;
- case 2: cout << "\nWhat do you want to push into B? Push : ";
- cin >> x;
- push_B(x); menu();
- break;
- case 3: if (topA == -1) cout << "\nStack A is empty." << endl;
- else cout << "\nData " << pop_A() << " has been removed from stack A.\n"; menu();
- break;
- case 4: if (topB == SIZE) cout << "\nStack B is empty." << endl;
- else cout << "\nData " << pop_B() << " has been removed from stack B.\n"; menu();
- break;
- case 5: if (topA == -1) cout << "\nStack A is empty." << endl;
- else cout << "\nData at top of stack A : " << top_A() << endl; menu();
- break;
- case 6: if (topB == SIZE) cout << "\nStack B is empty." << endl;
- else cout << "\nData at top of stack B : " << top_B() << endl; menu();
- break;
- case 7: show_A(); menu();
- break;
- case 8: show_B(); menu();
- break;
- case 9: cout << "\nThank You..! Terminating Program...\n" << endl;
- delete []arr;
- exit(0);
- break;
- default: cout << "\nInvalid choice. Try again." << endl;
- menu();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement