Question

In: Computer Science

Modify this linked list code to work with string. Insert the following items into the list...

Modify this linked list code to work with string. Insert the following items into the list and display the list. The items are: Pepsi, Coke, DrPepper, Sprite, Fanta. Insert them in that order. Display the list. Then delete DrPepper and redisplay the list. Then insert 7-UP and redisplay the list. Then append Water and redisplay the list. c++

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

#include <iostream>

using namespace std;

class ListNode {
public:
    int value;
    ListNode *next;

    ListNode(int nodeValue) {
      value = nodeValue;
      next = nullptr;
    }
};

class LinkedList {
private:
    ListNode *head;
public:
    LinkedList() { head = nullptr; }
    //~LinkedList(); // destructor

    void appendNode(int);
    void insertNode(int);
    void deleteNode(int);
    void displayList() const;

};

void LinkedList::appendNode(int newValue) {
ListNode *newNode;
ListNode *nodePtr;

newNode = new ListNode(newValue);

if (!head) {
     head = newNode;
}
else {
    nodePtr = head;
    while (nodePtr->next) {
        nodePtr = nodePtr->next;
    }
    nodePtr->next = newNode;
}
}

void LinkedList::displayList() const {
ListNode *nodePtr;
nodePtr = head;
while (nodePtr) {
    cout << nodePtr->value << " ";
    nodePtr = nodePtr->next;
}
}

void LinkedList::insertNode(int newValue) {
ListNode *newNode;
ListNode *nodePtr;
ListNode *previousNode = nullptr;

newNode = new ListNode(newValue);

if (!head) {
    head = newNode;
    newNode->next = nullptr;
}
else {

    nodePtr = head;
    /*cout << "nodeptr value when assigned as head: " <<
        nodePtr->value << endl;*/
    previousNode = nullptr;
    while (nodePtr != nullptr && nodePtr->value < newValue) {
        previousNode = nodePtr;
        nodePtr = nodePtr->next;
    }
    if (previousNode == nullptr) {
        head = newNode;
        newNode->next = nodePtr;
    }
    else {
        previousNode->next = newNode;
        newNode->next = nodePtr;
    }
}

}

void LinkedList::deleteNode(int searchValue) {
ListNode *nodePtr;
ListNode *previousNode;

if (!head) {
    return;
}
if (head->value == searchValue) {
    nodePtr = head->next;
    delete head;
    head = nodePtr;
}
else {
    nodePtr = head;
    while (nodePtr != nullptr && nodePtr->value != searchValue) {
        previousNode = nodePtr;
        nodePtr = nodePtr->next;
    }
    if (nodePtr) {
        previousNode->next = nodePtr->next;
        delete nodePtr;
    }
}
}

int main()
{
    LinkedList numbers;
    numbers.appendNode(1);
    numbers.appendNode(2);
    numbers.appendNode(4);
    numbers.displayList();
    cout << endl << endl;
    numbers.insertNode(3);
    numbers.displayList();
    cout << endl << endl;
    numbers.deleteNode(2);
    numbers.displayList();
    numbers.appendNode(6);
    cout << endl << endl;
    numbers.displayList();
    numbers.insertNode(5);
    cout << endl << endl;
    numbers.displayList();
    numbers.insertNode(2);
    cout << endl << endl;
    numbers.displayList();
    return 0;
}

Solutions

Expert Solution

Please find the answer below.
Please do comments in case of any issue. Also, don't forget to rate the question. Thank You So Much.

#include <iostream>
#include <string>

using namespace std;

class ListNode {
public:
   string value;
   ListNode *next;

   ListNode(string nodeValue) {
       value = nodeValue;
       next = nullptr;
   }
};

class LinkedList {
private:
   ListNode *head;
public:
   LinkedList() { head = nullptr; }
   //~LinkedList(); // destructor

   void appendNode(string);
   void insertNode(string);
   void deleteNode(string);
   void displayList() const;

};

void LinkedList::appendNode(string newValue) {
   ListNode *newNode;
   ListNode *nodePtr;

   newNode = new ListNode(newValue);

   if (!head) {
       head = newNode;
   }
   else {
       nodePtr = head;
       while (nodePtr->next) {
           nodePtr = nodePtr->next;
       }
       nodePtr->next = newNode;
   }
}

void LinkedList::displayList() const {
   ListNode *nodePtr;
   nodePtr = head;
   while (nodePtr) {
       cout << nodePtr->value << " ";
       nodePtr = nodePtr->next;
   }
}

void LinkedList::insertNode(string newValue) {
   ListNode *newNode;
   ListNode *nodePtr;
   ListNode *previousNode = nullptr;

   newNode = new ListNode(newValue);

   if (!head) {
       head = newNode;
       newNode->next = nullptr;
   }
   else {

       nodePtr = head;
       /*cout << "nodeptr value when assigned as head: " <<
nodePtr->value << endl;*/
       previousNode = nullptr;
       while (nodePtr != nullptr && nodePtr->value < newValue) {
           previousNode = nodePtr;
           nodePtr = nodePtr->next;
       }
       if (previousNode == nullptr) {
           head = newNode;
           newNode->next = nodePtr;
       }
       else {
           previousNode->next = newNode;
           newNode->next = nodePtr;
       }
   }

}

void LinkedList::deleteNode(string searchValue) {
   ListNode *nodePtr;
   ListNode *previousNode;

   if (!head) {
       return;
   }
   if (head->value == searchValue) {
       nodePtr = head->next;
       delete head;
       head = nodePtr;
   }
   else {
       nodePtr = head;
       while (nodePtr != nullptr && nodePtr->value != searchValue) {
           previousNode = nodePtr;
           nodePtr = nodePtr->next;
       }
       if (nodePtr) {
           previousNode->next = nodePtr->next;
           delete nodePtr;
       }
   }
}

