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

Modify the linked list code from class to work with strings. Insert the following food items...
Modify the linked list code from class to work with strings. Insert the following food items into the list and display the list. The items are: bread, noodles, milk, bananas, eggs. Insert them in that order. Display the list. Then delete milk and redisplay the list. Then insert ice cream and redisplay the list. Then append zucchini and redisplay the list. c++
Linked List: Complete the following code to create a linked list from an Array. After creating...
Linked List: Complete the following code to create a linked list from an Array. After creating the list, display the elements of the linked list iteratively. Write two others function called as RDisplayTailRecursion(first) and RDisplayTailRecursion(first) which will print elements of the linked list using the tail and head recursions respectively. #include <stdio.h> #include <stdlib.h> struct Node { }*first=NULL; void create(int A[], int n) { for(i=1; i<n; i++) { } } void Display(struct Node*p) { while(p!=NULL) { } } void RDisplayTailRecursion...
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...
Java Write a menu driven program that implements the following linked list operations : INSERT (at...
Java Write a menu driven program that implements the following linked list operations : INSERT (at the beginning) INSERT_ALPHA (in alphabetical order) DELETE (Identify by contents, i.e. "John", not #3) COUNT CLEAR
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; }
Write PSEUDOCODE to insert a node at position 2 in a doubly-linked list (assume position follows...
Write PSEUDOCODE to insert a node at position 2 in a doubly-linked list (assume position follows classic indexing from 0 to item_count - 1)
Implement the ADT character string as the class LinkedString by using a linked list of characters....
Implement the ADT character string as the class LinkedString by using a linked list of characters. Include the following LinkedString constructors and methods: LinkedString(char[] value) Allocates a new character linked list so that it represents the sequence of characters currently contained in the character array argument. LinkedString(String original) Initializes a new character linked list so that it represents the same sequence of characters as the argument. char charAt(int index) Returns the char value at the specified index. The first character...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT