In: Computer Science
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.
#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!