Question

In: Computer Science

C++ What Should This Program Do? Linked List Class Design your own linked list class (List.h)...

C++

What Should This Program Do?

Linked List Class

  • Design your own linked list class (List.h) to hold a series of strings.
  • The linked list node should be implemented as a struct.
  • The class should have member functions for appending, inserting, and deleting nodes.
  • You should also have a display function that will traverse the list & display each node’s value.
  • Don’t forget to add a destructor that destroys the list.

DRIVER – driver.cpp

Write a driver program (driver.cpp) that will do the following:

  1. Create a linked list object
  2. Call the linked list’s append function to append the following strings to your linked list. Afterwards, print to the screen to tell the user that you are inserting several strings to the list.
    1. “boogeyman”
    2. “ghost”
    3. “scarecrow”
    4. “witch”
    5. “zombie”
  3. Now call the linked list’s display function to print the list.
  4. Now call the linked list’s insert function to insert the “vampire” string in the correct sorted position. Print to the screen to tell the user that you are inserting “vampire” in to the list.
  5. Now call the linked list’s display function again to print the list.
  6. Now call the delete function to delete “ghost” from the list. Print to the screen to tell the user that you are deleting “ghost” from the list.
  7. Last, call the linked list’s display function again to print the list.

------------------------------------------------------------------------------

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

  • List.h
  • driver.cpp

Solutions

Expert Solution

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


Related Solutions

C++ question: Design and implement your own linked list class to hold a sorted list of...
C++ question: Design and implement your own linked list class to hold a sorted list of integers in ascending order. The class should have member functions for inserting an item in the list, deleting an item from the list, and searching the list for an item. Note: the search function should return the position of the item in the list (first item at position 0) and -1 if not found. In addition, it should have member functions to display the...
Make in c++ this Programming Challenge 1. Design your own linked list class to hold a...
Make in c++ this Programming Challenge 1. Design your own linked list class to hold a series of integers. The class should have member functions for appending, inserting, and deleting nodes. Don’t forget to add a destructor that destroys the list. Demonstrate the class with a driver program. 2. List Print Modify the linked list class you created in Programming Challenge 1 to add a print member function. The function should display all the values in the linked list. Test...
Develop a C++ "doubly" linked list class of your own that can hold a series of...
Develop a C++ "doubly" linked list class of your own that can hold a series of signed shorts Develop the following functionality: Develop a linked list node struct/class You can use it as a subclass like in the book (Class contained inside a class) You can use it as its own separate class Your choice Maintain a private pointer to a node class pointer (head) Constructor Initialize head pointer to null Destructor Make sure to properly delete every node in...
Could you check/edit my code? Design and implement your own linked list class to hold a...
Could you check/edit my code? Design and implement your own linked list class to hold a sorted list of integers in ascending order. The class should have member function for inserting an item in the list, deleting an item from the list, and searching the list for an item. Note: the search function should return the position of the item in the list (first item at position 0) and -1 if not found. In addition, it should member functions to...
(Write a C# program DO NOT USE CLASS)Implement the merge sort algorithm using a linked list...
(Write a C# program DO NOT USE CLASS)Implement the merge sort algorithm using a linked list instead of arrays. You can use any kind of a linked structure, such as single, double, circular lists, stacks and/or queues. You can populate your list from an explicitly defined array in your program. HINT: You will not be using low, middle and high anymore. For finding the middle point, traverse through the linked list while keeping count of the number of nodes. Break...
In this problem, you will write a program that reverses a linked list. Your program should...
In this problem, you will write a program that reverses a linked list. Your program should take as input a space-separated list of integers representing the original list, and output a space-separated list of integers representing the reversed list. Your algorithm must have a worst-case run- time in O(n) and a worst-case space complexity of O(1) beyond the input. For example, if our input is: 5 7 1 2 3 then we should print: 3 2 1 7 5 Please...
List.h template class List { // List class ADT              public:    virtual void...
List.h template class List { // List class ADT              public:    virtual void clear() = 0; // Inserts an item into the list at position index    virtual bool insert(const ListItemType& newItem) = 0;    // Append "it" at the end of the list    // The client must ensure that the list's capacity is not exceeded    virtual bool append(const ListItemType& newItem) = 0;    // Deletes an item from the list at a given position...
C++ Using an appropriate definition of ListNode, design a simple linked list class with only two...
C++ Using an appropriate definition of ListNode, design a simple linked list class with only two member functions and a default constructor: void add(double x); boolean isMember(double x); LinkedList( ); The add function adds a new node containing x to the front (head) of the list, while the isMember function tests to see if the list contains a node with the value x. Test your linked list class by adding various numbers to the list and then testing for membership....
C++ coding functions Implement the following class using linked lists. Creating a simple linked list class...
C++ coding functions Implement the following class using linked lists. Creating a simple linked list class to use is probably your first step.(Please do not use collections like .push() . pop(), etc.) and instead create the implementation A templated stack class that stores type T with the following public functions: - void Push(T t) - adds t to the top of the stack - T Pop() - asserts that the stack is not empty then removes and returns the item...
C++ Linked Lists Practice your understanding of linked lists in C++ by creating a list of...
C++ Linked Lists Practice your understanding of linked lists in C++ by creating a list of songs/artist pairs. Allow your user to add song / artist pairs to the list, remove songs (and associated artist) from the list and be sure to also write a function to print the list! Have fun! Make sure you show your implementation of the use of vectors in this lab (You can use them too ) You MUST modularize your code ( meaning, there...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT