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.

/*Q-8b-> WAP in C++ to implement Stack with Operations PUSH, POP, and PEEP
using Linkedlist */

#include<iostream.h>
#include<conio.h>
#include<process.h>
class stack
{
  private:
	 struct Node
	 { int info;                   //Declaration of Structure Node
	   Node *next;
	 }*top,*temp,*ptr,*x;
  public:
	 stack()                         //Constructor
	 {start=top=temp=ptr=NULL;}
	 void push(int);
	 void pop();                   //Member Functions
	 void peep();
};
void stack::push(int n)
{
  ptr=new Node;
  ptr->info=n;
  ptr->next=NULL;
     if(ptr==NULL)
      {
	cout<<"\nCannot Create New Node!!!";
	getch();
      }                          //Function to Push an Element in the Stack
  if(top==NULL)
     top=ptr;
  else
     {temp=top;
      top=ptr;
      ptr->next=temp;
     }
}
void stack::pop()
{
  if(top==NULL)
     cout<<"\nStack is Empty!!!";
  else
   { ptr=top;                       //Function to Pop from the Stack
     top=top->next;
     delete ptr;
   }
}
void stack::peep()
{ x=top;
  if(top==NULL)
   cout<<"\nStack is Empty!!!";       //Function to display the Stack
 while(x!=NULL)
  {
   cout<<x->info<<"\n";
   x=x->next;
  }
}
void main()                         //Main
{
 stack ob;
 char choice;
 int ch,data;
 do
 {clrscr();
  cout<<"\n\n\t\tS T A C K   O P E R A T I O N S";
  cout<<"\n\t\t_______________________________";
  cout<<"\n\n1.PUSH";
  cout<<"\n2.POP";                          //Menu
  cout<<"\n3.PEEP";
  cout<<"\n4.EXIT";
  cout<<"\n\nWhich Operation Do You Want To Perform:";
  cin>>ch;
      switch(ch)
	      {
		case 1: cout<<"\nEnter The Data You Want To Push:";
			cin>>data;
			ob.push(data);
			break;
		case 2: ob.pop();
			break;           //Calling of the Functions
		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');
}
			 //End of the program