Please follow these short instructions. I have completed the shell of the progra
ID: 3534263 • Letter: P
Question
Please follow these short instructions. I have completed the shell of the program below the instructions that you can copy and paste. Thank you so much for your time. I really need some help at the moment
#include "stdafx.h"
#include <fstream>
#include <string>
#include <iostream>
using namespace std;
struct ListNode
{
double value; // The value in this node
struct ListNode *next; // To point to the next node
};
ListNode *head; // List head pointer
void appendNode(double num)
{
ListNode *newNode; // To point to a new node
ListNode *nodePtr; // To move through the list
newNode = new ListNode; // Allocate a new node and store num there.
newNode->value = num;
newNode->next = NULL;
if (!head) // If there are no nodes in the list make newNode the first node.
head = newNode;
else // Otherwise, insert newNode at end.
{
nodePtr = head; // Initialize nodePtr to head of list.
while (nodePtr->next) // Find the last node in the list.
nodePtr = nodePtr->next;
nodePtr->next = newNode;
}
}
void insertNode(double num)
{
ListNode *newNode; // A new node
ListNode *nodePtr; // To traverse the list
ListNode *previousNode = NULL; // The previous node
newNode = new ListNode; // Allocate a new node and store num there.
newNode->value = num;
if (!head) // If there are no nodes in the list make newNode the first node
{
head = newNode;
newNode->next = NULL;
}
else // Otherwise, insert newNode
{
nodePtr = head; // Position nodePtr at the head of list.
previousNode = NULL; // Initialize previousNode to NULL.
while (nodePtr != NULL && nodePtr->value < num) // Skip all nodes whose value // is less than num.
{
previousNode = nodePtr;
nodePtr = nodePtr->next;
}
if (previousNode == NULL) // If the new node is to be the 1st in the list, // insert it before all other nodes.
{
head = newNode;
newNode->next = nodePtr;
}
else // Otherwise insert after the previous node.
{
previousNode->next = newNode;
newNode->next = nodePtr;
}
}
}
void displayList()
{
ListNode *nodePtr; // To move through the list
nodePtr = head; // Position nodePtr at the head of the list.
while (nodePtr) // Step through the list
{
cout << nodePtr->value << endl;
nodePtr = nodePtr->next;
}
}
void deleteNode(double num)
{
ListNode *nodePtr; // To traverse the list
ListNode *previousNode; // To point to the previous node
if (!head) // If the list is empty, do nothing.
return;
if (head->value == num) // Determine if the first node is the one.
{
nodePtr = head->next;
delete head;
head = nodePtr;
}
else
{
nodePtr = head; // Initialize nodePtr to head of list
void delList()
{
ListNode *nodePtr; // To traverse the list
ListNode *nextNode; // To point to the next node
nodePtr = head; // Position nodePtr at the head of the list.
while (nodePtr != NULL) // While nodePtr is not at the end of the list...
{
nextNode = nodePtr->next; // Save a pointer to the next node.
delete nodePtr; // Delete the current node.
nodePtr = nextNode; // Position nodePtr at the next node.
}
}
int main()
{
// Define a NumberList object.
// NumberList list;
// Build the list with some values.
appendNode(2.5);
appendNode(7.9);
appendNode(12.6);
// Display the list.
cout << "Here are the initial values: ";
displayList();
cout << endl;
// Delete the middle node.
cout << "Now deleting the node in the middle. ";
deleteNode(7.9);
// Display the list.
cout << "Here are the nodes left. ";
displayList();
cout << endl;
// Delete the last node.
cout << "Now deleting the last node. ";
deleteNode(12.6);
// Display the list.
cout << "Here are the nodes left. ";
displayList();
cout << endl;
// Delete the only node left in the list.
cout << "Now deleting the only remaining node. ";
deleteNode(2.5);
// Display the list.
cout << "Here are the nodes left. ";
displayList();
system ("PAUSE");
return 0;
}
Explanation / Answer
#include <fstream>
#include <string>
#include <iostream>
using namespace std;
struct ListNode
{
int id;
string name;
string address;
string city;
string state;
string zipCode;
struct ListNode *next; // To point to the next node
};
ListNode *head; // List head pointer
void appendNode(int id, string name, string address, string city, string state, string zipCode)
{
ListNode *newNode; // To point to a new node
ListNode *nodePtr; // To move through the list
newNode = new ListNode; // Allocate a new node and store id there.
newNode->id = id;
newNode->name = name;
newNode->address = address;
newNode->city = city;
newNode->state = state;
newNode->state = zipCode;
newNode->next = NULL;
if (!head) // If there are no nodes in the list make newNode the first node.
head = newNode;
else // Otherwise, insert newNode at end.
{
nodePtr = head; // Initialize nodePtr to head of list.
while (nodePtr->next) // Find the last node in the list.
nodePtr = nodePtr->next;
nodePtr->next = newNode;
}
}
void insertNode(int id, string name, string address, string city, string state, string zipCode)
{
ListNode *newNode; // A new node
ListNode *nodePtr; // To traverse the list
ListNode *previousNode = NULL; // The previous node
newNode = new ListNode; // Allocate a new node and store id there.
newNode->id = id;
newNode->name = name;
newNode->address = address;
newNode->city = city;
newNode->state = state;
newNode->state = zipCode;
if (!head) // If there are no nodes in the list make newNode the first node
{
head = newNode;
newNode->next = NULL;
}
else // Otherwise, insert newNode
{
nodePtr = head; // Position nodePtr at the head of list.
previousNode = NULL; // Initialize previousNode to NULL.
while (nodePtr != NULL && nodePtr->id < id) // Skip all nodes whose id // is less than id.
{
previousNode = nodePtr;
nodePtr = nodePtr->next;
}
if (previousNode == NULL) // If the new node is to be the 1st in the list, // insert it before all other nodes.
{
head = newNode;
newNode->next = nodePtr;
}
else // Otherwise insert after the previous node.
{
previousNode->next = newNode;
newNode->next = nodePtr;
}
}
}
void displayList()
{
ListNode *nodePtr; // To move through the list
nodePtr = head; // Position nodePtr at the head of the list.
while (nodePtr) // Step through the list
{
cout<<"ID:"<<nodePtr->id<<endl;
cout << nodePtr->name <<" "<< nodePtr->address<<","<<nodePtr->city<<" "<<nodePtr->state<<","<<nodePtr->zipCode<<endl;
cout<<endl;
nodePtr = nodePtr->next;
}
}
void deleteNode(int id)
{
ListNode *nodePtr; // To traverse the list
ListNode *nextNode; // To point to the next node
if (!head) // If the list is empty, do nothing.
return;
nodePtr = head;
if(head -> next == NULL)
{
delete head;
head = 0;
return;
}
while (nodePtr->next != NULL) // While nodePtr is not at the end of the list...
{
if (nodePtr->next->id == id) // Determine if the node is the one.
{
nextNode = nodePtr->next->next; // Save a pointer to the next node.
delete nodePtr->next; // Delete the current node.
nodePtr->next = nextNode; // Position nodePtr at the next node.
}
else
nodePtr = nodePtr->next; // Initialize nodePtr of lis
}
}
void delList()
{
ListNode *nodePtr; // To traverse the list
ListNode *nextNode; // To point to the next node
if(head)
{
nodePtr = head; // Position nodePtr at the head of the list.
while (nodePtr->next != NULL) // While nodePtr is not at the end of the list...
{
nextNode = nodePtr->next; // Save a pointer to the next node.
delete nodePtr; // Delete the current node.
nodePtr = nextNode; // Position nodePtr at the next node.
}
}
head = NULL;
}
int main()
{
// Define a idberList object.
// idberList list;
// Build the list with some ids.
appendNode(1,"Tom Hays","123 Maple Dr","Nashville","TN","37209");
appendNode(2,"MAry Smith","456 June Dr","Nashville","TN","37220");
appendNode(3,"Susan Bell","776 King Rd","Nashville","TN","37214");
// Display the list.
cout << "Here are the initial values: ";
displayList();
cout << endl;
// Delete the middle node.
cout << "Now deleting the node in the middle. ";
deleteNode(2);
// Display the list.
cout << "Here are the nodes left. ";
displayList();
cout << endl;
// Display the list.
cout << "Here are the nodes left. ";
displayList();
cout << endl;
// Delete the only node left in the list.
cout << "Now deleting the only remaining node. ";
delList();
// Display the list.
cout << "Here are the nodes left. ";
displayList();
system ("PAUSE");
return 0;
}