Inactive Tutor answered 04/08/22
I'm guessing that struct Node is already defined in another file, possibly in structs.h. If so, you can remove it from the .cpp file.
Taiwo A.
asked 04/08/22file deleteIthNode.cpp so that it finishes the implementation of the method deleteIthNode. This method deletes the ith item from a list of items.
#include "structs.h"
//Function to delete the ith node from the list
// If there are fewer than i nodes in the list, an error is returned.
struct Node {
int data;
struct Node* next;
};
void ListClass::deleteIthNode(int i)
{
NodePtrType q = head; //q will point to the item to delete
NodePtrType prev; //prev will point to the item before q
int j = 1; //j will keep count of which node in the list
// we are currently on
if (head == NULL)
cout<<"Not enough items in list\n";
else
{
//Traverse the list to find the ith item
//Replace this with a while loop that will traverse
//the list until either j == i or the end of the list
//is encountered.
while(i != j and q->next != NULL)
{
prev = q;
q = q->next;
j++;
}
//If there are i items in the list, delete the ith one
//Replace this with the if part of an if-else statement.
//The if part should delete the ith item if there is an
//ith item.
if(i == j)
{
if(prev == NULL)
head = q->next;
else
prev->next = q->next;
free(q);
}
else
cout<<"Not enough items in list\n";
}
}//end of deleteIthNode
i got an error like this
deleteIthNode.cpp:17:8: error: redefinition of ‘struct Node’
struct Node {
Inactive Tutor answered 04/08/22
I'm guessing that struct Node is already defined in another file, possibly in structs.h. If so, you can remove it from the .cpp file.
Get a free answer to a quick problem.
Most questions answered within 4 hours.
Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.