Stack Example in C++

Advertisements

Stack Pop Operation Queue in C++

Stack Program in C++

Program of stack is very simple when you insert any item in stack top will be increased by 1 and when you pop any item from stack top will be decreased by 1. Both insertion and deletion operation in stack perform from top of stack.

Stack Program in C++

	
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>

#define size 5

void pop();
void push();
void display();

 struct stack
  {
   int item;
   int stack[size];
  }s;

  int top=-1;

  void push()
  {
  if(top==size-1)
  {
  cout<<"\n stack is full";
  }
  else
  {
  top=top+1;
  cout<<"\n\n Enter element in stack: ";
  cin>>s.item;
  s.stack[top]=s.item;
  }
  }

  void pop()
  {
  if(top==0)
  {
  cout<<"\nStack is empty: ";
  }
  else
  {
  s.item=s.stack[top];
  top=top-1;
  cout<<"deleted data is: "cout<<s.item;
  }
  }

  void display()
  {
   int i;
   if(top==0)
   {
   cout<<"\n Stack is empty: ";
   }
   else
   {
    for(i=top;i>0;i--)
    {
    cout<<"\n s.stack[i]";
    }
   }
  }

  void main()
  {
   char ch,c;

   do
   {
   u:
   clrscr();
   cout<<"\n\n1: push";
   cout<<"\n2: pop";
   cout<<"\n3: display";
   cout<<"\n4: exit";
   cout<<"\nEnter your choice: ";
   cin>>c;
   switch(ch)
   {
    case 1:
	 up:
	 push();
	 break;
    case 2:
	 pop();
	 break;
    case 3:
	 display();
	 break;
    case 4:
	 exit(0);
	 break;
    default:
	 cout<<"\nwrong choice";
   }
   cout<<"\n\n Pushed an element (Y/N)";
   c=getch();
   if(c=='y'||c=='Y')
   {
   goto up;
   }
   else
   {
   goto u;
   }
   }
   while(ch!=5);
  }
stack example in c++
Stack Pop Operation Queue in C++

Google Advertisment

Buy This Ad Space @$20 per Month, Ad Size 600X200 Contact on: hitesh.xc@gmail.com or 9999595223

Magenet is best Adsense Alternative here we earn $2 for single link, Here we get links ads. Magenet

For Projects 9999595223

Google Advertisements


Buy Websites 9999595223

Buy College Projects with Documentation Contact on whatsapp 9999595223. Contact on: hitesh.xc@gmail.com or 9999595223 Try this Keyword C++ Programs

Advertisements