Create a C++ program to create a double linked list.

QUESTION

Create a C++ program to create a doubly-linked list, insert nodes into the list, display list data, length of nodes, search a value and modify it if exists, delete any node from the double linked list.

 

SUMMARY

A doubly linked list is a list that can be traversed in either way, i,e  from left to right or from right to left. Functions are defined in the program to perform certain operations on this doubly linked list.

 

EXPLANATION

The class node has a data variable that stores the data and two self-referential pointers to point to the next and previous nodes respectively. A class named doubly Linked List is implemented which has the head node (points to the first node of the list) as its member.

The method insert is used to insert a node into the list. If the list happens to be empty, the new node will be made as the head node. If not, the node is inserted at the end of the list.

The method delete() takes in integral data and deletes the node if it is present in the list.

The method length() returns the size of the list.

The method modify()  takes in two arguments, one: the data which is to be modified and two: the data which replaces the old data.

 

CODE

#include <iostream>
using namespace std;
class node 
{
    public:
    int data;
    node *next;
    node *prev;
    node()
    {
        data=0;
        next=NULL;
        prev=NULL;
    }
};
 
/*implementing doubly linked list */
class doublylinkedList 
{
    public:
    node *head;
    doublylinkedList()
    {
        head=NULL;
    }
    /* for insert into the list */
    void insert(int ele)
    {
        node *newnode=new node;
        newnode->data=ele;
        newnode->next=NULL;
        newnode->prev=NULL;
        if(head==NULL)
        {
            head=newnode;
        }
        else 
        {
            node *front=head;
            while(front->next!=NULL)
            {
                front=front->next;
            }
            front->next=newnode;
            newnode->prev=front;
        }
    }
    /* for delete into the list */
    void del(int ele)
    {
        node *front=head->next;
        node *prev=head;
        if(head->data==ele)
        head=head->next;
        else
        {
            while(front!=NULL)
            {
                if(front->data==ele)
                {
                    prev->next=front->next;
                    return;
                }
                prev->next=front;
                front=front->next;
            }
        }
    }
    /* finding the length of the list */
    int length()
    {
        int c=0;
        node *front=head;
        while(front!=NULL)
        {
            front=front->next;
            c++;
        }
        return c;
    }
    /* to modify a node */
    void modify(int ele,int newele)
    {
        node *front=head;
        while(front!=NULL)
        {
            if(front->data==ele)
            {
                front->data=newele;
                return;
            }
            front=front->next;
        }
        cout<<"The node-"<<ele<<" does not exist"<<endl;
    }
    void display()
    {
        cout<<"Displaying the list: "<<endl;
        node *front=head;
        while(front!=NULL)
        {
            cout<<front->data<<" ";
            front=front->next;
        }
        cout<<endl<<endl;
    }
};
int main()
{
    doublylinkedList ele;
    ele.insert(20);ele.insert(40);ele.insert(60);ele.insert(80);
    ele.insert(100);
    ele.display();
    cout<<"Length of the list: "<<ele.length()<<endl<<endl;
    ele.del(20);
    ele.display();
    ele.modify(40,54);
    ele.display();
}

 

OUTPUT

 

 

Also Read: Implement a class names Circular Linked List. The data structure backing up this class is a double-linked list that loops, the data stored in each node will be an integer

Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *