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

Here is the code I have so far. I know that I need to check to fix if the node d

ID: 3649274 • Letter: H

Question

Here is the code I have so far. I know that I need to check to fix if the node deleted is the head, so I have to change the pointer which I need help with. Also I need to check if the node to delete is in the middle, how do I change the pointers for that as well?


#include <iostream>
using namespace std;

struct LLNode {
int value;
LLNode *next;
};

LLNode * add_to_front (int value, LLNode *head);
void print( LLNode *head);
LLNode * search (LLNode* head, int key);


LLNode * remove (LLNode* head, LLNode *ptr_to_remove);



int main() {
LLNode *head = NULL; //empty list

cout << "Hey, give me number: ";
int v;
cin >> v;
head = add_to_front(v,head);

cin >> v;
head = add_to_front(v,head);

cin >> v;
head = add_to_front(v,head);

print(head);

cout << " value to search for: " << endl;
cin >> v;
LLNode *result = search(head, v);
if(result == NULL) {
cout << v << " is not in the list " << endl;
}
else {
cout << "found " << result->value << endl;
}

}


//search for key in the list pointed to by head
//if not in list, return NULL
LLNode * search (LLNode* head, int key) {
LLNode * current = head;
while(current != NULL) {
if ( current->value == key ) {
return current;
}
current = current->next;
}
return NULL;
}

//this function will return the new head of the list
LLNode * add_to_front (int value, LLNode *head) {
//create the box with the value
LLNode *new_node = new LLNode;
new_node->value = value;

//make the box point to the old head
new_node->next = head;

//make the box be the new head
head = new_node;
return head;

}


//print the entire list or empty if list is empty
void print( LLNode *head) {
if(head == NULL) {
cout << "Empty List" << endl;
return; // quit printing cuz I'm done!
}

LLNode *current = head;
while(current != NULL) {
cout << current->value << " ";
current = current->next;
}

//for( LLNode *current = head; current != NULL; current = current->next)
// cout << current->value << " ";

//this for loop implements the same as the while above
cout << endl;

}



//write code to remove ptr_to_remove from the list
//pointed to by head
//return new or old head as necessary
LLNode * remove (LLNode* head, LLNode *ptr_to_remove) {
if(head == NULL) { //list was empty, can't remove
return NULL; //return empty list
}
if(ptr_to_remove == NULL) { //can't remove NULL
return head;
}
if(head == ptr_to_remove) {
//do something to remove head
}
//code to remove other elements goes here


}

Explanation / Answer

#include using namespace std; struct LLNode { int value; LLNode *next; }; LLNode * add_to_front (int value, LLNode *head); void print( LLNode *head); LLNode * search (LLNode* head, int key); LLNode * remove (LLNode* head, LLNode *ptr_to_remove); int main() { LLNode *head = NULL; //empty list cout > v; head = add_to_front(v,head); cin >> v; head = add_to_front(v,head); cin >> v; head = add_to_front(v,head); print(head); cout