Question
Finish implementing the List class (in list.h): Implement a following member functions: removeAt : Remove the node at the specified index. Return the int value
Finish implementing the List class (in list.h):
Implement a following member functions: removeAt : Remove the node at the specified index. Return the int value contained at the now removed node. Exit program if an invalid index is provided. remove : Remove the provided value if it is contained in the list. Return true if the value was found and remove, return false if no changes were made to the list. at : Returns the int value contained at the node at the provided index. Exit program if an invalid index is provided. valueOf : Given a ListNode pointer, return the given value getNext : Given a ListNode pointer, return a pointer to its next ListNode getPrevious : Given a ListNode pointer, return a pointer to its previous ListNode In the list.h file, do not modify any of the contents of the List class unless they are marked with a TODO comment. List.h:
class ListNode { private: int data; ListNode* prev; ListNode* next; public: ListNode() { prev = next = NULL; } ListNode(int d, ListNode* p, ListNode* n) { data = d; prev = p; next = n; } friend class List; }; class List { private: ListNode* head; ListNode* tail; public: List() { head = tail = NULL; } ~List(); bool isEmpty() { return head == NULL; } bool contains(int value); void addToHead(int value); void addToTail(int value); int removeHead(); int removeTail(); int removeAt(int index); bool remove(int value); int at(int index); int valueOf(const ListNode* elem); const ListNode* getNext(const ListNode* node); const ListNode* getPrevious(const ListNode* node); const ListNode* getHead() { return head; } const ListNode* getTail() { return tail; } }; List::~List() { while (!isEmpty()) removeTail(); } bool List::contains(int value) { ListNode *temp = head; while (temp != NULL && temp->data != value) temp = temp->next; return temp != NULL; } void List::addToHead(int value) { if (isEmpty()) { head = tail = new ListNode(value, NULL, NULL); } else { head = new ListNode(value, NULL, head); head->next->prev = head; } } void List::addToTail(int value) { if (isEmpty()) { head = tail = new ListNode(value, NULL, NULL); } else { tail = new ListNode(value, tail, NULL); tail->prev->next = tail; } } int List::removeHead() { int value = head->data; if (head == tail) { delete tail; head = tail = NULL; } else { head = head->next; delete head->prev; head->prev = NULL; } return value; } int List::removeTail() { int value = head->data; if (head == tail) { delete tail; head = tail = NULL; } else { tail = tail->prev; delete tail->next; tail->next = NULL; } return value; } int List::removeAt(int index) { // TODO: implement removeAt } bool List::remove(int value) { // TODO: implement remove } int List::at(int index) { // TODO: implement at } int valueOf(const ListNode* elem) { // TODO: implement valueOf } const ListNode* List::getNext(const ListNode* node) { // TODO: imoplement getNext } const ListNode* List::getPrevious(const ListNode* node) { // TODO: implement getPrevious }
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