1

If suppose i want to implement a stack in c++ using arrrays is it better to do it via making a structure or class for storing the location of head and stuff like that or should you implement in more of a hard code style like this -

#include <iostream>
using namespace std;
int stack[100], n=100, top=-1;
void push(int val) {
   if(top>=n-1)
   cout<<"Stack Overflow"<<endl;
   else {
      top++;
      stack[top]=val;
   }
}
void pop() {
   if(top<=-1)
   cout<<"Stack Underflow"<<endl;
   else {
      cout<<"The popped element is "<< stack[top] <<endl;
      top--;
   }
}
void display() {
   if(top>=0) {
      cout<<"Stack elements are:";
      for(int i=top; i>=0; i--)
      cout<<stack[i]<<" ";
      cout<<endl;
   } else
   cout<<"Stack is empty";
}
int main() {
   int ch, val;
   cout<<"1) Push in stack"<<endl;
   cout<<"2) Pop from stack"<<endl;
   cout<<"3) Display stack"<<endl;
   cout<<"4) Exit"<<endl;
   do {
      cout<<"Enter choice: "<<endl;
      cin>>ch;
      switch(ch) {
         case 1: {
            cout<<"Enter value to be pushed:"<<endl;
            cin>>val;
            push(val);
            break;
         }
         case 2: {
            pop();
            break;
         }
         case 3: {
            display();
            break;
         }
         case 4: {
            cout<<"Exit"<<endl;
            break;
         }
         default: {
            cout<<"Invalid Choice"<<endl;
         }
      }
   }while(ch!=4);
   return 0;
}

Im just trying to know what is a more accepted method.

5
  • Unfortunately, if you ask ten C++ developers "what is a more accepted method for <X>" you are guaranteed to get at least eleven different answers. You are asking for opinions, and that's not an appropriate question for Stackoverflow, which is not about opinion polls, only fact-based questions on technical, programming topics. Commented Aug 26, 2022 at 18:01
  • "or should you implement in more of a hard code style like this" - Well, imagine if you want to have more than one stack in a program. The style presented as is here would make that difficult/impossible. As to struct vs class, google those to see what little difference there is. Commented Aug 26, 2022 at 18:02
  • If you want more than one stack, a class type could be useful - like std::stack. Commented Aug 26, 2022 at 18:03
  • If you ask seasoned C++ developers that aren't building on embedded systems where the rules become bizarre, how they would do this, they'd tell you to use std::stack<int> and worry about more important things that aren't already solved by existing containers/algorithms. Commented Aug 26, 2022 at 18:04
  • If possible, I recommend you invest in some good C++ books and learn about classes. And once you have implemented a nice working stack class, throw it away and use the standard std::stack. Commented Aug 26, 2022 at 18:05

1 Answer 1

6

The approach you've taken here using global variables is fine for a simple implementation, but it has a major drawback in most real-world applications: it's not reusable.

What if you need two stacks in your program? That would require creating a second set of global variables and a second set of functions to act on them.

That is the problem that using a class solves. If you wrap all of your stack's state in a class then you can create a single set of functions that can operate on any object of that class. Then creating a second stack is very simple.

Of course, for most real world applications you shouldn't implement your own stack anyway. Just use std::stack unless you have a very compelling reason not to. But that still supports the same conclusion. Because std::stack is a self-contained, reusable class any program can use it without having to re-implement their own stack logic (possibly multiple times).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.