Question

In C++ Create a data structure doubly linked list, implement the following operations for the doubly...

In C++

Create a data structure doubly linked list, implement the following operations for the doubly linked list:

addFirst

addLast

insertBefore

insertAfter

delete

printList

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <iomanip>


using namespace std;

//Node Class
class Node{
public:
    double v;
    Node *next;
    Node *prev;
};

//LinkedList class
class LList{
    //Head pointer
    Node *head=nullptr;
    Node *tail = nullptr;
public:
    //This adds a new node at the end of linked list
    void addFirst(double val){
        Node *nnode = new Node();
        nnode->v = val;
        if(head==nullptr){
            head = nnode;
            tail = nnode;
            return;
        }

        nnode->next = head;
        head->prev = nnode;
        head = nnode;
    }


    //This adds a new node at the end of linked list
    void addLast(double val){
        Node *nnode = new Node();
        nnode->v = val;
        if(head==nullptr){
            head = nnode;
            tail = nnode;
            return;
        }
        tail->next = nnode;
        nnode->prev = tail;
        nnode->next = nullptr;
        tail = nnode;
    }


    void insertBefore(double target, double val){
        Node *nnode = new Node();
        nnode->v = val;
        if(head->v==target){
            nnode->next = head;
            head->prev = nnode;
            head = nnode;
            return;
        }
        Node *temp = head;
        while(temp!=nullptr){
            if(temp->v==target){
                Node *prev = temp->prev;
                prev->next = nnode;
                nnode->prev = prev;
                nnode->next = temp;
                temp->prev = nnode;
                break;
            }
            temp = temp->next;
        }
    }

    void insertAfter(double target, double val){
        Node *nnode = new Node();
        nnode->v = val;
        if(target==tail->v){
            tail->next = nnode;
            nnode->prev = tail;
            nnode->next = nullptr;
            tail = nnode;
            return;
        }
        Node *temp = head;
        while(temp!=nullptr){
            if(temp->v==target){
                Node *nxt = temp->next;
                temp->next = nnode;
                nnode->prev = temp;
                nnode->next = nxt;
                nxt->prev = nnode;
                break;
            }
            temp = temp->next;
        }
    }

    //This deletes head node
    void deleteNode(double val){
        if(head->v==val){
            Node *temp = head;
            head = head->next;
            head->prev = nullptr;
            free(temp);
            return;
        }
        if(tail->v==val){
            Node *temp = tail;
            tail = tail->prev;
            tail->next = nullptr;
            free(temp);
            return;
        }
        Node *temp = head;
        while(temp!=nullptr){
            if(temp->v==val){
                Node *prev = temp->prev;
                Node *nxt = temp->next;
                prev->next = nxt;
                nxt->prev = prev;
                free(temp);
                break;
            }
            temp = temp->next;
        }
    }

    //This prints the linked list
    void print(){
        Node *temp = head;
        while(temp!=nullptr){
            cout<<temp->v<<"->";
            temp = temp->next;
        }
        cout<<"NULL"<<endl;
    }
};

int main(){
    LList a;
    a.addFirst(3);
    a.addLast(4);
    a.addFirst(2);
    a.addLast(5);
    a.print();
    a.insertAfter(4,6);
    a.insertBefore(3,7);
    a.insertBefore(2,1);
    a.print();
    a.deleteNode(4);
    a.deleteNode(1);
    a.print();
    return 0;
}


OUTPUT :

Add a comment
Know the answer?
Add Answer to:
In C++ Create a data structure doubly linked list, implement the following operations for the doubly...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT