Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

implement stack in c++ [closed]

Tags:

c++

stack

How can I code a stack in C++? I have tried this myself as follows:

#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;
}

But I get the following errors:

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 ==========

1 Answers

You've got a stray close bracket on line 31;

    cout<<"stack is emphty");

Fix that and see how many of the other "errors" disappear.

Don't worry about not spotting it straight away. It's happened to me many times. You're convinced that something serious is wrong with your code so you fail to spot the trivial typos and spelling mistakes.

like image 185
ChrisF Avatar answered Jan 28 '26 01:01

ChrisF



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!