In: Computer Science
C++
What Should This Program Do?
Linked List Class
DRIVER – driver.cpp
Write a driver program (driver.cpp) that will do the following:
------------------------------------------------------------------------------
Sample Output
The linked list has been created.
I am appending several strings to the list.
boogeyman
ghost
scarecrow
witch
zombie
I am inserting vampire in the list.
boogeyman
ghost
scarecrow
vampire
witch
zombie
I am deleting ghost from the list.
boogeyman
scarecrow
vampire
witch
zombie
All list nodes have been removed.
---------------------------------------------------------------------------------------------------------------------
What to Turn In
Okay I have coded it for you,
Heres the List.h
#ifndef LINKED_LIST
#define LINKED_LIST
#include<bits/stdc++.h>
using namespace std;
struct ListNode {
        string data;
        ListNode* next;
};
void append(ListNode *head,string s);
void insert(ListNode *head,string s);
void deleteNode(ListNode *head, string s);
void display(ListNode* head);
#endif
And heres the Main.cpp
#include <iostream>
#include "List.h"
using namespace std;
//void List()
void append(ListNode *head,string s){
    
    ListNode *newNode=new ListNode;
    newNode->data=s;
    newNode->next=NULL;
    if(head==NULL){
        head=newNode;
        return;
    }
    ListNode *curr=head;
    while(curr){
        if(curr->next==NULL){
            curr->next=newNode;
            return;
        }
        curr=curr->next;
    }
}
void insert(ListNode *head,string s){
    cout<<"I am inserting "<<s<<" to the list"<<endl;
    ListNode *newNode=new ListNode;
    newNode->data=s;
    newNode->next=NULL;
    ListNode *curr=head;
    ListNode *prev=new ListNode;
    prev->data="";
    prev->next=NULL;
    while(!(prev->data<=s&&s<=curr->data)){
        prev=curr;
        curr=curr->next;
    }
    if(curr != NULL){
        newNode->next = curr;
    }
    if(prev != NULL){
        prev->next = newNode;
    }
    else{
        head = newNode;
    }
}
void deleteNode(ListNode *head, string s){
    cout<<"I am deleting "<<s<<" from the list"<<endl;
    ListNode *curr=head;
    ListNode *prev=NULL;
    while(curr){
        if(curr->data==s) break;
        prev=curr;
        curr=curr->next;
    }
    if(curr==head){
        head=curr->next;
        delete curr;
        return;
    }
    if(curr->next==NULL){
        prev->next=NULL;
        delete curr;
        return;
    }
    
    prev->next=curr->next;
    delete curr;
    return;
}
void display(ListNode* head){
    ListNode *curr=head;
    while(curr){
        cout<<curr->data<<" ";
        curr=curr->next;
    }
    cout<<endl;
}
int main(){
    ListNode *head=new ListNode();
    cout<<"The linked list has been created"<<endl;
    cout<<"I am appending several strings to the list"<<endl;
    append(head,"boogeyman");
    append(head,"ghost");
    append(head,"scarecrow");
    append(head,"witch");
    append(head,"zombie");
    display(head);
    insert(head,"vampire");
    display(head);
    deleteNode(head,"ghost");
    display(head);
    return 0;
}
Please reach out in the comments if you have any doubt