September 22, 2012 / IST / Language, Software Development.

In computer science, a stack is an area of memory that holds all local variables and parameters used by any function, and remembers the order in which functions are called so that function returns occur correctly. Each time a function is called, its local variables and parameters are “pushed onto” the stack. When the function returns, these locals and parameters are “popped.” Because of this, the size of a program’s stack fluctuates constantly as the program is running, but it has some maximum size.

The stack was first proposed in 1946, in the computer design of Alan M. Turing (who used the terms “bury” and “unbury”) as a means of calling and returning from subroutines. In 1957, the Germans Klaus Samelson and Friedrich L. Bauer patented the idea. The same concept was developed, independently, by the Australian Charles Leonard Hamblin in the first half of 1957.

/* Q8a-> WAP in C++ To Implement Stack With Operation PUSH,POP and PEEP using
Array*/

#include<iostream.h>
#include<conio.h>
#include<process.h>
#define SIZE 10
static int top=-1;
class stack
{
  private:
	  int ar[SIZE];
  public:
	  void push(int item);
	  void pop();
	  void peep();
};
void stack::push(int item)
{
   if(top==SIZE-1)
      cout<<"\nThe Stack is Full!!!";
   else
     { ar[++top]=item;
       cout<<"\nElement succesfully pushed in the Stack!!!";
     }
}
void stack::pop()
{
  if(top<0)
    cout<<"\nStack Under flow!!!";
  else
   { top--;
     cout<<"\nElement sucessfully popped from the Stack!!!";
   }
}
void stack::peep()
{ if(top<0)
    cout<<"\nThe Stack is Empty it cannot be Peeped!!!";
  else
    for(int i=top;i>=0;i--)
      cout<<ar[i]<<" ";
}
void main()
{
  char choice;
  int ch,num;
  stack ob;
  do
  { clrscr();
  cout<<"\n\n\t\t\tS T A C K   O P E R A T I O N S";
  cout<<"\n\t\t\t-------------------------------";
  cout<<"\n\n1.PUSH";
  cout<<"\n2.POP";
  cout<<"\n3.PEEP";
  cout<<"\n4.EXIT";
  cout<<"\n\nEnter your choice:";
  cin>>ch;
   switch(ch)
   {
     case 1: cout<<"\nEnter the Element you want to Push:";
	     cin>>num;
	     ob.push(num);
	      break;
     case 2: ob.pop(); break;
     case 3: ob.peep(); break;
     case 4: exit(0);
     default: cout<<"\nPlease Enter a Valid Choice(1-4)!!!";
   }
  cout<<"\nDo you want to Continue(Y/N):";
  cin>>choice;
  }while(choice=='y' || choice=='Y');

 getch();
}