Friday, October 11, 2013

I need stack implementation via a linked list a complete running code.can someone help me please?

This is a complete running code in c++.
Enjoy

#ifndef LINKEDLIST_H
#define LINKEDLIST_H

template <class T>
class LinkedList
{

private:
        // Declare a structure for the list
        struct ListNode
        {
                T value;
                    
               struct ListNode *next;
        };
ListNode *head; // List head pointer

public:
int top;
LinkedList(void)// Constructor
       {
                     top =-1;
                     head = NULL;

                    
       }     
              void push ();
              void pop ();
              bool empty()

              {
                     return head==NULL;

              }
              bool full ()
              {
                     return head!=NULL;
              }

       void displayList();
 };

template <class T >
void LinkedList <T>::push()
{
int num;

       ListNode *newnode ,*findptr;
if ( top != 3)
{
       newnode= new ListNode;
       cout<<" Enter a value of num ";
       cin>> num;
       newnode->value=num;
       newnode->next=NULL;
       if  ( head==NULL)
       {
             
              head=newnode;
      
              top++;
       }
       else
       {
              findptr=head;
             
              while ( findptr->next!=NULL)
              {
                     findptr=findptr->next;
              }
              findptr->next=newnode;
              newnode->next=NULL;
              top++;
       }
}
       else
       {
              cout<<" stack is full "<<endl;
       }
}


template <class T>

void LinkedList <T> ::pop()
{
       int num;


       if ( top!=-1)
       {
             
              cout<<" Enter a value for POP "<<endl;
              cin>> num;
       ListNode *movePtr,*pptr,*fptr;

       if (head== NULL )
             
       {
              cout<<" Your list is empty "<<endl;
       }

       else
       {
              if ( head->value== num)
              {
                     movePtr=head->next;
                     delete head;
                     head=movePtr;
              }
              else
              {
                     movePtr=head;
             

                     while (movePtr->value!= num && movePtr!=NULL )

                     {
                    
                           pptr=movePtr;
                           fptr=pptr;
                           movePtr=movePtr->next;
                           fptr=fptr->next->next;
                     }

                           movePtr->next=NULL;
                           movePtr->value=0;
                           delete movePtr;
                           pptr->next=fptr;
                          
              }
       }
       }
       else
       {
              cout<<" Stack is empty "<<endl;
       }
             
}
template <class T>
void LinkedList <T>::displayList()
{

              ListNode *moveptr;
      
       if ( head== NULL )
       {
              cout<<"  Your List is Empty "<<endl;
       }
       else
       {
              moveptr=head;

              while ( moveptr!=NULL)
              {
                     cout<<moveptr->value<<endl;
                     moveptr=moveptr->next;
              }

       }
}
#endif


#include <iostream>
using namespace std;
#include "code.h"
void main ()
{
       LinkedList<int> list;
       cout<<" 1 for push "<<endl;
       cout<<" 2 for pop "<<endl;
       cout<<" 3 for display "<<endl;
int x;

do {
       cout<<" ENTER A CHOICE  ";
       cin>> x;
       switch ( x)
      
       {
       case 1:

       list.push();
       break;
       case 2:
              list.pop( );
       break;
       case 3:
                     list.displayList();
       break;
       case 4 :
              break;

       }

}      while ( x !=4);


}

1 comment:

  1. I could not resist commenting. Perfectly written!

    My website; Minecraft Download

    ReplyDelete