如何在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 ==========发布于 2010-07-18 00:14:40
如果您只在需要时更改状态(变量、成员等),则代码将不容易出错,并且更易于阅读。因此,如果您将push函数重写为
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将被转换为int,并且赋值将被解释为arr[top]=0。您可以完全删除赋值,因为元素一旦弹出就在堆栈之外,因此它是否为零并不重要。
顺便说一句,我会使用top作为指向下一个可用堆栈项的指针,而不是指向最后使用的堆栈项的指针,以避免保留的“非指针”值为-1。
发布于 2010-07-17 23:51:31
你在第31行有一个零散的右括号;
cout<<"stack is emphty");修复它,看看有多少其他的“错误”消失了。
不要担心不能立即发现它。这在我身上发生过很多次了。你确信你的代码中有一些严重的错误,所以你没有发现一些微不足道的打字错误和拼写错误。
发布于 2010-07-17 23:53:41
正如您的编译器所报告的,您在第31行有一个不平衡的括号。
cout<<"stack is emphty");请仔细阅读这些编译器错误。他们通常立即指出问题所在。
https://stackoverflow.com/questions/3272180
复制相似问题