Friday, October 11, 2013

I need doubly linked list implementation in c++ using templates and linked list. please provide maximum functions?

Here is your running code...

#ifndef DOUBLY_H
#define DOUBLY_H
template <class T>
class doubly
{
private:
       class ListNode
       {
       public:
              T value;
              ListNode *next;
               ListNode *prev;
               ListNode()
               {
                     next=NULL;
                     prev=NULL;
               }
       };
       ListNode *head; // List head pointer
public:
       doubly(void)// Constructor
       {
              head = NULL;
       }
       void insertfirst(T);
       void insertlast(T);
       void Removefirst();
       void Removelast();
       void insertAfter(T,T);
       void insertBefore(T,T);
       void insertNode(T);
       void deleteNode(T);
       void displayList();
};
template <class T>
void doubly<T>::insertfirst(T num)
{
       ListNode *newnode ,*tptr;
       newnode= new ListNode;
       newnode->value=num;
       if(head==NULL)
       {
              head=newnode;
       }
       else
       {
              tptr=head;
              newnode->next=head;
              tptr->prev=newnode->next;
              head=newnode;
       }
}
template <class T>
void doubly<T>::insertlast(T num)
{
       ListNode *newnode,*tptr;
       newnode= new ListNode;
       newnode->value=num;
       if(head==NULL)
       {
              head=newnode;
       }
       else
       {
              tptr=head;
              while(tptr->next)
              {
                     tptr=tptr->next;
              }
              tptr->next=newnode;
              newnode->prev=tptr->next;
       }
}
template <class T>
void doubly<T>::Removefirst()
{
       ListNode *tptr,*tptr2;
       if(!head)
       {
              cout<<"Empty"<<endl;
       }
       else
       {
              tptr=head;
              tptr2=head;
              tptr2=tptr2->next;
              delete tptr;
              head=tptr2;
       }
}
template <class T>
void doubly<T>::Removelast()
{
       ListNode *tptr,*tptr2;
       if(!head)
       {
              cout<<"Empty"<<endl;
       }
       else
       {
              tptr=head;
              tptr2=head;
              while(tptr->next!=NULL)
              {
                     tptr2=tptr;
                     tptr=tptr->next;
              }
              delete tptr;
              tptr2->next=NULL;
       }
}
template <class T>
void doubly<T>::insertAfter(T key,T num)
{
       ListNode *newnode,*temp;
       T count=1;
       newnode=new ListNode;
       newnode->value=num;
       if(!head)
              cout<<"Empty "<<endl;
       else
       {
              temp=head;
              while(key!=count)
              {
                     temp=temp->next;
                     count++;
              }
              temp->next->prev=newnode;
              newnode->next=temp->next;
              newnode->prev=temp;
              temp->next=newnode;
       }     
}
template <class T>
void doubly<T>::insertBefore(T key,T num)
{
       ListNode *newnode,*temp,*temp2;
       T count=1;
       newnode=new ListNode;
       newnode->value=num;
       if(!head)
              cout<<"Empty "<<endl;
       else
       {
              temp=head;
              while(key!=count)
              {
                     temp2=temp;
                     temp=temp->next;
                     count++;
              }
              temp->prev=newnode;
              newnode->next=temp;
              temp2->next=newnode;
              newnode->prev=temp2;
       }

}
      
template <class T>
void doubly<T>::displayList()
{
       ListNode *nodeptr;
       if (head==NULL)
       {
              cout<<" Your list is empty "<<endl;
       }
       else
       {
              nodeptr=head;
              while (nodeptr!=NULL)
              {
                     cout<<nodeptr->value<<endl;
                     nodeptr=nodeptr->next;
              }
       }
}
template <class T>
void doubly<T>::deleteNode(T num)
{
       ListNode *temp,*temp2;
       if (head->value== num)
       {
              head=head->next;
       }
       else
       {
              temp=head;
              while(temp && temp->value!=num)
              {
                     temp2=temp;
                     temp=temp->next;
              }
              temp->next->prev=temp2->next;
              temp2->next=temp->next;
              delete temp;
       }
}
#endif

#include <iostream.h>
#include "doubly.h"
void main(void)
{
doubly<int> list;
cout<<"\t\tWelcome in doubly link list "<<endl;
cout<<"\nLets call insert function for inserting 2,4,6"<<endl;
list.insertfirst(2);
list.insertfirst(4);
list.insertfirst(6);
list.displayList();
cout<<"\nLets call insert function for inserting 8,10,12 in last"<<endl;
list.insertlast(8);
list.insertlast(10);
list.insertlast(12);
list.displayList();
cout<<"\nLets display forward list "<<endl;
list.displayList();
cout<<"\nDelete First \n";
list.Removefirst();
list.displayList();
cout<<"\nDelete last "<<endl;
list.Removelast();
list.displayList();
cout<<"\nDelete node number 2\n";
list.deleteNode(2);
list.displayList();
cout<<"\nInsert 14 after node 2\n";
list.insertAfter(2,14);
list.displayList();
cout<<"\nInsert 11 before node 3 "<<endl;
list.insertBefore(3,11);
list.displayList(); 

}

1 comment:

  1. Your mode of descrіbing all in this article is genuinely good, all can easily be aware of it, Thanks a lot.


    Feel free to surf to my webpage ... temp

    ReplyDelete