C++ stack push pop implementation source code. It uses two stacks, user can push pop to both using menu selection. Menu have options 0 to 9 for example push value to stack one , pop value to stack two, display both etc. Switch statement has used for menu options. Both are the size of 10 user can increase their size according to need. There is an option to build stack. It uses for loop to assign 5 values to each.
Concept used
Concept used
- Stack push pop
- Dynamic memory allocation
- array
- for loop
- switch statement
#include <iostream> #include <stdio.h> #include <conio.h> #include <stdlib.h> #include <string.h> using namespace std; ////////////////////////// int ts = 10; int len = 10; int top1 = -1; int top2 = -1; int ch = 0; int* exp = new int[ts]; int* stk1 = new int[len]; int* stk2 = new int[len]; void inputexp(); void push1(int); void push2(int); void pop1(); void pop2(); int topitem1(); int topitem2(); void displaysk1(); void displaysk2(); void menu(); void buildstack(); ///////////////////////////////// void menu() { int op; system("pause"); system("cls"); cout << "press 0 to enter array:\n"; cout << "press 1 to push element to stack 1:\n"; cout << "press 2 to push element to stack 2:\n"; cout << "press 3 to get top stack 1:\n"; cout << "press 4 to get top stack 2:\n"; cout << "press 5 to pop from stack 1 and push in 2:\n"; cout << "press 6 to pop from stack 2 and push in 1:\n"; cout << "press 7 to display sk 1:\n"; cout << "press 8 to display sk 2:\n"; cout << "press 9 to build stack:\n"; cout << "option:"; cin >> op; switch (op) { case 0: { inputexp(); menu(); } break; case 1: { int it; cout << "enter value:"; cin >> it; push1(it); menu(); } break; case 2: { int it; cout << "enter value:"; cin >> it; push2(it); menu(); } break; case 3: { int t = 0; t = topitem1(); cout << "top is:" << t << endl; menu(); } break; case 4: { int t = 0; t = topitem2(); cout << "top is:" << t << endl; menu(); } break; case 5: { pop1(); menu(); } break; case 6: { pop2(); menu(); } break; case 7: { displaysk1(); menu(); } break; case 8: { displaysk2(); menu(); } break; case 9: { buildstack(); cout << "stacks successfully built\n"; menu(); } break; default: cout << "invalid\n"; break; } //switch } //funciton ///////////////////////////////// void push1(int ch) { ++top1; stk1[top1] = ch; } //function ////////////////////////// void push2(int ch) { ++top2; stk2[top2] = ch; } //function /////////////////////////// void pop1() { ch = stk1[top1]; push2(ch); top1--; } //function ////////////////////////// void pop2() { ch = stk2[top2]; push1(ch); top2--; } //function ////////////////////////// int topitem1() { return stk1[top1]; } //function ////////////////////////// int topitem2() { return stk2[top2]; } //function ////////////////////////// void inputexp() { int i; cout << "enter array:\n"; for (i = 0; i < ts; i++) { cin >> exp[i]; } exp[i] = '\0'; cout << "successfully entered\n"; cout << "array u entered is \n"; for (int j = 0; j < ts; j++) cout << exp[j] << endl; } //funciton /////////////////////////// void buildstack() { for (int k = 0; k < 10; k++) { if (exp[k] % 2 == 0) { push1(exp[k]); } else { push2(exp[k]); } } //for } //function /////////////////////////////// void displaysk1() { cout << "stack 1 is:\n"; for (int j = 0; j <= top1; j++) { cout << stk1[j] << endl; } cout << endl; } /////////////////////////// void displaysk2() { cout << "stack 2 is:\n"; for (int k = 0; k <= top2; k++) { cout << stk2[k] << endl; } } //////////////////////////////// //////////////////////////////// int main() { menu(); getch(); }
This is a simple small c++ project code it can be edit or improve more its basic purpose is just to give the idea to how stack push pop works. How it can be implement in code using array, dynamic memory and others.
find more c++ simple projects and solution
find more c++ simple projects and solution