Question: #include #include #include using namespace std; /* List Node: */ template class Node { private: T data; public: Node* next; Node* previous; Node(T data) {

 #include #include #include using namespace std; /* List Node: */ template
#include
#include
#include
using namespace std;
/* List Node: */
template class Node {
private:
T data;
public:
Node* next;
Node* previous;
Node(T data) {
this->data = data;
}
T getData() {
return this->data;
}
string toString() {
stringstream s;
s getData();
return s.str();
}
};
/* Linked List: */
template class DoublyLinkedList {
private:
T* head;
T* tail;
int list_size;
public:
DoublyLinkedList() {
this->head = NULL;
this->tail = NULL;
this->list_size = 0;
}
T* getHead() {
return this->head;
}
T* getTail() {
return this->tail;
}
int size(bool update=false) {
if (!update) {
return this->list_size;
}
int size = 0;
T* temp = this->head;
while (temp) {
size++;
temp = temp->next;
}
this->list_size = size;
return this->list_size;
}
void addNodeAsTail(T* new_node) {
new_node->next = NULL;
new_node->previous = NULL;
if (this->head == NULL) {
this->head = new_node;
this->tail = this->head;
this->list_size = this->list_size + 1;
} else {
this->tail->next = new_node;
new_node->previous = this->tail;
this->tail = new_node;
this->list_size = this->list_size + 1;
}
}
void addNodeAsHead(T* new_node) {
new_node->next = NULL;
new_node->previous = NULL;
if (this->head == NULL) {
this->head = new_node;
this->tail = this->head;
this->list_size = this->list_size + 1;
} else {
this->head->previous = new_node;
new_node->next = this->head;
this->head = new_node;
this->list_size = this->list_size + 1;
}
}
void printList() {
cout
T* temp = this->head;
while(temp) {
cout toString() ";
temp = temp->next;
}
cout
}
};
int main()
{
DoublyLinkedList > ll;
ll.addNodeAsHead(new Node(1));
ll.addNodeAsHead(new Node(10));
ll.printList();
Given a DoublyLinkedList and Node class, update DoublyLinkedList class to have in addition the following: Write a recursive C++ method removeElement (inside DoublyLinkedList class) that takes as parameters a linked ist and an element x then removes all the occurrences of x on the list. For instance, if the linked list is [10, 3, 2, 7, 2, 8, 9, 2] then after deleting the element 2, the list becomes [10, 3, 7, 8, 9]. Write a C++ method iterativeRemoveElement (inside DoublyLinkedList class) which behaves just like the method in part a but doesn't use recursion. Compare the two methods, consider performance, memory and readability a. b. c

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!