-->





c++ stack push pop example project

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
  • 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();
}

Sample Output:


c++ stack push pop example project


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

3 comments:

  1. This is a really poor example to set for beginning programmers. It doesn't really focus on teaching pushing and popping. In fact, without comments, it doesn't really teach anything. Beginners looking at this code won't know what is important and what is a trivial detail. They will likely assume that is all code worth emulating and it is not.

    Here are a few issues with the code that make it unsuitable as an example:

    It uses non-standard C++ headers: stdio.h, conio.h, stdlib.h, string.h
    It is non portable (use of conio.h and system() calls)
    It has very poor names: (ts, exp)
    It doesn't use "const" for values that are not intended for modification: (ts)
    It uses globals.
    It allocates with new when that is not necessary.
    It allocates with new without using delete.
    It isn't modular and has no encapsulation.
    It has poor or no input validation.
    It uses poor style for incrementing in C++ (post rather than pre).
    Sets a terrible example of how to document functions.

    If all these issues were address, its other issues would become more apparent.

    Some readers might say, "Well this is just intended for beginners." Or "We have to avoid advanced features when showing to beginners because they don't understand them."

    But these comments just illustrate how hard it is to teach beginners. It isn't easier to teach beginners, it is harder. We have to choose examples that can be done with high quality code (that sets a proper example) but is still understandable to beginners. This code just doesn't meet that bar and does a disservice to C++ and to beginners that want to learn it.

    Jon

    ReplyDelete
  2. Fahad u are doing awesome job
    but for beginners u should write a simple program about PUSH then a program about POP and then a program which combine both or a program which act as both.

    Regular reader of you blog

    ReplyDelete