in c++
Suppose we implement a doubly linked list class templateLinkedList with template type T. LinkedList has fields Node *headPtr, Node *tailPtr and int length, where thestruct type Node has fields prev and next of type Node* along with data of type T. The prev and next pointers of each Node point to the previous and next Nodes in the list (or are respectively null in the case of the list’s head or tail node). headPtr and tailPtr should always point respectively to the first and last Nodes in the list (or be null if the list is empty) .
Implement a member function
void insertAtEnd(const T & d) that allocates a new Node with data equal to d and inserts it at the end of the linked list.
ONLY type the function code, not the whole class . Do not call any other member functions of the LinkedList class.
Linked list class:
#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 & operator<(ostream &="" out,="" const="" linkedlist="" &="">(ostream> { }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
class Node {
public:
T data;
Node * next; Node(const T & d = T(), Node *n = nullptr) { data = d; next = n; }};template class LinkedList { friend ostream & operator<(ostream &="" out,="" const="" linkedlist="" &="">(ostream> { }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
Node(const T & d = T(), Node *n = nullptr) { data = d; next = n; }};template class LinkedList { friend ostream & operator<(ostream &="" out,="" const="" linkedlist="" &="">(ostream> { }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
data = d;
next = n;
}
};
template class LinkedList { friend ostream & operator<(ostream &="" out,="" const="" linkedlist="" &="">(ostream> { }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
class LinkedList {
friend ostream &
operator<(ostream &="" out,="" const="" linkedlist="" &="">(ostream>
{
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
// int length;
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
// to do
~LinkedList() {
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
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
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
Already registered? Login
Not Account? Sign up
Enter your email address to reset your password
Back to Login? Click here