Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

hi , could you help me to fix my error in code line ( linkList c++) NumberList.h // Specification file for the NumberList class #ifndef

hi ,

could you help me to fix my error in code line ( linkList c++)

NumberList.h

// Specification file for the NumberList class #ifndef NUMBERLIST_H #define NUMBERLIST_H

class NumberList { private: // Declare a structure for the list struct ListNode { double value; // The value in this node struct ListNode *next; // To point to the next node };

ListNode *head; // List head pointer

public: // Constructor NumberList() { head = nullptr; }

// Destructor ~NumberList();

// Linked list operations void appendNode(double); void insertNode(double); void deleteNode(double); void displayList() const; }; #endif

//---------------------NumberList.ccp------------------------------------------------------

// Implementation file for the NumberList class #include // For cout #include "NumberList.h" using namespace std;

//************************************************** // appendNode appends a node containing the * // value pased into num, to the end of the list. * //**************************************************

void NumberList::appendNode(double num) { ListNode *newNode; // To point to a new node ListNode *nodePtr; // To move through the list

// Allocate a new node and store num there. newNode = new ListNode; newNode->value = num; newNode->next = nullptr;

// If there are no nodes in the list // make newNode the first node. if (!head) head = newNode; else // Otherwise, insert newNode at end. { // Initialize nodePtr to head of list. nodePtr = head;

// Find the last node in the list. while (nodePtr->next) nodePtr = nodePtr->next;

// Insert newNode as the last node. nodePtr->next = newNode; } }

//************************************************** // displayList shows the value * // stored in each node of the linked list * // pointed to by head. * //**************************************************

void NumberList::displayList() const { ListNode *nodePtr; // To move through the list

// 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 << nodePtr->value << endl;

// Move to the next node. nodePtr = nodePtr->next; } }

//************************************************** // The insertNode function inserts a node with * // num copied to its value member. * //**************************************************

void NumberList::insertNode(double num) { ListNode *newNode; //A new node ListNode *nodePtr; //To traverse the List ListNode *previousNode = nullptr; //The previous node

//Allocate a new node and store num there. newNode = new ListNode; newNode->value = num;

// if there are no nodes in the list // make newNode the first node if (!head) { head = newNode; newNode->next = nullptr; } else // Otherwise , insert newNode { //position nodeptr at the head of list. nodePtr = head; // //Initialize previousNode to nullptr. previousNode = nullptr;

//skip all nodes whose value is less than num. while (nodePtr != nullptr && nodePtr->value < num) { previousNode = nodePtr; nodePtr = nodePtr->next; } // If the new node is to be the 1st the list, // insert it before all other nodes. if (previousNode == nullptr) { head = newNode; newNode->next = nodePtr; } else // otherwise insert after the previous node. { previousNode->next = newNode; newNode->next = nodePtr; } }

}

//************************************************** // The deleteNode function searches for a node * // with num as its value. The node, if found, is * // deleted from the list and from memory. * //**************************************************

void NumberList::deleteNode(double num) { ListNode *nodePtr; // To traverse the list ListNode *previousNode; // To point to the previous node

// If the list is empty, do nothing. if (!head) return;

// Determine if the first node is the one. if (head->value == num) { nodePtr = head->next; delete head; head = nodePtr; } else { // Initialize nodePtr to head of list nodePtr = head;

// Skip all nodes whose value member is // not equal to num. while (nodePtr != nullptr && nodePtr->value != num) { previousNode = nodePtr; nodePtr = nodePtr->next; }

// If nodePtr is not at the end of the list, // link the previous node to the node after // nodePtr, then delete nodePtr. if (nodePtr) { previousNode->next = nodePtr->next; delete nodePtr; } } }

//************************************************** // Destructor * // This function deletes every node in the list. * //**************************************************

NumberList::~NumberList() { 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 != nullptr) { // Save a pointer to the next node. nextNode = nodePtr->next;

// Delete the current node. delete nodePtr;

// Position nodePtr at the next node. nodePtr = nextNode; } }

//--------main.cpp---------------------

// This program demonstrates the displayList member function. #include #include "NumberList.h" using namespace std;

int main() { // Define a NumberList object. NumberList list;

// Append some values to the list. list.appendNode(2.5); list.appendNode(7.9); list.appendNode(12.6);

// Insert a node in the middle of the list. list.insertNode(10.5);

// Display the values in the list. cout << "Here are the inital values: "; list.displayList(); cout << endl; // Delete the middle node. cout << "Now deleting the node in the middle. "; list.deleteNode(7.9);

// Display the list. cout << "Here are the nodes left. "; list.displayList(); cout << endl;

// Delete the last node. cout << "Now deleting the last node. "; list.deleteNode(12.6);

// Display the list. cout << "Here are the nodes left. "; list.displayList(); cout << endl;

// Delete the only node left in the list. cout << "Now deleting the only remaining node. "; list.deleteNode(2.5);

// Display the list. cout << "Here are the nodes left. "; list.displayList(); return 0; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Inference Control In Statistical Databases From Theory To Practice Lncs 2316

Authors: Josep Domingo-Ferrer

2002nd Edition

3540436146, 978-3540436140

More Books

Students also viewed these Databases questions