Sunday 6 October 2013

C++ code to implement stack using linked list

/*top was compared to null instead of initializing to null*/
#include<iostream>
using namespace std;

template<class T>
class node
{
      public:
              T data;
              node *link;
};
template<class T>
class stack
{
       node<T> *top;
      public:
             stack();
             ~stack();
             int isempty();
             void push(T x);
             T pop();
             T topmost();
             void display();
};
template<class T>
stack<T>::stack()
{
                 top=NULL;/*initially was top==null*/
}
template<class T>
int stack<T>::isempty()
{
    if(top==NULL)
    return 1;
    else
    return 0;
}
template<class T>
void stack<T>::push(T x)
{
     node<T> *temp;
     temp=new node<T>;
     temp->data=x;
     temp->link=top;
     top=temp;
}
template<class T>
T stack<T>::pop()
{
                 node<T> *temp;
                 T x;
                 if(isempty())
                 return -1;
                 else
                 {
                     temp=top;
                     top=top->link;
                     x=temp->data;
                     delete temp;
                     return x;
                 }
}
template<class T>
void stack<T>::display()
{
     node<T> *temp;
     if(isempty())
     {
                  cout<<"\n stack is empty ";
     }
     else
     {
         cout<<"\nthe contents are :";
         temp=top;
         while(temp!=NULL)
         {
                          cout<<temp->data;
                          temp=temp->link;
         }
     }
}
template<class T>
T stack<T>::topmost()
{
                     if(isempty())
                     return -1;
                     else
                     return(top->data);
}
template<class T>
stack<T>::~stack()
{
                  node<T> *temp=NULL;
                  while(top!=NULL)
                  {
                                  temp=top;
                                  top=top->link;
                                  delete temp;
                  }
}
int main()
{
    int ch,x;

    stack<int> a;
    do
    {
              cout<<"\n 1.insertion \n 2.deletion \n 3.display \n 4.topmost element \n 5.exit \n";
              cout<<"\n enter ur choice :";
              cin>>ch;
              switch(ch)
              {
                        case 1:
                             cout<<"enter data element :\n";
                             cin>>x;
                             a.push(x);
                             break;
                        case 2:
                             x=a.pop();
                             if(x==-1)
                             cout<<"\n stack is empty....";
                             else
                             cout<<"\n the deleted value is :"<<x<<"\n";
                             break;
                        case 3:
                             a.display();
                             break;
                        case 4:
                             x=a.topmost();
                             if(x==-1)
                             cout<<"\n stack is empty..";
                             else
                             cout<<"\n the topmost value is :"<<x<<"\n";
                             break;
                        case 5:
                             break;
              }
    }while(ch<5);
    return 0;

}

No comments:

Post a Comment