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
Node(const T & d = T(), Node
data = d;
next = n;
}
};
template
class LinkedList {
friend ostream & out
operator<<(ostream & out, const LinkedList & other)
{
}
private:
Node
// int length;
public:
LinkedList () {
headPtr = nullptr;
}
LinkedList(const LinkedList
// to do
}
~LinkedList() {
// to do
}
void insertFirst(const T & d) {
// headPtr = new Node
Node
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
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