c++ - 在C++中实现栈

标签 c++ stack

<分区>

如何用 C++ 编写堆栈代码?我自己试过如下:

#include <iostream>
using namespace std;
#define max 10
class stack{

private:
    int arr[max];
    int top;
public:
    stack(){

        top=-1;//empty initialy stack 

    }

    void push(int i){
        top++;
         if (top<max){

             arr[top]=i;
         }
         else{
             cout<<"stack full"<<endl;
             top--;
    }

    }
int pop(){

    if (top==-1){
         cout<<"stack is emphty");
     return NULL;
    }
    else{
        int data=arr[top];
        arr[top]=NULL;
        top--;

     return data;
}
}
bool empty(){

     return (top==-1);

}
};
int main(){

    stack a;
    a.push(12);
    a.push(30);
    a.push(23);
    a.push(42);
    a.push(100);
    while (!a.empty()){
        a.pop();



    }



     return 0;
}

但是我得到以下错误:

1>------ Build started: Project: stack_implementations, Configuration: Debug Win32 ------
1>  stak_implementation.cpp
1>c:\users\david\documents\visual studio 2010\projects\stack_implementations\stak_implementation.cpp(31): error C2059: syntax error : ')'
1>c:\users\david\documents\visual studio 2010\projects\stack_implementations\stak_implementation.cpp(34): error C2059: syntax error : 'else'
1>c:\users\david\documents\visual studio 2010\projects\stack_implementations\stak_implementation.cpp(34): error C2334: unexpected token(s) preceding '{'; skipping apparent function body
1>c:\users\david\documents\visual studio 2010\projects\stack_implementations\stak_implementation.cpp(42): error C2628: 'stack' followed by 'bool' is illegal (did you forget a ';'?)
1>c:\users\david\documents\visual studio 2010\projects\stack_implementations\stak_implementation.cpp(44): error C2065: 'top' : undeclared identifier
1>c:\users\david\documents\visual studio 2010\projects\stack_implementations\stak_implementation.cpp(31): error C2143: syntax error : missing ';' before '}'
1>c:\users\david\documents\visual studio 2010\projects\stack_implementations\stak_implementation.cpp(47): error C2059: syntax error : '}'
1>c:\users\david\documents\visual studio 2010\projects\stack_implementations\stak_implementation.cpp(47): error C2143: syntax error : missing ';' before '}'
1>c:\users\david\documents\visual studio 2010\projects\stack_implementations\stak_implementation.cpp(47): error C2059: syntax error : '}'
1>c:\users\david\documents\visual studio 2010\projects\stack_implementations\stak_implementation.cpp(56): error C2039: 'empty' : is not a member of 'stack'
1>          c:\users\david\documents\visual studio 2010\projects\stack_implementations\stak_implementation.cpp(4) : see declaration of 'stack'
1>c:\users\david\documents\visual studio 2010\projects\stack_implementations\stak_implementation.cpp(56): fatal error C1903: unable to recover from previous error(s); stopping compilation
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

最佳答案

您在第 31 行有一个杂散的右括号;

    cout<<"stack is emphty");

解决这个问题,看看有多少其他“错误”消失了。

不要担心不能立即发现它。它发生在我身上很多次。您确信您的代码存在严重错误,因此您无法发现微不足道的错别字和拼写错误。

关于c++ - 在C++中实现栈,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3272180/

相关文章:

c++ - 什么是 undefined reference /未解析的外部符号错误,我该如何解决?

c++ - 如何从java调用现有的c++ dll

c++ - 如何通过 HTTPS POST 正确发送二进制数据?

winapi - Windows 函数调用中的堆栈是如何设置的?

c++ - C++ 中 Stack 的重载赋值运算符

C++ 命名空间问题

c++ - C++ 中的优先级堆栈

algorithm - 如何从堆栈中搜索项目?

c - 为递归函数绘制堆栈框架

c++ - 为什么将 unique_ptr 与数组一起使用会导致编译错误?