Friday 2 September 2011

Stacks Program using Array


#include<iostream>
#include<cstdlib>
# define max 10
using namespace std;
template<class T>
class stacks
{
 T stack[max];
 int top;
 public:
 stacks()
   {
     top=-1;
   }
 bool push(T);
 bool pop();
 void display();
};
template<class T>
bool stacks<T>::push(T ele)
{
 if(top==max-1)
    return 0;
 else
   {
    top++;
    stack[top]=ele;
    return -1;
   }
 return 0;
}
template<class T>
bool stacks<T>::pop()
{
 if(top==-1)
    return 0;
 else
  {
    top--;
    return 1;
  }
 return 0;
}
template<class T>
void stacks<T>::display()
{
 for(int i=top;i>-1;i--)
   cout<<stack[i]<<endl;
}
int main()
{
 stacks<float>ob;
 int choice;
 float ele;
 bool t;
 while(1)
    {
      cout<<"\n1.Push \n2.Pop \n3.Display \n4.Exit";
      cout<<"\nEnter your choice  ";
      cin>>choice;
      switch(choice)
          {
                case 1:
                 {
                   cout<<"\nEnter element to be pushed\n";
                   cin>>ele;
                   t=ob.push(ele);
                   if(t==true)
                      cout<<"\nPush successful";
                   else
                      cout<<"\nPush unsuccessful";
                 }
                 break;
                case 2:
                 {
                  t=ob.pop();
                  if(t==true)
                    cout<<"\nPop successful";
                  else
                    cout<<"\nPop unsuccessful";
                 }
                 break;
                case 3:
                 ob.display();
                 break;
                case 4:
                 exit(1);
            }
      }
}
             

No comments:

Post a Comment