Stack Implemented Through LinkList

// Stacklink.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
using namespace std;

class node
{
          friend class linklist;
private:
          int val;
          node *next;
public:
          void setvalue(int x)
          {
                   val=x;

          }
          int getvalue()
          {
                   return val;
          }
          void setnext(node *a)
          {
                   next=a;
          }
          node * getnext()
          {
                   return next;

          }
};
class stack
{
private:
         
          node* head;
          node* top;
          int size;
public:
          stack()
          {
                   head=NULL;
                   size=0;
                   top=NULL;

          }
          void push(int temp)
         
          {
                  
          node *n=new node();
          n->setvalue(temp);
          if(size==0)
          {
                   n->setnext(NULL);
                   head=n;
          }

         
         
          else if(size>0)
          {
                   top->setnext(n);
                   n->setnext(NULL);
          }
         

                   top=n;
                   size++;
                  
          }
          node *getnode(int loc)
          {
                   node *x;
                   x=head;
                   for(int i=1;i<loc;i++)
                   {
                             x=x->getnext();
                   }
                   return x;
                   }
          int pop()
          {       
                   if(size==0)
                   {
                             cout<<"No elements to pop"<<endl;
                             return 0;
                   }
                   else
                   {
                   node* p;
                   node*n;
                   int x;
                   p=top;
                   x=p->getvalue();
                   delete p;
                   n=getnode(size-1);
                   n->setnext(NULL);
                   top=n;
                   size--;
                   cout<<"Element popoed with value "<<x<<endl;
                   return x;
          }
          }
         
          int asize()
          {
                   return size;
          }
          void display()
          {
                   node *p=top;
                   int loc=size;
                   while(loc!=0)
                   {
                   cout<<p->getvalue()<<endl;
                   loc--;
                   p=getnode(loc);
                  
                   }
          }
                  
         
};

int _tmain(int argc, _TCHAR* argv[])
{
          stack st1;
          char ch;
          do
          {
                   cout<<"                                     MENU "<<endl;
                   cout<<"1.Push Element"<<endl;
                  
                   cout<<"2.Display "<<endl;
         
                   cout<<"3.Pop Element"<<endl;
                  
                   cout<<"4.exit"<<endl;
                   cout<<"Enter your choice ";
                   cin>>ch;
                   switch(ch)
                   {
                   case '1':
                             st1.push(10);
                             st1.push(20);
                             st1.push(30);
                            
                             break;
                   case '2':
                             st1.display();
                             break;
                   case '3':
                            
                             st1.pop();
                            
                             break;
                   case '4':
                             break;
                   default:
                             break;
                   }
          }
                   while(ch<'4');

         
         
         
         
         


          return 0;
}


Comments

Popular posts from this blog

Using Progress Bar In C#

Get elements by class name in javascript

Jquery serer side datatables in asp.net