简体   繁体   中英

“top” value won't change how many times ever i push or pop a stack

I was implementing stack using dynamic array. My idea is simple: I want the user to enter his/her desired length then create a stack with that length. Then he/she can push, pop an integer; see the top and max value (for debugging purpose); check if it is full or empty (also for debugging). He/She can push until the stack is full then must pop, he/she can pop until it's empty then must push. I use a do while and switch to make it easier debugging and manipulating. But when i started testing, my top attribute of my Stack class remained 0 how many times ever i pushed or popped. I have been trying to find the error but still haven't found it.

Here is my code:

#include <cstdio>
#include <iostream>

using std::cin;
using std::cout;
using std::endl;

class Stack {
    int top;
    int max; // max index
    int *data;
    public:
        Stack(int length);
        int isEmpty();
        int isFull();
        int push(int n);
        int pop();
        int getTop(void);
        int getMax(void);
        ~Stack(void);
};

Stack::Stack(int length){
    top = 0;
    max = length;
    data = new int[length+1];
}
int Stack::isEmpty(void){
    return top==0;
}
int Stack::isFull(void){
    return top==max;
}
int Stack::push(int n){
    if(!isFull()){
    data[++top] = n;
    return 1;
    }
    else return 0;

}
int Stack::pop(void){
    if(!isEmpty()){
        return data[top--];
    }
    else return -911; //rare and indicative number
}
int Stack::getTop(void){
    return top;
}
int Stack::getMax(void){
    return max;
}
Stack::~Stack(void){
    delete[] data;
}

int main(void){
    int length = 0;
    cout << "Enter stack's length': "; cin >> length;
    Stack list(length);
    char lock;
    do{
        cout << "1. push" << endl;
        cout << "2. pop" << endl;
        cout << "3. show top index" << endl;
        cout << "4. show max index" << endl;
        cout << "5. isFull?" << endl;
        cout << "6. isEmpty?" << endl;
        cout << "0. Quit" << endl;
        scanf("%d", &lock);
        switch(lock){
            case 1:
                int temp;
                cout << "Enter an integer: ";
                cin >> temp;
                printf(list.push(temp)?"success\n":"fail\n");
                break;
            case 2:
                cout << "Top's data: " << list.pop() << endl;
                break;
            case 3:
                cout << list.getTop() << endl;
                break;
            case 4:
                cout << list.getMax() << endl;
                break;
            case 5:
                printf(list.isFull()?"True\n":"False\n");
                break;
            case 6:
                printf(list.isEmpty()?"True\n":"False\n");
                break;
            case 0: break;
            default:
                cout << "Not an available work!" << endl;
        }
    } while (lock!= 0);

    return 0;
}

You have subtle memory corruption. The cause:

q58036810.cpp:71:21: warning: format specifies type 'int *' but the argument has type 'char *' [-Wformat]
        scanf("%d", &lock);
               ~~   ^~~~~
               %s

When you're entering numbers at the menu, it's too big for lock and so is overwriting the memory that stores top inside of list . Change char lock to int lock and the problem should go away. In the future, always compile with warnings and heed them. Also, by the nature of this problem, it won't be consistently reproducible (ie, it seems to work fine on my machine.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM