Home > OS >  why I can not pop elements from character stack?
why I can not pop elements from character stack?

Time:12-20

I'm writing an example of a stack using a character array in C . when run program it show 3 options. 1st for push, 2nd for pop and 3rd for output. I do not know when I push character into string I cannot pop the the top value.

#include <iostream>
int top = -1;
char stack[10];
int n = 10;
using namespace std;
char push()
{
    string a;

    if (top == n - 1) {
        cout << "stack is full" << endl
             << endl;
    }
    else
        cout << "enter value to push" << endl;
    cin >> a;
    // loop
    for (int i = 0; i < a.length(); i  ) {
        stack[top] = a[i];
        top = top   1;
    }
    for (int i = top; i >= -1; i--) {
        cout << "  " << stack[i] << endl;
    }
    return top;
}
char pop()
{
    char x;
    //  int l;
    //  cin>>l;
    if (top < 0) {
        cout << "stack is empty" << endl;
        return 0;
    }
    else
        for (int i = n; i > -1; i--) {

            stack[top] = x;
            --top;
        }
}

int main()
{
    int choose;

    do {
        cout << endl;
        cout << "choose your option" << endl;
        cout << "1 for push" << endl;
        cout << "2 for pop" << endl;
        cout << "3 for print" << endl;
        cout << "-1 for exit" << endl;
        cin >> choose;
        switch (choose) {
        case (1): {
            push();
            break;
        }
        case (2): {

            pop();
        } break;
        case (3): {
            if (top >= 0) {
                cout << "All values in the Stack are " << endl;
                for (int i = top; i >= -1; i--) {

                    cout << stack[i] << endl;
                }
            }

        } break;
        }
    } while (choose != -1);
    return 0;
}

I do not know why I can not pop element from character stack. I am trying but i cannot figure what is wrong with my code. A little help would be appreciated.

CodePudding user response:

else
{
    char s[10]{};
    stack[top] = s[top];
    top = top - 1;
}

This pop method can pop one by one in your code.

But I don't know why you write and erase values ​​at -1 index.

And the fact that there are no errors is even more surprising.

CodePudding user response:

#include <iostream>
int top = -1;
char stack[10];
int n = 10;
using namespace std;
int push()
{
    string a;

    if (top == n - 1)
    {
        cout << "stack is full" << endl
            << endl;
        return 0;
    }
    else
    {
        cout << "enter value to push" << endl;
        cin >> a;
    }

    for (int i = 0; i < a.length(); i  )
    {
        top = top   1;
        stack[top] = a[i];
    }
    for (int i = top; i >= -1; i--)
    {
        cout << "  " << stack[i] << endl;
    }
    return 0;
}
int pop()
{
    if (top < 0)
    {
        cout << "stack is empty" << endl;
        return 0;
    }
    else
        --top;
    return 0;
}

int main()
{
    int choose;

    do
    {
        cout << endl;
        cout << "choose your option" << endl;
        cout << "1 for push" << endl;
        cout << "2 for pop" << endl;
        cout << "3 for print" << endl;
        cout << "-1 for exit" << endl;
        cin >> choose;
        switch (choose)
        {
        case (1):
        {
            push();
            break;
        }
        case (2):
        {

            pop();
        }
        break;
        case (3):
        {
            if (top >= 0)
            {
                cout << "All values in the Stack are " << endl;
                for (int i = top; i > -1; i--)
                {

                    cout << stack[i] << endl;
                }
            }
        }
        break;
        }
    } while (choose != -1);
    return 0;
}

you don't want to replace the char to delete the char if you want to use the delete operator

for this pop operation just decrement the top value

and I put top increment before value assignment so it prevents putting the value in -1 index

  • Related