how to fix the error in the remove function (c++)
struct LinkedList {
public:
LinkedList () : head_(0) {};
~LinkedList () { delete_nodes (); };
// returns 0 on success,-1 on failure
int insert (const int &new_item) {
return ((head_ = new Node(new_item, head_)) != 0) ? 0 : -1;
}
// returns 0 on success, -1 on failure
int remove (const int &item_to_remove) {
Node *marker = head_;
Node *temp = 0; // temp points to one behind as we iterate
while (marker != 0) {
if (marker->value() == item_to_remove) {
if (temp == 0) { // marker is the first element in the list
if (marker->next() == 0) {
head_ = 0;
delete marker; // marker is the only element in the list
marker = 0;
} else {
head_ = new Node(marker->value(), marker->next());
delete marker;
marker = marker->next();
}
return 0;
} else {
temp->next (marker->next());
delete temp;
temp = 0;
return 0;
}
}
// marker = 0; // reset the marker
temp = marker;
marker = marker->next();
}
return -1; // failure
}
void print (void) {
Node *marker = head_;
while (marker != 0) {
printf("%d\n", marker->value());
marker = marker->next();
}
}
private:
void delete_nodes (void) {
Node *marker = head_;
while (marker != 0) {
Node *temp = marker;
delete marker;
marker = temp->next();
}
}
Step by stepSolved in 2 steps with 1 images
- My code does not produce the required output, can anyone let me know what seems to be off? Input: from Node import Node from LinkedList import LinkedList def print_linkedlist(list): print(f'Linked List ------ Nodes ----------------------------------') print(f'head: {list.head.name:<6}', end=' ') node = list.head while node != None: print(f"{node.name:<12}", end=' ') node = node.next print() print(f'tail: {list.tail.name:<6}', end=' ') node = list.head while node != None: print(f"data:{node.data:<7}", end=' ') node = node.next print() print(f' ', end=' ') node = list.head while node != None: if node.next != None: print(f"next:{node.next.name:<7}", end=' ') else: print("next:None") node = node.next print() print(f' ', end=' ') node = list.tail while node != None: if node.prev != None:…arrow_forwardGiven the tollowiıng class template detinition: template class linkedListType { public: const linked ListType& operator=(const linked ListType&); I/Overload the assignment operator. void initializeList(); /Initialize the list to an empty state. //Postcondition: first = nullptr, last = nullptr, count = 0; linked ListType(); Ildefault constructor //Initializes the list to an empty state. /Postcondition: first = nullptr, last = nullptr, count = 0; -linkedListīype(); Ildestructor /Deletes all the nodes from the list. //Postcondition: The list object is destroyed. protected: int count; //variable to store the number of elements in the list nodeType *first; //pointer to the first node of the list nodeType *last; //pointer to the last node of the list 1) Write initializeList() 2) Write linkedListType() 3) Write operator=0 4) Add the following function along with its definition: void rotate(); I/ Remove the first node of a linked list and put it at the end of the linked listarrow_forwardPlease answer the following section.arrow_forward
- Given main.py and a PersonNode class, complete the PersonList class by writing find_first() and find_last() methods at the end of the PersonList.py file. The find_first() method should find the first occurrence of an age value in the linked list and return the corresponding node. Similarly, the find_last() method should find the last occurrence of the age value in the linked list and return the corresponding node. For both methods, if the age value is not found, None should be returned. The program will replace the name value of each found node with a new name. Ex. If the input is: Alex 23 Tom 41 Michelle 34 Vicky 23 -1 23 Connor 34 Michela main.py(can not edit) from PersonNode import PersonNodefrom PersonList import PersonList if __name__ == "__main__": person_list = PersonList() # Read input lines until encountering '-1' input_line = input() while input_line != '-1': split_input = input_line.split(' ') name = split_input[0] age =…arrow_forwardIn a linked list, devise an approach that will remove all nodes with the same key.arrow_forwardThe nodes of the strongest linkedlist are undetermined.Is one especially lengthy?arrow_forward
- Computer Networking: A Top-Down Approach (7th Edi...Computer EngineeringISBN:9780133594140Author:James Kurose, Keith RossPublisher:PEARSONComputer Organization and Design MIPS Edition, Fi...Computer EngineeringISBN:9780124077263Author:David A. Patterson, John L. HennessyPublisher:Elsevier ScienceNetwork+ Guide to Networks (MindTap Course List)Computer EngineeringISBN:9781337569330Author:Jill West, Tamara Dean, Jean AndrewsPublisher:Cengage Learning
- Concepts of Database ManagementComputer EngineeringISBN:9781337093422Author:Joy L. Starks, Philip J. Pratt, Mary Z. LastPublisher:Cengage LearningPrelude to ProgrammingComputer EngineeringISBN:9780133750423Author:VENIT, StewartPublisher:Pearson EducationSc Business Data Communications and Networking, T...Computer EngineeringISBN:9781119368830Author:FITZGERALDPublisher:WILEY