Question

In: Computer Science

c++ example of a double linked list of chars I need to create a double linked...

c++ example of a double linked list of chars

I need to create a double linked list of chars. this should be a class that allows you to input a single character at a time, list the resulting characters, find any character and delete the first example of a character.

Solutions

Expert Solution

#include<iostream>

using namespace std;

struct Node{

        char c;
        Node* next;
        Node* prev;
};

class DoubleLinkedList{

        private:
                Node* head = NULL;
                Node* tail = NULL;
        
        public:
                DoubleLinkedList(){
                        head = NULL;
                        tail = NULL;
                }
                ~DoubleLinkedList(){
                        Node* temp = head; 
                        while(temp){
                                head = head->next;
                                delete temp;
                                temp = head;
                        }
                }
                bool insertNode(char c){
                        try{
                                Node* node = new Node;
                                node->c = c;

                                if(!head){
                                        head = node;
                                        tail = node;
                                }
                                else{
                                        tail->next = node;
                                        node->prev = tail;
                                        tail = node;
                                }
                        }
                        catch(...){
                                return false;
                        }
                        return true;
                }
                bool searchNode(char c){
                        if(head){
                                Node* temp = head; 
                                while(temp){
                                        if(temp->c == c)
                                                return true;
                                        temp = temp->next;
                                }
                        }
                        return false;
                }
                void printList(){
                        Node* temp = head; 
                        while(temp){
                                cout<<temp->c<<" <-> ";
                                temp = temp->next;
                        }
                        cout<<"NULL\n";
                }
                bool deleteNode(char c){
                        try{
                                if(head){

                                        Node* temp = head;
                                        if(head->c == c){
                                                head = head->next;
                                                if(head)
                                                        head->prev = NULL;
                                                delete temp;
                                        }
                                        else{
                                                while(temp){
                                                        if(temp->c == c){
                                                                temp->prev->next = temp->next;
                                                                if(temp->next)
                                                                        temp->next->prev = temp->prev;
                                                                if(tail == temp)
                                                                        tail = temp->prev;
                                                                delete temp;
                                                                break;
                                                        }
                                                        temp = temp->next;                   
                                                }
                                        }
                                }
                                else{
                                        return false;
                                }
                        }
                        catch(...){
                                return false;
                        }
                        return true;
                }
};

int main(){

        DoubleLinkedList *dll = new DoubleLinkedList();

        char choice, c;

        while(true){
                cout<<"Enter your choice\n";
                cout<<"[1] Insert Node\n";
                cout<<"[2] Search List\n";
                cout<<"[3] Print List\n";
                cout<<"[4] Delete Node\n";
                cout<<"[5] Exit\n";

                cin>>choice;

                if(choice == '1'){
                        cout<<"Enter the character to add to the list:\n";
                        cin>>c;
                        
                        if(dll->insertNode(c))
                                cout<<"Insertion Successfull\n";
                        else
                                cout<<"Insertion failed\n";
                }
                else if(choice == '2'){
                        cout<<"Enter the character to search in the list:\n";
                        cin>>c;

                        if(dll->searchNode(c))
                                cout<<"Character is present\n";
                        else
                                cout<<"Character is not present\n";

                }
                else if(choice == '3'){
                        dll->printList();
                }
                else if(choice == '4'){
                        cout<<"Enter the character to delete from list:\n";
                        cin>>c;

                        if(dll->deleteNode(c))
                                cout<<"Deletion Successfull\n";
                        else
                                cout<<"Deletion failed\n";
                }
                else if(choice == '5'){
                        delete dll;
                        return 0;
                }
                else{
                        cout<<"Invalid input. Please try again\n";
                }
        }

        return 0;
}

Code Screenshot:

Output Screenshot:

I have tested the code throughly. If you find any bug or erro let me know I will fix it. If you have any doubts or query let me know. Happy to help!


Related Solutions

I want c++ /c program to build a double linked list and find with a function...
I want c++ /c program to build a double linked list and find with a function the middle element and its position and the linked list must be inserted by the user aand note You should handle the both cases of whether the size of the list even or odd
I need an example of how to swap and index within a doubly linked list with...
I need an example of how to swap and index within a doubly linked list with index + 1. This is written in java. public class A3DoubleLL<E> {    /*    * Grading:    * Swapped nodes without modifying values - 2pt    * Works for all special cases - 1pt    */    public void swap(int index) {        //swap the nodes at index and index+1        //change the next/prev connections, do not modify the values   ...
Hi I would like to see an example in c++ of Stack As Linked List I...
Hi I would like to see an example in c++ of Stack As Linked List I need a seek() function which receives a double number X and which returns in which position in the stack is X. If the value X is not in the stack, the function should return -1
I need to create a linked list that contains a fixed arraylist. Each new entry is...
I need to create a linked list that contains a fixed arraylist. Each new entry is added to array list. If the arraylist is full, create a new arraylist and add to the linklist. In java please.
Exercise 2: Write a program in Java to manipulate a Double Linked List: 1. Create Double...
Exercise 2: Write a program in Java to manipulate a Double Linked List: 1. Create Double Linked List 2. Display the list 3. Count the number of nodes 4. Insert a new node at the beginning of a Double Linked List. 5. Insert a new node at the end of a DoubleLinked List 6. Insert a new node after the value 5 of Double Linked List 7. Delete the node with value 6. 8. Search an existing element in a...
Given a doubly linked list in c++, how do I create a function that returns the...
Given a doubly linked list in c++, how do I create a function that returns the pointer to first node in the given pattern, For example, given mainList (a -> b -> c -> d) and sublist  (b -> c), our function should return a Node pointer that points to first node of the sublist in the mainList. If the pattern doesn't exist in the mainList, we should return a nullptr, there are multiple of the same sublist in the mainList,...
Given a doubly linked list in c++, how do I create a function that returns the...
Given a doubly linked list in c++, how do I create a function that returns the pointer to first node in the given pattern, For example, given mainList (a -> b -> c -> d) and sublist  (b -> c), our function should return a Node pointer that points to first node of the sublist in the mainList. If the pattern doesn't exist in the mainList, we should return a nullptr, there are multiple of the same sublist in the mainList,...
can u give me an example of how to removing Double Linked List a node at...
can u give me an example of how to removing Double Linked List a node at a given location in java the method should be like this: public void removeFromLocation(int location) { }                                }
Write a program where you- 1. Create a class to implement "Double Linked List" of integers....
Write a program where you- 1. Create a class to implement "Double Linked List" of integers. (10) 2. Create the list and print the list in forward and reverse directions. (10)
Can you make this singular linked list to doubly linked list Create a Doubly Linked List....
Can you make this singular linked list to doubly linked list Create a Doubly Linked List. Use this to create a Sorted Linked List, Use this to create a prioritized list by use. Bring to front those links recently queried. -----link.h------ #ifndef LINK_H #define LINK_H struct Link{ int data; Link *lnkNxt; }; #endif /* LINK_H */ ----main.cpp---- //System Level Libraries #include <iostream> //I/O Library using namespace std; //Libraries compiled under std #include"Link.h" //Global Constants - Science/Math Related //Conversions, Higher Dimensions...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT