stack implementation in c++ code example

Example 1: c++ stack and queue

#include <iostream>
#include <stack> 
#include <queue>
using namespace std;

void printStack(stack<int> custstack)
{
    for(int i = 0; i < 3; i++)
    {
        int y = custstack.top();
        cout << y << endl;
        custstack.pop();
    }
}

void printQueue(queue<int> custqueue)
{
    for(int i = 0; i < 3; i++)
    {
        int y = custqueue.front();
        cout << y << endl;
        custqueue.pop();
    }
}

int main ()
{
    cout << "Stack:" << endl;
    // this stack stacks three elements one by one and displays each element before its removal
    stack<int> MY_STACK; // define stack and initialize to 3 elements
    MY_STACK.push(69); // last element popped / displayed
    MY_STACK.push(68); // second element popped / displayed
    MY_STACK.push(67); // third popped/displayed
    printStack(MY_STACK);

    cout << endl << "Switching to queue" << endl;

    queue<int> MY_QUEUE;
    MY_QUEUE.push(69); // first out
    MY_QUEUE.push(68); // second out 
    MY_QUEUE.push(67); // third out
    printQueue(MY_QUEUE);
    return 0;
}

Example 2: stack c++

/* Stack is a data structure that provides two O(1) time operations:
adding an element to the top and removing an element from the top.
It is only possible to access the top element of a stack. */
stack<int> s;
s.push(3);
s.push(2);
s.push(5);
cout << s.top(); // 5
s.pop();
cout << s.top(); // 2

Example 3: push pop code in c++

#include<iostream>
using namespace std;
#define Max 100
class stack{
	public:
		int top;
		int size;
		int *s;
		int stack[Max];
		
		void push()
		{
			int value;
			if(top==size-1)
			{
				cout<<"overflow";
			}
			else
			{
				cout<<"Enter value to push \n";
				cin>>value;
				top++;
				stack[top]=value;
			}
		}
		int pop()
		{
			if(top==-1)
			{
				cout<<"Underflow";
			}
			else
			{
				cout<<"Deleted value is \n"<<stack[top];
				top--;
			}
		}
		void display()
		{
			int i;
			for(i=top;i>=0;i--)
			{
				cout<<stack[i]<<endl;
			}
		}
};
int main()
{
	stack st;
	cout<<"Enter the size of the stack";
	cin>>st.size;
	st.s=new int[st.size];
	st.top=-1;
	int ch;
	while(st.size!=0)
	{	
	cout<<endl<<"    #####       STACK MENU     #####     "<<endl;
	cout<<"1. PUSH OPERATION \n2. POP OPERATION \n3. DISPLAY \n4.Exit \n";
	cin>>ch;
	switch(ch)
	{
		case 1: 
		    st.push();
		    break;
		case 2:
			st.pop();
			break;
		case 3:
			st.display();
			break;
		case 4:
			exit(0);
			
		default:cout<<"\n Choose correct option";
	}
}
return 0;
}

Example 4: stack implementation in c

#include <stdio.h>

int MAXSIZE = 8;       
int stack[8];     
int top = -1;            

int isempty() {

   if(top == -1)
      return 1;
   else
      return 0;
}
   
int isfull() {

   if(top == MAXSIZE)
      return 1;
   else
      return 0;
}

int peek() {
   return stack[top];
}

int pop() {
   int data;
	
   if(!isempty()) {
      data = stack[top];
      top = top - 1;   
      return data;
   } else {
      printf("Could not retrieve data, Stack is empty.\n");
   }
}

int push(int data) {

   if(!isfull()) {
      top = top + 1;   
      stack[top] = data;
   } else {
      printf("Could not insert data, Stack is full.\n");
   }
}

int main() {
   // push items on to the stack 
   push(3);
   push(5);
   push(9);
   push(1);
   push(12);
   push(15);

   printf("Element at top of the stack: %d\n" ,peek());
   printf("Elements: \n");

   // print stack data 
   while(!isempty()) {
      int data = pop();
      printf("%d\n",data);
   }

   printf("Stack full: %s\n" , isfull()?"true":"false");
   printf("Stack empty: %s\n" , isempty()?"true":"false");
   
   return 0;
}

Example 5: stack c++

#include <bits/stdc++.h> 

stack<int> stk;
stk.push(5);
int ans = stk.top(5); // ans =5
stk.pop();//removes 5

Tags:

Cpp Example