int main()
{
   LinkedList numbers;
   numbers.appendNode("Pepsi");
   numbers.appendNode("Coke");
   numbers.appendNode("DrPepper");
   numbers.displayList();
   cout << endl << endl;
   numbers.insertNode("Sprite");
   numbers.deleteNode("Fanta");
   numbers.displayList();
   cout << endl << endl;
   numbers.deleteNode("DrPepper");
   numbers.displayList();
   return 0;
}

output


Related Solutions

java Modify doubly Linked List code to include following index (rank) based access operations int RetrieveAt(int...
java Modify doubly Linked List code to include following index (rank) based access operations int RetrieveAt(int index) void DeleteAt(int index) void Swap(int index, int index) index starts at 0 (just like array's subscript) If the index is out of bound, RetrieveAt returns 0, DeleteAt does nothing, do nothing in Swap. Write your own testing program to test the modified class -----------------------------------------DLinkedlist.java---------------------------- public class DLinkedList { private class Node { String data; Node next; Node prev; public Node(String s) { data...
Take the following code and modify the if(args >= 3) statement to work with a dynamic...
Take the following code and modify the if(args >= 3) statement to work with a dynamic amount of inputs. Example: ./cat file1 file2 file3 file4 filen-1 filen should output a text file that concatenates the files file1 to filen into one file #include <stdio.h> #include <stdlib.h> int main(int argc, char **argv) {    char ch;    if (argc ==1)    {        while((ch = fgetc(stdin)) != EOF) fputc(ch, stdout);    }    if (argc == 2)    {   ...
modify the code below to create a GUI program that accepts a String from the user...
modify the code below to create a GUI program that accepts a String from the user in a TextField and reports whether or not there are repeated characters in it. Thus your program is a client of the class which you created in question 1 above. N.B. most of the modification needs to occur in the constructor and the actionPerformed() methods. So you should spend time working out exactly what these two methods are doing, so that you can make...
How to convert Sudo Code to C language? keyboard_process() {//insert a string. //check for shared memory...
How to convert Sudo Code to C language? keyboard_process() {//insert a string. //check for shared memory If(shared memory not full) { //sendthe string to the shared memory //send signal to process; }
Java The List ADT has an interface and a linked list implementation whose source code is...
Java The List ADT has an interface and a linked list implementation whose source code is given at the bottom of this programming lab description. You are to modify the List ADT's source code by adding the method corresponding to the following UML: +hasRepeats() : boolean hasRepeats() returns true if the list has a value that occurs more than once in the list hasRepeats() returns false if no value in the list occurs more than once in the list For...
Java The List ADT has an interface and a linked list implementation whose source code is...
Java The List ADT has an interface and a linked list implementation whose source code is given at the bottom of this programming lab description. You are to modify the List ADT's source code by adding the method corresponding to the following UML: +hasRepeats() : boolean hasRepeats() returns true if the list has a value that occurs more than once in the list hasRepeats() returns false if no value in the list occurs more than once in the list For...
The file supplied.o contains code that can build, display, and destroy a linear linked list (singly-linked)....
The file supplied.o contains code that can build, display, and destroy a linear linked list (singly-linked). For this lab, you will need to write the following two functions in list.cpp, and add function prototypes for them to list.h. The provided main.cpp has calls to each of these functions commented out. As you write the functions, uncomment them from main.cpp. void reverse(node * head, node *& newHead) Recursively make a revserse copy of the source list with head where newhead is...
Java: Determine the ouotput of this code ArrayList<String>list=new ArrayList<String>();             list.add("Namath");           &
Java: Determine the ouotput of this code ArrayList<String>list=new ArrayList<String>();             list.add("Namath");             list.add("Sauer");             list.add("Maynard");             list.add("Namath");             list.add("Boozer");             list.add("Snell");             list.add("Namath");             list.add("Atkinson");             list.add("Lammonds");             list.add("Dockery");             list.add("Darnold");             list.remove(2);             list.set(2, "Brady");             list.remove(2);             list.set(4,"Unitas");             list.add(1,"Lamomica");             list.add(3,"Hanratty");             list.remove("Namath");             list.remove(list.size()-1);             list.remove(2);             list.set(7, "Riggins");             Iterator iter = list.iterator();           while (iter.hasNext())         {   System.out.print(iter.next() + " ");                         }                     } }
I know this code takes in a set of numbers into a doubly linked list and...
I know this code takes in a set of numbers into a doubly linked list and sorts it using insertion sort. Could you explain exactly how this code is working? main.c Source Code: #include #include #include "node.h" int main() { struct mynode *head=NULL; int value; printf("Give first value: \n"); scanf("%d",&value); printf("Give next values, and input 0 to end list: \n"); do{ if(value>0){    head = pushNode(head, value);    scanf("%d",&value); } }while (value>0); printf("Before insertion sort: "); printlist(head); head=insertsort(head); printf("After insertion...
This is the code what I have for doubly linked list for STACK. This is Python...
This is the code what I have for doubly linked list for STACK. This is Python language and I want anyone to help me with the following questions. Can you check for me if it is good Doubly Linked List? ####THIS IS THE ENTIRE ASSIGNMENT#### ADD the Following feature: Include a class attribute in the container class called name. In the implementation - Pod: You should ask the user to enter the name of the container and the program should...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT