Question: C++ ONLY PLEASE. I am having a problem with my insertNode() function. ********************************************************************* here is what i am trying to do: **************************************************************************** Here is my
C++ ONLY PLEASE.
I am having a problem with my insertNode() function.
*********************************************************************
here is what i am trying to do:

****************************************************************************
Here is my code:
********** list.h ***************
#ifndef List_H #define List_H
#include #include #include #include using namespace std; class List { private: // Declare a structure for the list struct ListNode { string value; struct ListNode *next; // To point to the next node };
ListNode *head; //List head pointer ListNode *tail; //List tail pointer
public: List() { head = NULL; tail = NULL; } ~List() { ListNode *nodePtr; // To traverse the list ListNode *nextNode; // To point to the next node
// Position nodePtr at the head of the list. nodePtr = head;
// While nodePtr is not at the end of the list... while (nodePtr != NULL) { // Save a pointer to the next node. nextNode = nodePtr->next;
// Delete the current node. delete nodePtr;
// Position nodePtr at the next node. nodePtr = nextNode; } }
void appendNode(string word) { ListNode *newNode; // To point to a new node
newNode = new ListNode; newNode->value = word; newNode->next = NULL;
// If there are no nodes in the list make newNode the first node. if (!head ) { head = newNode; tail = newNode; } else // Otherwise, insert newNode at end. { //set the current last node's next pointer to the new node tail->next = newNode; /ow the tail is the new node tail = newNode; } }
void insertNode(string word) //***********THIS IS WHERE THE PROBLEM IS************ { ListNode *current; ListNode *previous = NULL; ListNode *newNode; newNode = new ListNode; newNode->value = word; newNode->next = NULL; if(!head) { head = newNode; } else { while ((current != NULL) && current->value.compare(newNode->value) >= 0 ) { previous = current; current = current->next; } if(previous == NULL) { head = newNode; newNode->next = current; } else { previous->next = newNode; newNode->next = current; } } }
void displayList() const { ListNode *nodePtr; // To move through the list
if(head != NULL) { // Position nodePtr at the head of the list. nodePtr = head; // While nodePtr points to a node, traverse the list. while (nodePtr) { // Display the value in this node. cout value
// Move to the next node. nodePtr = nodePtr->next; } } else cout
void deleteNode(string word) { ListNode *nodePtr; // To traverse the list ListNode *previousNode; // To point to the previous node
if (!head) return;
if (head->value == word) { nodePtr = head->next; delete head; head = nodePtr; } else { nodePtr = head; while (nodePtr != NULL && nodePtr->value != word) { previousNode = nodePtr; nodePtr = nodePtr->next; }
if (nodePtr) { if(nodePtr == tail) { tail = previousNode; } previousNode->next = nodePtr->next; delete nodePtr; } } }
}; #endif
************** List.cpp **************
#include #include #include
#include "List.h"
using namespace std;
int main() { List* monsters = new List; cout appendNode("boogeyman"); monsters->appendNode("ghost"); monsters->appendNode("scarecrow"); monsters->appendNode("witch"); monsters->appendNode("zombie"); monsters->displayList(); cout insertNode("vampire"); monsters->displayList(); cout deleteNode("ghost"); monsters->displayList(); cout
return 0; }
************** My output - notice vampire is not sorted in alphabetically. Please help ***************

PLEASE ADJUCT MY CODE AS NEEDED WITH NOTES PLEASE. THANKS IN ADVANCE!!!
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. a. "boogeyman" b. "ghost" C. "scarecrow" d. "witch" e. "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. C:\CSC1318-183\Lab 5>g++ -o labs lab5.cpp C:\CSC1318-193\Lab 5>lab5 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 witch zombie Svampire Iam deleting ghost from the list. boogeyman scarecrow witch zombie vampire All nodes have been removed. C:\CSC1318-193\Lab 5>
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
