how to implement stack c++

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

0
0

                                    // CPP program to illustrate 
// Implementation of push() function 
#include &lt;iostream&gt; 
#include &lt;stack&gt; 
using namespace std; 
  
int main() 
{ 
    // Empty stack 
    stack&lt;int&gt; mystack; 
    mystack.push(0); 
    mystack.push(1); 
    mystack.push(2); 
  
    // Printing content of stack 
    while (!mystack.empty()) { 
        cout &lt;&lt; ' ' &lt;&lt; mystack.top(); 
        mystack.pop(); 
    } 
} 

0
0
4
3

                                    #include &lt;bits/stdc++.h&gt; 

stack&lt;int&gt; stk;
stk.push(5);
int ans = stk.top(5); // ans =5
stk.pop();//removes 5

4 (3 Votes)
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
stack using array c++ class stack in c++ stack inc++ how to make the stack in cpp c++ include stack how to display a stack in c++ how to define stack in c++ c++ stack for making applications take stack in c++ call stack implementation in c++ c++ built in stack is implemented using all the functions in stack in c++ implement stack from scratch in c++ stack basic operations c++ use stack in c++ c++ stack operations stack implementation using class in c++ build stack in c++ c++ stack operations code c++ stack data structure stl stack in c++ stack implemention in c++ c++ stack implementation implement stack in c++ c++ stack documentation implementing stack using c++ application of stack with example in c++ c++ std stack example declare stack in c++ implementation of stack c++ stack in cpp? import stack c++ create a new stack in cpp stack stl in cpp how is a stack implemented in c++ Stack C++ example stack using array in c ++ what's stack in c++ stack adt in c++ importing stack in c++ stack cpp program how stack in made in c++ stack cpp stl create stack in c++ Stack implementation using array in C++ c++ stack example function call stack in c++ call stack in c++ stack in data structure c++ stack stl cpp defining a stack in c++ stack simple example in c++ stack in c++ for beginners how to implement stack c++ what is call stack in c++ Stack ds in c++ stack implementation cpp stack c+= functions stack basics in cpp stack syntax in cpp C++ stack structure stack code in cpp Stack (C++) stack declaration in c++ stack operations stl c++ hwo to implement stack in c++ stack code c++ how to use stack in c++ stack in c++ code what is stack in c++ stack class c++ stack c++ builtin stack int c++ stack in stl cc++ stack cplusplus how to include stack in cpp stack implementation in c++ stack methods in c++ how to create stack in c++ making a stack in c++ stack methods c++ stack class in c++ cpp stack stl in c stack definition in c++ stack in c++ stl methods c++ stack.h functions in c++ stack stl stack in c++ stl basic stack in c++ c++ stack oop c++ stack in class stack in class c++ declaring a stack in c++ stack class cpp stack library in cpp how to create a stack class in c++ writing a stack in c++ c++ stack code class stack C++ stack operation c++ stack algorithm in c++ stack in c++ cppreference c++ stack functions stack implementation c++ cpp stl stack library of stack in c++ creating a stack class in c++ stack c++ reference stack operations in cpp stack functions in c++ what is a stack in c++ hpw to define a stack c++ include stack c++ stack class implementation c++ stack inb c++ how to use a stack in c++ how to declare a stack in c++ program using stack in C++ stack method in c++ stack functions c++ create stack c++ stack c++ implementation stack cpp stack in c++; stack stl functions c++ stack \ stack ++ stack implementation in cpp stack begin c++ std::stack.back satck stl cpp stack &quot;used as an underlying container for the stack&quot; how to stack c++ c++ operations used in stack data structure stl STL FILO container how to initialize a stack in c++ stack in c++ stack header file in c++ how to access stack elements in c++ stacks cpp stack std stack stl functions c++ stack library c++ stack stl c++ stack char c++ does c++ have a stack type stack c++ stl std stack c++ stack c_++ c++ stack container variable c++ stack.end stack class functions c++ the stack c++ stl in c for stack stack stl c stack functions in cpp class functions c++ stack stl stack underlying data structure stack stl in c++ stack in cpp c++ what to use a stack on reference stack include stack define stacks in cpp stack.back() c++ stl stack stack use in cpp a stack class has member functions for what c++ stl stack methods stack&lt;char, list&lt;char&gt;&gt; containers c++ c++ stack peek stack in stkl stack stl funtions what is the fron tof a stack c++ c++ stack c++ stack insert std stack stack stl c++ stack std stacks stack c++
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source