Question: Help, i keep getting overloading error on line 38 of the header file - void destroy(); any suggestions? IntList.h struct ListNode { int value ;

Help, i keep getting overloading error on line 38 of the header file - void destroy(); any suggestions?

IntList.h

struct ListNode { int value; struct ListNode* next; }; class IntList { private: ListNode* head; void destroy(); public: IntList() { head = nullptr; } ~IntList(); void insertNode(int); void deleteNode(int); void print(); void reverse(); void removeByPos(int); void destroy(); } 

IntList.cpp

#include "IntList.h" #include #include using namespace std; IntList :: ~IntList() { destroy(); } void IntList :: insertNode(int value) { if(head == NULL) { head = new ListNode(); head->value = value; head->next = NULL; } else { //create new node for given value ListNode* newNode = new ListNode(); newNode->value = value; newNode->next = NULL; //declare variables ListNode* curr = head; ListNode* prev = NULL; //loop until last node while(curr != NULL) { // if given value is less than any node,we break and insert given value inbetween them (prev and current) if(value < curr->value) { break; } //hop to next node prev = curr; curr = curr->next; } //if previous is null, then given value is less than head node's value so insert given value as head and move prev head to next if(prev == NULL) { //point given node's next as head newNode->next = head; //mark the head as current node head = newNode; } else if(prev->next==NULL) //this is the case when we reached last node, i.e.. given value is not less than any node's value . So we insert that node at the end of list { //making newNode as last node prev->next = newNode; } else //if previous is not null then we insert given node at middle of previous and current node { //make newNode's next as previous's next newNode->next = prev->next; //make previous node's next as newNode prev->next = newNode; } } } void IntList :: deleteNode(int value) { //declare variables ListNode* curr = head; ListNode* prev = NULL; //loop until last node while(curr != NULL) { // if we reach appropriate node we break and delete them after while if(value == curr->value) { break; } //hop to next node prev = curr; curr = curr->next; } if(curr == NULL) return; if(prev==NULL) //case1: node to be deleted is head { ListNode* nodeToBeDeleted = head; //move head to its next, so we delete safely the required node head = head->next; free(nodeToBeDeleted); } else if(curr->next == NULL) //case2: node to be deleted is last node { //cutoff the last node by removing the link between it and its previous node prev->next = NULL; free(curr); } else if(prev != NULL && curr != NULL) //case3: node to be deleted is a not a head/ end node { ListNode* nodeToBeDeleted = curr; //if node to be deleted is in middle of any two nodes, no point prev's next -> curr's next then we delete current prev->next = curr->next; free(nodeToBeDeleted); } } void IntList :: print() { //declare variables ListNode* curr = head; //loop until last node while(curr != NULL) { cout<value << " "; //hop to next node curr = curr->next; } cout<//logic is: we're create another fresh linkedlist by creating new node of each node in current list and insert at head each time. // so first visited node becomes last, last becomes first and all other nodes arrange appropriately //declare variables ListNode* currHead = head; ListNode* newHead = NULL; //loop until last node while(currHead != NULL) { //create new node for each node in the current linked list ListNode* newNode = new ListNode(); newNode->value = currHead->value; newNode->next = NULL; if(newHead==NULL) { newHead = newNode; } else { newNode->next = newHead; newHead = newNode; } //hop to next node currHead = currHead->next; } //reassign head to newHead for poiting new list with nodes reversed head = newHead; } void IntList :: removeByPos(int value) { //declare variables ListNode* prev = NULL, *curr = head; //loop until last node and value not zero while(curr != NULL && value>=0) { //if position reaches zero,then that it is node to be deleted if(value==0) break; value= value-1; //hop to next node prev = curr; curr = curr->next; } if(value !=0) return; if(prev==NULL) //case1: node to be deleted is head i.e.. position 0 { ListNode* nodeToBeDeleted = head; //move head to its next, so we delete safely the required node head = head->next; free(nodeToBeDeleted); } else if(curr->next == NULL) //case2: node to be deleted is last node i.ee position= length of list { //cutoff the last node by removing the link between it and its previous node prev->next = NULL; free(curr); } else if(prev != NULL && curr != NULL) //case3: node to be deleted is a not a head/ end node { ListNode* nodeToBeDeleted = curr; //if node to be deleted is in middle of any two nodes, no point prev's next -> curr's next then we delete current prev->next = curr->next; free(nodeToBeDeleted); } } void IntList :: destroy() { //declare variables ListNode* current = head; ListNode* next = NULL; //loop until last node while (current != NULL) { //keep copy of next node because we delete the current node before moving to next node next = current->next; //delete the current node free(current); //move to next node current = next; } //mark head as null head = NULL; } 

main.cpp

#include #include #include "IntList.h"; using namespace std; int main() { IntList myList; cout<<"********************************************************************************** "; cout<<"Testing the insertNode function, nodes inserted in this order: 12, 4, 3, 5, 77, 13 "; cout<<"********************************************************************************** "; myList.insertNode(12); myList.insertNode(4); myList.insertNode(3); myList.insertNode(5); myList.insertNode(77); myList.insertNode(13); cout<<"Here are the values in myList: "; myList.print(); cout<<"************************************* "; cout<<"Testing the removeByPos function "; cout<<"************************************* "; cout<<"Removing node at position 0 which is the first node,3 should be removed... "; myList.removeByPos(0); myList.print(); cout<<"Removing node at position 4 which is the last node,77 should be removed... "; myList.removeByPos(4); myList.print(); cout<<"Removing node at position 2 which is a middle node,12 should be removed... "; myList.removeByPos(2); myList.print(); cout<<"Removing node 99, a position that is too big.List should remain the same... "; myList.removeByPos(99); myList.print(); cout<<" Testing the deleteNode function "; cout<<"Removing number 5, the first node, 5 should be removed... "; myList.deleteNode(5); myList.print(); cout<<"Removing number 77 which is at the end, 77 should be removed... "; myList.deleteNode(77); myList.print(); cout<<"Removing number 12 which is in middle of the list, 12 should be removed... "; myList.deleteNode(12); myList.print(); cout<<"Removing number 100 which is not in the list, list should remain the same... "; myList.deleteNode(100); myList.print(); cout<<" Adding back the deleted nodes "; myList.insertNode(12); myList.insertNode(5); myList.insertNode(77); myList.print(); cout<<"************************************* "; cout<<"Testing the remove function "; cout<<"************************************* "; myList.reverse(); myList.print(); return 0; } 

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!