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++
Write a C++ code to insert the following numbers in two Linked Lists. Insert numbers of...
Write a C++ code to insert the following numbers in two Linked Lists. Insert numbers of first list in Linked List#1, and numbers of second list in Linked List#2. Do not insert both lists in a single Linked List. List#1. 5, 78, 45, 23, 11, 89, 10, 78, 6, 99, 876, 5, 67, 13 List#2. 5, 89, 688, 52, 557, 953, 5, 7, 55, 35, 89, 99, 99, 6, 557, 89, 5, 99, 6, 2, 45, 12, 7, 6, 94,...
Explain and demonstrate a Linked List by adding following items into a Linked List: 10, 30,...
Explain and demonstrate a Linked List by adding following items into a Linked List: 10, 30, 15, 25 (show your work, you may write on paper and upload if you prefer)
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...
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)
write a java code to implement a linked list, called CupList, to hold a list of...
write a java code to implement a linked list, called CupList, to hold a list of Cups. 1.Define and write a Cup node class, called CupNode, to hold the following information about a cup: •number (cup number) •capacity (cup capacity in ml) •Write a method size() that returns the number of elements in the linkedlist CupList. •Write a method getNodeAt() that returns the reference to cup node object at a specific position given as a parameter of the method. •Write...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT