Question
PROBLEM: Write a method named DeleteFront for the List class that takes an integer n as input. This method should remove the first n nodes
PROBLEM: Write a method named DeleteFront for the List class that takes an integer n as input. This method should remove the first n nodes from the list. You can assume that n will always be a positive integer.
Exisiting code:
void list :: addZeros()
//Declaring temp of type node* node* temp;
//Making temp points to start node * temp = start;
//Looping through each node in linked list while(temp != NULL){
//Creating new node nn = new node;
//Assigning data to 0 nn->data = 0;
//Making next of new node to next of temp nn->next = temp->next;
//Assigning next of temp to new node nn //So, new node will be the next node from temp temp->next = nn;
//Moving to the next to next node in the linked list //So, here we get next node after the newnode that we just added to the linked list temp = nn->next; } }
void List::sumToFront()
// point temp to the head node of the list
Node temp = start;
int sum = 0;
// traverse the list
while( temp!= NULL )
{
// add the current node to sum
sum += temp->data;
// go to next node
temp = temp->next;
}
//if sum is greater than 50
if ( sum > 50)
{
temp = new node;
temp -> next = start;
start = temp;
temp ->data = sum;
}
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started