In: Computer Science
Please only edit the list.cpp file only, implement the push_front method that will insert a new element to the front of the list. |
//list.h // Doubly linked list template<typename T> class List; template <typename T> template <typename T>
#endif |
//list.cpp // Implement the push_front method #include <string> using namespace std; // Node class implemenation template <typename T> // List implementation template <typename T>
} template <typename T>
template <typename T> // Iterator implementation template <typename T>
template <typename T> template <typename T> template <typename T> |
list_test.cpp // Test for push_front method int main() { // Should print - kings of king Ozymandias is name
My } |
The required code followed by the screenshot of output has been added below, please note that the modification is in list.cpp file only so I have given only that file not all. Also to run your code, replace q2 with list in remaining files as q2 do not exist here to include. The explanation of the modified code has been added in front of it in comment lines.
CODE:
//list.cpp
// Implement the push_front method
#include <string>
#include "list.h"
using namespace std;
// Node class implemenation
template <typename T>
Node<T>::Node(T element) { // Constructor
data = element;
previous = nullptr;
next = nullptr;
}
// List implementation
template <typename T>
List<T>::List() {
head = nullptr;
tail = nullptr;
}
template <typename T>
List<T>::~List() { // Destructor
for(Node<T>* n = head; n != nullptr; n = n->next) {
delete n; //Deconstructor create an error evertime i ran it.
}
}
template <typename T>
void List<T>::push_front(T element) {
Node<T>* newnode = new Node<T>(element); // made a new node
Iterator<T> iterhead; // made an iterator for head
iterhead.position = head; // set its position to head
Iterator<T> iternew; // made an iterator for new
iternew.position = newnode; // set its position to new
iternew.position->next = head; // set the next of new node to head
if (head == nullptr) // if head is null then add the newnode to tail
tail = newnode;
else // else add to the previous of head
iterhead.position->previous = newnode;
head = newnode; // in last made the new node to head.
}
template <typename T>
Iterator<T> List<T>::begin() {
Iterator<T> iter;
iter.position = head;
iter.container = this;
return iter;
}
template <typename T>
Iterator<T> List<T>::end() {
Iterator<T> iter;
iter.position = nullptr;
iter.container = this;
return iter;
}
// Iterator implementation
template <typename T>
Iterator<T>::Iterator() {
position = nullptr;
container = nullptr;
}
template <typename T>
T Iterator<T>::get() const {
return position->data;
}
template <typename T>
void Iterator<T>::next() {
position = position->next;
}
template <typename T>
void Iterator<T>::previous() {
if (position == nullptr) {
position = container->tail;
} else {
position = position->previous;
}
}
template <typename T>
bool Iterator<T>::equals(Iterator<T> other) const {
return position == other.position;
}
OUTPUT:
NOTE: In case of any query, you can mention it in the comment section. HAPPY LEARNING!!