// This is the implementation file for the method that deletes the ith item // f
ID: 3553634 • Letter: #
Question
// This is the implementation file for the method that deletes the ith item
// from a list of a ListClass object
#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.
//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.
else
cout<<"Not enough items in list ";
}
}//end of deleteIthNode
Explanation / Answer
First Box:-
while(j != i) {
prev = q;
q = q->next;
if(q == NULL) {
cout<<"Not enough items in list ";
break;
}
j++;
}
Second Box:-
if(j==i) {
prev->next = q;
delete q;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.