#include<iostream.h>
#include<conio.h>
#include<process.h>
const int max=5;
class stack
 {
  int s[max];
  int top;
  int temp;
  public:
  stack (int j,int p){top=j;temp=p;}
  void push(int item);
  void pop();
  void display();
 };
  void stack::push(int item)
   {
    if(top<4)
     {
      top++;
      s[top]=item;
     }
    else
     {
      cout<<"\nStack is full!";
     }
  }
  void stack::pop()
   {
    if(top>-1)
     {
      cout<<"\nThe popped item is "<<s[top];
      top--;
     }
    else
     {
      cout<<"\nStack is empty!";
     }
  }
 void stack::display()
  {
   temp=top;
   if(temp==-1)
    {
     cout<<"\nStack is empty!" ;
    }
    else
     {
      while(temp>=0)
       {
	cout<<"\n"<<s[temp];
	temp--;
       }
      }
  }
 void main()
  {
   clrscr();
   stack x(-1,-1);
   for(;;)
    {
     int ch,item;
     cout<<"\nEnter->\n1:push 2:pop 3:display 4:exit:\t";
     cin>>ch;
     switch(ch)
      {
       case 1: cout<<"\nEnter the items:\t";
	       cin>>item;
	       x.push(item);
	       break;
       case 2: x.pop();
	       break;
       case 3: x.display();
	       break;
       case 4: exit(0);
	       break;
     }
   }
 }

Using stack without dynamic mem allocation is like... ummm... using stack without dynamic mem allocation (LOL)

The answer is most assuredly forty-two.

The answer is most assuredly forty-two.

Well what else would it be, although i don't feel that this is important enought to be given an answer equivelant to the meaning of life!

but seriously, whats the question / problem?

Chris

What...a totally useless program.

1. Implement a stack class in 42 lines or less (including blank lines and braces).

GO!

Member Avatar for iamthwee

The answer is #include <stack>

What's the purpose of this thread??? To show an inferior implementation of stack?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.