Question: #include #include DoublyLinkedList.h //-------------------------------------------- // Function: DoublyLinkedList() // Purpose: Class constructor // Returns: void //-------------------------------------------- DoublyLinkedList::DoublyLinkedList() { head = NULL; tail = NULL; } //--------------------------------------------

 #include #include "DoublyLinkedList.h" //-------------------------------------------- // Function: DoublyLinkedList() // Purpose: Class constructor

#include #include "DoublyLinkedList.h"

//-------------------------------------------- // Function: DoublyLinkedList() // Purpose: Class constructor // Returns: void //--------------------------------------------

DoublyLinkedList::DoublyLinkedList() { head = NULL; tail = NULL; }

//-------------------------------------------- // Function: addToDLLTail(double) // Purpose: Add DLLNode with double value to tail of DLL // Returns: void //--------------------------------------------

void DoublyLinkedList::addToDLLTail(double el) { if (tail != NULL) { // If DLL is non-empty tail = new DLLNode(el,0,tail); // Allocate new DLLNode tail->prev->next = tail; // Link new node to tail of DLL } else { // If DLL is empty head = new DLLNode(el); // Allocate new DLLNode tail = head; // Node is first in the DLL, so it // is both head and tail } }

//-------------------------------------------- // Function: deleteFromDLLTail() // Purpose: Delete DLLNode from tail of DLL // Returns: void //--------------------------------------------

double DoublyLinkedList::deleteFromDLLTail() { double el = tail->info; // Get value of node to be deleted

if (tail == 0) throw("EMPTY"); // Return an error condition

else if (head == tail) { // If only DLLNode in the DLL delete head; // Deallocate DLLNode head = 0; // Set both head and tail to values for tail = 0; // a DLL with no nodes }

else { // If more than one DLLNode in DLL tail = tail->prev; // Move tail to previous DLLNode delete tail->next; // Deallocate DLLNode tail->next = 0; // Set next of new last DLLNode to 0 } return el; // Return value in deleted DLLNode }

#ifndef DOUBLY_LINKED_LIST_H #define DOUBLY_LINKED_LIST_H

#include using namespace std;

//-------------------------------------------------- // class: DLLNode // // purpose: To create a data element for a Doubly Linked List // //-------------------------------------------------- class DLLNode { public: DLLNode(); DLLNode(const double el, DLLNode *n = NULL, DLLNode *p = NULL) { info = el; // Place value into new node next = n; // Set link to next node in list prev = p; // Set link to previous node in list };

double info; DLLNode *next, *prev; };

class DoublyLinkedList { public: DoublyLinkedList(); // Class constructor void addToDLLTail(double el); // Add a DLLNode with value el to tail of DLL double deleteFromDLLTail(); // Remove a DLLNode from tail of DLL

private: DLLNode *head, *tail; };

#endif

Create and thoroughly test new methods named addToDLLHead(double el) and double deleteFromDLLHead() that insert and delete from a doubly-linked list at the head, similar to how the existing methods addToDLLTail(double el) and double deleteFromDLLTail() do at the DLL's tail

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!