Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In this assignment, you will complete the implementation of a class of single linked list. The LinkedList class is defined in the linkedlist.h header file.

In this assignment, you will complete the implementation of a class of single linked list. The LinkedList class is defined in the "linkedlist.h" header file. Don't change the interface in the header except for adding comments, as the test programs use the LinkedList interface. Complete the implementation of the data structrure in the "linkedlist.cpp" file, and add sufficient comments. You must complete the TODOs in "linkedlist.h" and "linkedlist.cpp". Delete "TODO" after you are done.

#include #include"linkedlist.h"

using namespace std;

/** * @brief Destructor to destroy all nodes and release memory */ LinkedList::~LinkedList() { //TODO: Add code here. Make sure memory is released properly. }

/** * @brief Purpose: Checks if the list is empty * @return true if the list is empty, false otherwise */ bool LinkedList::isEmpty() const { // TODO: Add code here return count == 0; }

/** * @brief Get the number of nodes in the list * @return int The number of nodes in the list */ int LinkedList::length() const{ //TODO: Add code here }

/** * @brief Convert the list to a string * */ string LinkedList::toString() { string str = "["; Node *ptr = front; if (ptr != nullptr) { // Head node is not preceded by separator str += to_string(ptr->val); ptr = ptr->next; } while (ptr != nullptr) { str += ", " + to_string(ptr->val); ptr = ptr->next; } str += "]"; return str; }

/** * @brief Displays the contents of the list */ void LinkedList::displayAll() { cout << toString() << endl; }

//TODO: Add comments void LinkedList::addRear(T val) { // TODO: Add code here // consider the two cases of whether the list was empty }

//TODO: Add comments void LinkedList::addFront(T val) { // TODO: Add code here // consider the two cases of whether the list was empty }

//TODO: Add comments bool LinkedList::deleteFront(T &OldNum) { // TODO: Add code here // consider if the list was empty and return false if the list is empty // consider the special case of deleting the only node in the list }

//TODO: Add comments bool LinkedList::deleteRear(T &OldNum) { // TODO: Add code here // consider if the list was empty and return false if the list is empty // consider the special case of deleting the only node in the list }

/* --- harder ones for test 2 and 3 -- */

/** * @brief Delete a node at a given position from the list. The * node at position pos is deleted and the value of the deleted node is returned in val. * The valid range of pos is 1 to count. pos = 1 is the first node, and pos = count is the last node. * @param pos: position of the node to be deleted * @param val: it is set to the value of the node to be deleted * @return true: if the node was deleted successfully * @return false: if the node was not deleted successfully because the position was out of range */ bool LinkedList::deleteAt(int pos, T &val) { //TODO: Add code here // check if the pos is valid first, then move the ptr to the rigth positon // consider the special case of deleting the first node and the last node // Do not forget to set value. }

/** * @brief Insert a value at a specified position in the list. The valid pos is in the range of 1 to count+1. * The value will be inserted before the node at the specified position. if pos = 1, the value will be inserted * at the front of the list. if pos = count+1, the value will be inserted at the rear of the list. * @param pos: position to insert the value at. * @param val: value to insert. * @return true: if the value was inserted. * @return false: if the value was not inserted because pos is out of the range. */ bool LinkedList::insertAt(int pos, T val) { //TODO: Add code here // check if the pos is valid first, then move the ptr to the rigth positon // consider the special case of inserting the first node and the last node }

/** * @brief Copy Constructor to allow pass by value and return by value of a LinkedList * @param other LinkedList to be copied */ LinkedList::LinkedList(const LinkedList &other) { // Start with an empty list front = nullptr; rear = nullptr; count = 0; // TODO: Add code here. Interate through the other list and add a new node to this list // for each node in the other list. The new node should have the same value as the other node.

}

/** * @brief Overloading of = (returns a reference to a LinkedList) * @param other LinkedList to be copied * @return reference to a LinkedList */ LinkedList &LinkedList::operator=(const LinkedList &other) { if(this != &other) { // check if the same object // Delete all nodes in this list // TODO: Add code here // Interate through the other list and add a new node to this list // Be sure to set the front and rear pointers to the correct values // Be sure to set the count to the correct value // TODO: Add code here } return *this; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Pro PowerShell For Database Developers

Authors: Bryan P Cafferky

1st Edition

1484205413, 9781484205419

More Books

Students also viewed these Databases questions