Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 932119
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T20:36:47+00:00 2026-05-15T20:36:47+00:00

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

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T20:36:48+00:00Added an answer on May 15, 2026 at 8:36 pm

    The code will be less error prone, and clearer to read, if you only change state (variables, members etc) when you need to. So, if you rewrite the push function into

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

    it’ll be cleaner. Also, const is type safe, whereas #define is not, so const int max=10 is preferrable to #define max 10.

    Since the stack is a stack of integers, NULL in your assignment arr[top]=NULL will be cast into an int and the assignment will be interpreted as arr[top]=0. You could remove the assignment altogether, since the element is outside the stack once popped, and thus it doesn’t matter whether it’s zero or not.

    BTW, I’d use top as a pointer to the next available stack item rather than a pointer to the last used one, in order to avoid having a reserved ‘non pointer’ value of -1.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can anybody help me out with this exception. I have tried couple of fixes
I have tried setting this up based on previously asked questionshere on stack overflow
I'm stuck on one thing i can't get solved. I have part of code,
I am stuck on this one piece of code because I can't get the
In the aspx code view , we can code like this: <asp:ListBox runat=server> <asp:ListItem
I am using XAMPP and was wondering if I can code my site to
I have tried looking in other areas, but I can't seem to figure out
Actually I am stuck in middle of some mysql code. Can anyone suggest a
I can code in Java, and I'm trying to understand the wiki article on
One thing I lack understanding on is how can code compiled for an Intel

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.