2010-07-17 144 views
1

如何在C++中编写堆栈?在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 ========== 
+0

这是用于学校作业吗?一个好主意可能是为了便于阅读而更好地压缩代码。 – 2010-07-17 15:53:22

+0

不要忘记commententing,你也可以使用C++ const,也许你以后可以作为一个额外的程序作为模板,并在堆栈已满时抛出异常。 – Quonux 2010-07-17 15:57:11

回答

1

如果您只是在需要时更改状态(变量,成员等),代码将不太容易出错,并且读起来更清晰。因此,如果您将推入功能改写为

void push(int i){ 
    if (top<max-1){ 
     arr[++top]=i; 
    } 
    else{ 
     cout<<"stack full"<<endl; 
    } 
} 

它会更干净。此外,const是类型安全的,而#define不是,所以const int max=10对于#define max 10是可取的。

由于堆栈是一堆整数,因此您的作业arr[top]=NULL中的NULL将被转换为整型,并且该作业将被解释为arr[top]=0。你可以完全删除这个赋值,因为元素一旦弹出就在栈外,因此无论它是否为零都没关系。

顺便说一句,我会使用top作为指向下一个可用堆栈项目的指针,而不是指向最后一个使用堆栈项目的指针,以避免保留“非指针”值为-1。

5

你已经有了31行流浪接近支架;

cout<<"stack is emphty"); 

修复该问题,看看有多少其他“错误”消失。

不要担心不会马上发现它。这发生在我身上很多次。你确信你的代码有严重错误,所以你不能发现错误的拼写错误和拼写错误。

+0

是的,我已经修复它,谢谢大家 – 2010-07-17 15:55:36

1

您的编译器报告了第31行的括号不平衡。

cout<<"stack is emphty"); 

请仔细阅读这些编译错误。他们通常会立即指出问题的症结所在。