You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

数组实现Stack时推入最大元素数出现异常问题求助

问题解决:数组实现栈的display函数异常

问题描述

用C++实现基于数组的栈时,当推入与数组容量相同数量的元素后,调用display函数会输出错误提示(显示"Stack Underflow !"和"No Element Present in Stack"),而非正常显示栈中元素。

原代码

#include<iostream>
using namespace std;

class Stack{
private:
    int* arr;
    int top;
    int size;

public:
    Stack(int s){ // 参数化构造函数
        this->size = s;
        arr = new int[s];
        for (int i = 0; i < size; i++){
            arr[i] = 0;
        }
        top = -1;
    }

    ~Stack( ){ // 析构函数
        delete[ ]arr;
        arr = NULL;
    }

    void push(int num);
    void pop( );
    void display( );
};

void Stack::push(int num){
    if (top == size - 1){
        cout << "Stack Overflow !" << endl;
    }
    else{
        top++;
        arr[top] = num;
    }
}

void Stack::pop( ){
    if (top == -1){
        cout << "Stack is Already Empty ." << endl;
    }
    else{
        cout << "Last Element Removed from Stack " << endl;
        arr[top] = 0;
        top--;
    }
}

void Stack::display( ){
    if (top == size - 1){
        cout << "Stack Underflow !" << endl;
        cout << "No Element Present in Stack" << endl;
    }
    else{
        for (int i = 0; i < size; i++){
            cout << arr[i] << "  ";
        }
    }
    cout << endl;
}


int main( ){
    int choice = 0;
    int size , value , number;

    cout << "Program for Implementation of Stack using Arrays : \n" << endl;
    cout << "Enter Size of Stack : ";
    cin >> size;

    Stack s1(size);

    do{
        cout << endl;
        cout << "1. Push : Add Element at Last in Stack " << endl;
        cout << "2. Pop : Remove Last Element from Stack " << endl;
        cout << "3. Display all Element from Stack " << endl;
        cout << "4. Exit Program " << endl << endl;

        cout << "Enter Your Choice : ";
        cin >> choice;
        cout << endl;

        switch (choice){
            case 1: {
                cout << "Enter Number of Elements to Push : ";
                cin >> number;
                cout << "Enter " << number << " Element to Add in your Stack : ";
                for (int i = 0; i < number; i++){
                    cin >> value;
                    s1.push(value);
                }
                break;
            }

            case 2: {
                cout << "Enter Number of Elements to Pop : ";
                cin >> number;
                for (int i = 0; i < number; i++){
                    s1.pop( );
                }
                break;
            }

            case 3: {
                cout << "Displaying all Element from Stack : " << endl;
                s1.display( );
                break;
            }

            case 4:{
                cout << "Exiting Program ." << endl;
                break;
            }

            default: {
                cout << "Invalid Choice !" << endl;
            }
        }
    } while (choice != 4);

    return 0;
}

问题分析

display函数存在两处核心逻辑错误:

  1. 栈状态判断完全颠倒:代码中if (top == size - 1)是栈满的判断条件(top指向最后一个有效元素),但代码错误输出了栈空(Underflow)的提示,混淆了栈满和栈空的判断逻辑。栈空的正确判断条件是top == -1。
  2. 遍历范围错误:else分支遍历整个数组的size长度,会把未被推入栈的初始化0值也显示出来,正确做法是只遍历到top索引位置,展示栈中实际存在的元素。

修复后的display函数

void Stack::display( ){
    if (top == -1){
        cout << "Stack Underflow !" << endl;
        cout << "No Element Present in Stack" << endl;
    }
    else{
        cout << "Stack elements: ";
        for (int i = 0; i <= top; i++){
            cout << arr[i] << "  ";
        }
    }
    cout << endl;
}

额外优化建议

  • 若要贴合栈"后进先出"的特性,可以修改循环为从栈顶到栈底输出:for (int i = top; i >= 0; i--)。
  • 初始化数组时可省略循环,直接用arr = new int[s]();,会自动将元素初始化为0,简化代码。

内容的提问来源于stack exchange,提问作者Abhishek Yadav

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.19 03:42:35