FoodForFutureGeeks

Tuesday 8 January 2013

Doubly Linked List Implementation

Following program gives Implementation of Doubly Linked List with its various operations:


/*************************************************************
* Author: Aniruddha S N
* 
*/

#include<iostream>
using namespace std;

// structure node
struct node
{
    int info;
    struct node* next;
    struct node* prev;
};

//function declarations
 void display_list();
 void Insert_Element_List(int ele);
 void DeleteElementAtPos(int pos);
 void display_Prev_Next(int pos);
 void delete_Entire_list();

 //global variable first
 node* first = NULL;

 //main function
int main()
{    
    int ele = 0;
    int ch  = 0;
    int pos = 0;

//Menu to display Doubly linked list operations
    while(1)
    {
        cout<<"**************DoublyLinkedListOperationsMenu*************\n";
        cout<<"1---->Insert an element in list\n";
        cout<<"2---->displayList\n";
        cout<<"3---->DeleteAnElement at specified position in list\n";
        cout<<"4---->Display Previous and Next ele of the mentioned node\n";
        cout<<"5---->exit\n";
        cout<<"***************************************************\n";
        cout<<"\n\n";
        cout<<"Enter your choice:\n";
        cin>>ch;

        //using switch case call respective functions for respective choices
        switch(ch)
        {
        case 1: cout<<"Enter the element to be inserted into list\n";
                cin>>ele;
                Insert_Element_List(ele);
                break;
        case 2:display_list();
               break;
        case 3:cout<<"Enter the position to delete elements\n";
               cin>>pos;
               DeleteElementAtPos(pos);
               break;
        case 4:cout<<"Enter the Node Position to display previous and next ele\n";
               cin>>pos;
               display_Prev_Next(pos);
               break;
        default: delete_Entire_list();
                 exit(0);
        }

    }
     return 0;
}//end of main

//function to insert an element in singly linked list
void  Insert_Element_List(int ele)
{
    node* temp;
     if(first == NULL)
     {
         first= new node();
         first->info=ele;
         first->next=NULL;
         first->prev=NULL;
     }
     else
     {
         node* temp1;
         temp1 = new node();
         temp=first;

     //find the end of list
         while( temp->next != NULL)
             temp=temp->next;

         temp1->info=ele;
         temp1->next=NULL;
     //attach the new node at the end of list
         temp->next=temp1;
         temp1->prev=temp;
     }
}


//function to display the elements in the list
void  display_list()
{
    if ( first == NULL)
        cout<<"list doesnt have any elements";

    else if(first->next == NULL)
    {
        cout<<"List Contents:\n";
        cout<<first->info<<"\n";
    }

    else
    {
        node* temp;
        temp=first;
        cout<<"List Contents:\n";
        while( temp != NULL)
        {
            cout<<temp->info<<"\n";
            temp=temp->next;
        }
    }
}

//function to delete element at specified position
void DeleteElementAtPos(int pos)
{
    int flag = 0;
    node* prev;
    node* temp;


    if( first == NULL )
    { cout<<"List Empty\n";
    }

    else if( pos >1 && first->next == NULL )
    {
        cout<<"No element at specified position in list\n";
    }
            
    else if( pos ==1)
      {
            temp=first;
            prev=temp;
            temp=temp->next;
             temp->prev=NULL;
            //re initiate first
            first=temp;
            prev->next=NULL;
            prev->prev=NULL;
            delete prev;
            prev= NULL;
            
        }

    // for all other positions
    else
    {

        temp=first;
        flag=1;
        while(flag < pos && temp->next != NULL)
        {
           prev=temp;
           temp=temp->next;
           flag++;
        }
        //check whether the specified position exists
        if( flag < pos)
        {
            cout<<"No element at specified position in list\n";
        }
        //delete the node at the specified position
        else
        {
            prev->next = temp->next;
            temp->next->prev=prev;

            temp->next=NULL;
            temp->prev=NULL;
            delete temp;
            temp=NULL;
        }
    
    }//end of else for all other positions
}

void display_Prev_Next(int pos)
{
    if(first == NULL)
        cout<<"List empty\n";
    else if(first->next == NULL)
        cout<<"Only one element in List\n";
    //for other valid cases
    else
    {
        node* temp=first;
        node* prev;
        int flag=1;
        while( flag < pos && temp->next!=NULL)
        {
            prev=temp;
            flag++;
            temp=temp->next;
        }
        //check whether its an invalid node position
        if(flag < pos)
        {
            cout<<"No element at specified position in the list\n";
        }
        //if the position is valid
        else
        {
            cout<<"Previous Node:"<<prev->info<<"\n";
            cout<<"Current  Node:"<<temp->info<<"\n";
            if(temp->next == NULL)
                cout<<"Next Node:End of List\n";
            else
            cout<<"Next Node:"<<temp->next->info<<"\n";
        }
    }
    //end of else other valid cases
}

//function to delete entire list and prevent memory leakage when exit is called
void delete_Entire_list()
{
    node* temp;
    node* curr;
    temp=first;
    if(first == NULL)
        return;
    while(temp->next != NULL)
    {
      curr=temp;
      temp=temp->next;
      delete curr;
    }
    delete temp;
}

No comments:

Post a Comment