Taiwo A.

asked • 04/08/22

NEED HELP WITH THIS

 file 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 {


1 Expert Answer

By:

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.