Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This is what I have so far, but it only works on some of the data files... My er

ID: 3544418 • Letter: T

Question

This is what I have so far, but it only works on some of the data files... My error is that it will read only one

line and will not go on to the next one. Can someone please show me how to fix it? Thanks

#include "inlab6.h"
#include "structs.h"

#include <iostream>
#include <fstream>
#include <stdlib.h>
using namespace std;

//Function to delete the ith node from the list
// If there are fewer than i nodes in the list, an error is returned.
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 ";
else
{
//Traverse the list to find the ith item
// The following is a while loop that will traverse
// the list until either j == i or the end of the list
// is encountered.
*********************



while (j==i)
{
q = head;
head= head->next;
}

**********************


//If there are i items in the list, delete the ith one
// The following is the if part of an if-else statement.
// The if part should delete the ith item if there is an
// ith item.


************************


if(prev == NULL)
{
head = head->next;
delete prev;
}


************************


else
cout<<"Not enough items in list ";
}
}//end of deleteIthNode

Explanation / Answer

#include "inlab6.h"

#include "structs.h"

#include <iostream>

#include <fstream>

#include <stdlib.h>

using namespace std;

void ListClass::deleteIthNode(int i)

{

NodePtrType q = head;

NodePtrType prev=NULL;

int j = 1;

if (head == NULL)

cout<<"Not enough items in list ";

else

{head=head->next;

while (j!=i)

{prev=q;

q = head;

head= head->next;

j++;

}

if(i==1)

{delete q;

}

else if(q!=NULL)

{prev->next=head;

delete q;

}

else

cout<<"Not enough items in list ";

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote