Question

in c++ Each of the member functions in WrongCode.cpp has errors in the way it performs...

in c++

Each of the member functions in WrongCode.cpp has errors in the way it performs a linked list operation. Find as many mistakes as you can

//--------------------one

void NumberList::appendNode(double num)

{

ListNode *newNode, *nodePtr;

// Allocate a new node & store num

newNode = new listNode;

newNode->value = num;

// If there are no nodes in the list

// make newNode the first node.

if (!head)

head = newNode;

else // Otherwise, insert newNode.

{

// Find the last node in the list.

while (nodePtr->next)

nodePtr = nodePtr->next;

// Insert newNode as the last node.

nodePtr->next = newNode;

}

}

//--------------------two

void NumberList::deleteNode(double num)

{

ListNode *nodePtr, *previousNode;

// If the list is empty, do nothing.

if (!head)

return;

// Determine if the first node is the one.

if (head->value == num)

delete head;

else

{

// Initialize nodePtr to head of list.

nodePtr = head;

// Skip all nodes whose value member is

// not equal to num.

while (nodePtr->value != num)

{

previousNode = nodePtr;

nodePtr = nodePtr->next;

}

// Link the previous node to the node after

// nodePtr, then delete nodePtr.

previousNode->next = nodePtr->next;

delete nodePtr;

}

//--------------------there

NumberList::~NumberList()

{

ListNode *nodePtr, *nextNode;

nodePtr = head;

while (nodePtr != nullptr)

{

nextNode = nodePtr->next;

nodePtr->next = nullptr;

nodePtr = nextNode;

}

}

Homework Answers

Answer #1

In the above problem, I have updated the code and it is marked in bold. which are the changes in the code.

void NumberList::appendNode(double num)

{

ListNode *newNode, *nodePtr;

// Allocate a new node & store num

newNode = new listNode;

newNode->value = num;

newNode->next = NULL; // This is to be added in order to end the list.

// If there are no nodes in the list

// make newNode the first node.

if (!head)

head = newNode;

else // Otherwise, insert newNode.

{

// Find the last node in the list.
// nodePtr = head; // This is the node pointer which is to be added

while (nodePtr->next)

nodePtr = nodePtr->next;

// Insert newNode as the last node.

nodePtr->next = newNode;

}

}

//--------------------two

void NumberList::deleteNode(double num)

{

ListNode *nodePtr, *previousNode;

// 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;

head = nodePtr;

delete nodePtr;

else

{

// Initialize nodePtr to head of list.

nodePtr = head;

// Skip all nodes whose value member is

// not equal to num.

while (nodePtr->value != num)

{

previousNode = nodePtr;

nodePtr = nodePtr->next;

}

// Link the previous node to the node after

// nodePtr, then delete nodePtr.

previousNode->next = nodePtr->next;

delete nodePtr;

}

//--------------------there

NumberList::~NumberList()

{

ListNode *nodePtr, *nextNode;

nodePtr = head;

while (nodePtr != nullptr)

{

nextNode = nodePtr->next;

nodePtr->next = nullptr;

delete nodePtr; // This will delete the memory allocated for the node

nodePtr = nextNode;

}

}

That was a nice question to answer
Friend, If you have any doubts in understanding do let me know in the comment section. I will be happy to help you further.
Please like if you think effort deserves like.
Thanks

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
my code has several functions; delete and backward functions are not working, rewrite the code for...
my code has several functions; delete and backward functions are not working, rewrite the code for both functions and check them in the main: #include<iostream> #include<cassert> using namespace std; struct nodeType {    int info;    nodeType *link; }; class linkedList { public:    void initializeList();    bool isEmptyList();    void print();    int length();    void destroyList();    void insertFirst(int newItem);    void insertLast(int newItem);    int front();    linkedList();    void copyList(const linkedList otherList);    void insertNewValue(int value);...
Write a program that will read the information from a file into a list and then...
Write a program that will read the information from a file into a list and then display the list to the screen. Remove the fifth item in the list and display the list again. Ask the program user for an entry into the list and add it to the list. Display the list one last time. disneyin.txt file daisy   123 donald   345 goofy   654 mickey   593 minnie   489 daffy   432 pluto   765 huey   321 dewey   987 lewey   554 porky   333...
IN C++ PLEASE!!! What needs to be done is in the code itself where it is...
IN C++ PLEASE!!! What needs to be done is in the code itself where it is written TO DO List! #include<iostream> using namespace std; template<typename T> class DoubleList{​​​​​     class Node{​​​​​     public: T value; Node* next; Node* prev;         Node(T value = T(), Node* next = nullptr, Node* prev = nullptr){​​​​​             this->value = value;             this->next = next;             this->next = next;         }​​​​​     }​​​​​;     int size;     Node* head;     Node* tail; public:     DoubleList(){​​​​​         size = 0;         head = nullptr;     }​​​​​     int length(){​​​​​         return size;     }​​​​​...
Data Structure in C++ I keep getting the same warning, and I cant seem to fix...
Data Structure in C++ I keep getting the same warning, and I cant seem to fix it.. Can you explain to me what I am doing wrong? Warning: dlist.cc: In function 'std::ostream& operator<<(std::ostream&, dlist&)': dlist.cc:66:10: error: invalid initialization of reference of type 'std::ostream& {aka std::basic_ostream&}' from expression of type 'dlist::node*' dlist.cc: In function 'dlist operator+(dlist&, dlist&)': dlist.cc:93:8: error: invalid operands of types 'dlist::node*' and 'dlist::node*' to binary 'operator+' dlist.cc:97:8: error: could not convert 'result' from 'int' to 'dlist' My code:...
Question: I get a Segmentation fault error sometimes when I addElementBack or print. Am I using...
Question: I get a Segmentation fault error sometimes when I addElementBack or print. Am I using pointers correctly and deleting memory properly? #ifndef DYNAMICARRAY_H #define DYNAMICARRAY_H #include <cstdlib> #include <iostream> using namespace std; // Node class class Node { int data; Node* next; Node* prev; public: Node(); Node(int); void SetData(int newData) { data = newData; }; void SetNext(Node* newNext) { next = newNext; }; void SetPrev(Node* newPrev) { prev = newPrev; }; int getData() { return data; }; Node* getNext()...
could you implement this function please, im having issues with it. void makeList (const ListNode::value_type [],const...
could you implement this function please, im having issues with it. void makeList (const ListNode::value_type [],const size_t& count) class ListNode { public: typedef int value_type; ListNode (value_type d = value_type(), ListNode* n = NULL) { datum = d; next = n; }    //Assessor value_type getDatum () const { return datum; } ListNode const* getNext () const { return next; }    //Mutator void setDatum (const value_type& d) {datum = d; } ListNode* getNext () { return next; } void...
The Binary Search Tree implementation for bst.zip. The code in the destructor of the BST class...
The Binary Search Tree implementation for bst.zip. The code in the destructor of the BST class is empty. Complete the destructor so the memory allocated for each node in the BST is freed. Make a couple of different trees in your main method or in a function to test the destructor (the program should not crash upon exiting). bst.zip (includes the following files below in c++): bst.h: #pragma once #include #include "node.cpp" using namespace std; template class BST { public:...
Java Language Add a method (convertToRing ()) that converts the list to a circular. This is,...
Java Language Add a method (convertToRing ()) that converts the list to a circular. This is, which makes the last node instead of pointing to null point to the first. Code: class Node { int value; Node nextNode; Node(int v, Node n) { value = v; nextNode = n; } Node (int v) { this(v,null); } } class LinkedList { Node head; //head = null; LinkedList() { } int length() { Node tempPtr; int result = 0; tempPtr = head;...
Java Language Add a method (convertToRing ()) that converts the list to a circular. This is,...
Java Language Add a method (convertToRing ()) that converts the list to a circular. This is, which makes the last node instead of pointing to null point to the first. Code: class Node { int value; Node nextNode; Node(int v, Node n) { value = v; nextNode = n; } Node (int v) { this(v,null); } } class LinkedList { Node head; //head = null; LinkedList() { } int length() { Node tempPtr; int result = 0; tempPtr = head;...
I'm not sure how to fix my code I keep getting an error with rhs.begin. I...
I'm not sure how to fix my code I keep getting an error with rhs.begin. I linked the header file and the test file, THESE TWO CANNOT be changed they have absolutely no errors in them. To clarify I ONLY need help with the copy constructor part. /* ~~~~~~~~~~~~list.cpp~~~~~~~~~~~~~~~*/ #include #include "list.h" using namespace std; Node::Node(string element) { data = element; previous = nullptr; next = nullptr; } List::List() { first = nullptr; last = nullptr; } List::List(const List& rhs)//...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT