In: Computer Science
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;
}
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