Question: Implement a LinkedList class that uses a doubly-linked list as the underlying data structure. That means each node will have both a previous and a

Implement a LinkedList class that uses a doubly-linked list as the underlying data structure. That means each node will have both a previous and a next pointer along with the data. This will be a class template similar to the LinkedList code I posted on below except we will include fields int length; and Node * tailPtr; along with the original Node * headPtr.

For now just implement the same functions as in the posted below along with corresponding insertLast, removeLast and getLast methods. Also provide a getLength() method.

#ifndef LINKED_LIST_H

#define LINKED_LIST_H

#include "pch.h"

template

class Node {

public:

T data;

Node * next;

Node(const T & d = T(), Node *n = nullptr) {

data = d;

next = n;

}

};

template

class LinkedList {

friend ostream & out

operator<<(ostream & out, const LinkedList & other)

{

}

private:

Node * headPtr;

// int length;

public:

LinkedList () {

headPtr = nullptr;

}

LinkedList(const LinkedList & other) {

// to do

}

~LinkedList() {

// to do

}

void insertFirst(const T & d) {

// headPtr = new Node(d, headPtr);

Node * temp = new Node(d);

temp->next = headPtr;

headPtr = temp;

}

void removeFirst() {

if (headPtr == nullptr) return;

Node * temp = headPtr;

headPtr = headPtr->next;

delete temp;

}

T getFirst() const {

if (headPtr == nullptr)

throw logic_error("Can't get first of empty list");

return headPtr->data;

}

T getLast() const {

}

void clear() {

}

};

#endif

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!