Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need help writing the blank functions in this Double Linked List Header file.

ID: 3832944 • Letter: I

Question

I need help writing the blank functions in this Double Linked List Header file. The insertAhead and the insertBehind must contain a TRY CATCH

I have added the Node Header/Implementation at the bottom

//#ifdef _DOUBLE_LINKED_LIST
//#define _DOUBLE_LINKED_LIST
//#include "Node.h"

template
class DoubleLinkedList
{
public:
   DoubleLinkedList();
   //Initialized pointers to nullptr and the size to 0
   ~DoubleLinkedList();
   //Deallocates all nodes

   bool isEmpty() const;
   //Returns true if m_size is zero, false otherwise
   //Does not alter the list (hence it's const)
   int size() const;
   //returns the size of the list
   //NOTE: Unlike Java, C++ does not allow variables and methods to have the same name. Hence, the m_ convention for the member variables.
   //Does not alter the list (hence it's const)
   void pushFront(T value);
   //Puts the value of some type, T, in a node
   //Puts that node at the front of the list
   //Increase m_size by 1

   void pushBack(T value);
   //Puts the value of some type, T, in a node
   //Puts that node at the end of the list
   //Increase m_size by 1
   bool removeBack();
   //One element is removed from the back of the list.
   //Returns true if the back element was removed, false if the list is empty.
   bool removeFront();
   //One element is removed from the front of the list.
   //returns True if the front element was removed, false if the list is empty.
   bool remove(T value);
   //deletes the node containing the passed value
   //This method is in charge of deleting a node to prevent memory leaks. Any memory leaks will result in loss of points
   //Ensure that m_front is set to the next node in the list. If the list has exactly one node, delete it and set the front and back pointers to nullptr
   //returns true if the node was removed, false otherwise
   void insertAhead(T listValue, T newValue) throw (std::runtime_error);
   //Assumes listValue is in the list a node is created, newValue is placed in it, and the node placed ahead of the first occurence of listValue. Also, size increased by 1.
   //Example given list of ints 5, 8, 3, 9 and a new value, 11 is inserted ahead of 8 the new list will be: 5, 11, 8, 3, 9
   //throws std::exception if listValue isn't in the list
   void insertBehind(T listValue, T newValue) throw (std::runtime_error);
   //Assumes listValue is in the list a node is created, newValue is placed in it, and the node placed ahead of the first occurrence of listValue. Also, size increased by 1.
   //Example given list of ints 5, 8, 3, 9 and a new value, 11 is inserted behind 8 the new list will be: 5, 8, 11, 3, 9
   //throws std::exception if listValue isn't in the list
   Node* find(T value) const;
   //Assumes T is comparable with ==
   //returns a pointer to the first node in the list (from front to back) that contains the value or nullptr if not found
   void printList() const;
   //Assumes The type T is overloaded to be printable via the << operator
   //If the list is empty, prints ""
   //If the list is not empty, then print its content separated by spaces
   //NOTE: this method is used to test other parts of your list. Make sure you pass the print tests before moving on!
   void printListReverse() const;

private:
   Node* m_front;
   //A pointer that is that is always looking at the front (first) node
   Node* m_back;
   //A pointer that is that is always looking at the back (last) node
   int m_size;
   //The current size of the list. (e.g., if there are three nodes in the list the size is 3. If there are no nodes in the list, the size is zero)
};   // end class

template
DoubleLinkedList::DoubleLinkedList()
//Initialized pointers to nullptr and the size to 0
{
   m_front= nullptr;
   m_back = nullptr;
   m_size = 0;
}

template
DoubleLinkedList::~DoubleLinkedList()
//Deallocates all nodes
{
}

template
bool DoubleLinkedList::isEmpty() const
//Returns true if m_size is zero, false otherwise
//Does not alter the list (hence it's const)
{
   return m_size == 0;
}

template
int DoubleLinkedList::size() const
//returns the size of the list
//NOTE: Unlike Java, C++ does not allow variables and methods to have the same name. Hence, the m_ convention for the member variables.
//Does not alter the list (hence it's const)
{
   return m_size;
}

template
void DoubleLinkedList::pushFront(T value)
//Puts the value of some type, T, in a node
//Puts that node at the front of the list
//Increase m_size by 1
{
   Node * p = new Node;
   p->setValue(value);
   p->setNext(nullptr);
   p->setPrevious(nullptr);
   if ( isEmpty() )
   {
       m_front = m_back = p;
       m_size++;
       p = nullptr;
       return;
   }
   // at least one node
   p->setNext(m_front);
   m_front->setPrevious(p);
   m_front = p;
   m_size++;
   p = nullptr;
   return;
}

template
void DoubleLinkedList::pushBack(T value)
//Puts the value of some type, T, in a node
//Puts that node at the end of the list
//Increase m_size by 1
{
   Node * p = new Node;
   p->setValue(value);
   p->setNext(nullptr);
   p->setPrevious(nullptr);
   if (isEmpty())
   {
       m_front = m_back = p;
       m_size++;
       p = nullptr;
       return;
   }
  
   p->setPrevious(m_back);
   m_back->setNext(p);
   m_back = p;
   m_size++;
   p = nullptr;
   return;
}

template
bool DoubleLinkedList::removeBack()
//One element is removed from the back of the list.
//Returns true if the back element was removed, false if the list is empty.
{
return false; //dummy statement
}

template
bool DoubleLinkedList::removeFront()
//One element is removed from the front of the list.
//returns True if the front element was removed, false if the list is empty.
{
   return false; // dummy statement
}

template
bool DoubleLinkedList::remove(T value)
//deletes the node containing the passed value
//This method is in charge of deleting a node to prevent memory leaks. Any memory leaks will result in loss of points
//Ensure that m_front is set to the next node in the list. If the list has exactly one node, delete it and set the front and back pointers to nullptr
//returns true if the node was removed, false otherwise
{
   return false; // dummy statement
}

template
void DoubleLinkedList::insertAhead(T listValue, T newValue) throw (std::runtime_error)
//Assumes listValue is in the list a node is created, newValue is placed in it, and the node placed ahead of the first occurence of listValue. Also, size increased by 1.
//Example given list of ints 5, 8, 3, 9 and a new value, 11 is inserted ahead of 8 the new list will be: 5, 11, 8, 3, 9
//throws std::exception if listValue isn't in the list
{
}

template
void DoubleLinkedList::insertBehind(T listValue, T newValue) throw (std::runtime_error)
//Assumes listValue is in the list a node is created, newValue is placed in it, and the node placed ahead of the first occurrence of listValue. Also, size increased by 1.
//Example given list of ints 5, 8, 3, 9 and a new value, 11 is inserted behind 8 the new list will be: 5, 8, 11, 3, 9
//throws std::exception if listValue isn't in the list
{
}

template
Node* DoubleLinkedList::find(T value) const
//Assumes T is comparable with ==
//returns a pointer to the first node in the list (from front to back) that contains the value or nullptr if not found
{
   return nullptr; // dummy statement
}

template
void DoubleLinkedList::printList() const
//Assumes The type T is overloaded to be printable via the << operator
//If the list is empty, prints ""
//If the list is not empty, then print its content separated by spaces
//NOTE: this method is used to test other parts of your list. Make sure you pass the print tests before moving on!
{
   if (isEmpty())
   {
       cout << "";
       return;
   }
   const Node * walker = m_front;
   while (walker != nullptr)
   {
       cout << walker->getValue();
       if (walker->getNext() != nullptr)
           cout << " ";
       walker = walker->getNext();
   } // end while
   cout << endl;

   return;
}

template
void DoubleLinkedList::printListReverse() const
//Assumes The type T is overloaded to be printable via the << operator
//If the list is empty, prints ""
//If the list is not empty, then print its content separated by spaces
//NOTE: this method is used to test other parts of your list. Make sure you pass the print tests before moving on!
{
   if (isEmpty())
   {
       cout << "";
       return;
   }
   const Node * walker = m_back;
   while (walker != nullptr)
   {
       cout << walker->getValue();
       if (walker->getNext() != nullptr)
           cout << " ";
       walker = walker->getPrevious();
   } // end while
   cout << endl;

   return;
}

//#endif

//Node Implementation

#ifndef _NODE
#define _NODE
#include <iostream>
#include <string>
#include <stdexcept>
using namespace::std;
using namespace::std;

template<class T>
class Node
{

public:
Node();
//initialize next and previous to nullptr
//initialize the m_value to T()
// Assumes that the type T has a parmeterless constructor
//m_value = T();
T getValue() const;
Node<T> * getNext() const;
Node<T> * getPrevious() const;
void setValue(T parm);
void setNext(Node<T> * parm);
void setPrevious(Node<T> * parm);

private:
   T value;
   // Value in the node
   Node<T> * previous;
   // pointer to previous node, use nullptr if none
   Node<T> * next;
   // pointer to next node, use nullptr if none
};

template<class T>
Node<T>::Node()
{
   value = T();
   previous = nullptr;
   next = nullptr;
}

template<class T>
T Node<T>::getValue() const
{
   return value;
}

template<class T>
Node<T> * Node<T>::getNext() const
{
   return next;
}

template<class T>
Node<T> * Node<T>::getPrevious() const
{
   return previous;
}

template<class T>
void Node<T>::setValue(T parm)
{
   value = parm;
}

template<class T>
void Node<T>::setNext(Node<T> * parm)
{
   next = parm;
}
template<class T>
void Node<T>::setPrevious(Node<T> * parm)
{
   previous = parm;
}

#include "DoubleLinkedList.h"
#endif

Explanation / Answer


DoubleLinkedList.h


#include "Node.h"
#include <iostream>
#include <string>
#include <cstdlib>
#ifndef DOUBLELINKEDLIST_H
#define DOUBLELINKEDLIST_H

template<typename T>
class DoubleLinkedList
{
    public:
//        void create_list(int value);
    Node<T>* find(T value) const;
    int size() const;


    void pushFront(T value);


    void pushBack(T value);


    bool remove(T value);


    bool removeFront();


    bool removeBack();


    void search_element(int value);


    void insertAhead(T listValue, T newValue) throw (std::runtime_error); //throw (std::runtime_error)


    void insertBehind(T listValue, T newValue) throw (std::runtime_error); //throw (std::runtime_error)


    void printList();


    bool isEmpty() const;
       //returns true if the list is empty, false otherwise.
    DoubleLinkedList();
    ~DoubleLinkedList();


private:
       Node<T>* m_front;
       Node<T>* m_back;
       int m_size;
};


#include "DoubleLinkedList.hpp"
#endif

DoubleLinkedList.hpp


#include "DoubleLinkedList.h"
#ifndef DOUBLELINKEDLIST_HPP
#define DOUBLELINKEDLIST_HPP

template<typename T>
DoubleLinkedList<T>::DoubleLinkedList()
{
    m_size = 0;
    m_front = nullptr;
    m_back = nullptr;
}

template<typename T>
DoubleLinkedList<T>::~DoubleLinkedList()
{
while(size() != 0)
   {
       removeFront();
   }
}

template<typename T>
bool DoubleLinkedList<T>::isEmpty() const
{
    if(m_front != nullptr)
   {
       return false;
   }
   else{
       return true;
   }
}


template<typename T>
void DoubleLinkedList<T>::pushFront(T value)
{
   Node<T>* tempFront;
tempFront = (Node<T>*)malloc(sizeof(Node<T>));
if(m_front != nullptr)
   {
   tempFront->setValue(value);
   tempFront->setNext(m_front);
   m_front = tempFront;
   m_back = tempFront;
   }
   else
   {
tempFront->setValue(value);
tempFront->setNext(m_front);
m_front = tempFront;
   }
    std::cout<<"Value Inserted"<<std::endl;

    m_size++;
}

/*
* Insertion of element at a particular position
*/

template<typename T>
void DoubleLinkedList<T>::pushBack(T value)
{
   Node<T>* tempBack;
    tempBack = (Node<T>*)malloc(sizeof(Node<T>));
    if (m_front != nullptr)
    {
       tempBack = m_front;
       while(tempBack->getNext() != nullptr)
       {
           tempBack = tempBack -> getNext();
       }
       Node<T>* tempBack2;
      tempBack2 = (Node<T>*)malloc(sizeof(Node<T>));
       tempBack2->setValue(value);
       tempBack2->setNext(nullptr);
       tempBack2->setPrevious(tempBack);
       tempBack->setNext(tempBack2);
       m_back = tempBack;
        return;
    }
   else{
       tempBack->setValue(value);
       tempBack->setNext(nullptr);
       tempBack->setPrevious(nullptr);
       m_front = tempBack;
   }
    std::cout<<"Value Inserted"<<std::endl;
}

/*
* Deletion of element from the list
*/

template<typename T>
bool DoubleLinkedList<T>::removeBack()
{
   if(m_front != nullptr)
   {
       Node<T>* tempRemove;
      tempRemove = (Node<T>*)malloc(sizeof(Node<T>));
       tempRemove = m_front;
       Node<T>* switcher;
      switcher = (Node<T>*)malloc(sizeof(Node<T>));
       while(tempRemove->getNext() != nullptr)
       {
           switcher = tempRemove;
           tempRemove = tempRemove->getNext();
       }
       m_back = tempRemove->getPrevious();
       if(tempRemove == m_front)
           m_front = nullptr;
       switcher->setNext(nullptr);
       delete tempRemove;
       return true;
   }
   else
       return false;
}

template<typename T>
bool DoubleLinkedList<T>::removeFront(){
   if(m_front != nullptr)
   {
       Node<T>* tempRemove;
      tempRemove = (Node<T>*)malloc(sizeof(Node<T>));
       tempRemove = m_front;
       tempRemove->setPrevious(nullptr);
       m_front = tempRemove->getNext();
       delete tempRemove;
       return true;
   }
   else
       return false;
}

template<typename T>
bool DoubleLinkedList<T>::remove(T value)
{
if(m_front != nullptr)
   {
   Node<T>* tempRemove;
    tempRemove = (Node<T>*)malloc(sizeof(Node<T>));
   Node<T>* tempRemove2;
    tempRemove2 = (Node<T>*)malloc(sizeof(Node<T>));
     /*first element deletion*/
    if (m_front->getValue() == value)
    {
       tempRemove = m_front;
       tempRemove->setPrevious(nullptr);
       m_front = tempRemove->getNext();
       delete tempRemove;
        std::cout<<"Value Deleted"<<std::endl;
        return true;
    }
    if(m_front->getNext() == nullptr)
    return false;
    tempRemove = m_front;
    while (tempRemove->getNext()->getNext() != NULL)
    {
        if (tempRemove->getNext()->getValue() == value)
        {
            tempRemove2 = tempRemove->getNext();
            tempRemove->setNext(tempRemove2->getNext());
            tempRemove2->getNext()->setPrevious(tempRemove);
            std::cout<<"Value Deleted"<<std::endl;
                     delete tempRemove2;
            return true;
        }
        tempRemove = tempRemove->getNext();
    }
     /*last element deleted*/
    if (tempRemove->getNext()->getValue() == value)
    {
       tempRemove2 = tempRemove->getNext();
        delete tempRemove2;
        tempRemove->setNext(nullptr);
        std::cout<<"Value Deleted"<<std::endl;
        return true;
    }
}
else
    std::cout<<"Value "<<value<<" not found"<<std::endl;
   return false;
}

template<typename T>
void DoubleLinkedList<T>::insertAhead(T listValue, T newValue) throw (std::runtime_error)
{
if(m_front != nullptr)
   {
Node<T>* tempAhead;
    tempAhead = (Node<T>*)malloc(sizeof(Node<T>));
    if (m_front->getValue() == listValue)
    {
      tempAhead->setValue(newValue);
      tempAhead->setNext(m_front);
      m_front = tempAhead;
      return;
    }
    if(m_front->getNext() == nullptr)
    {
      throw(std::runtime_error("Not in list"));
    return;
    }
    tempAhead = m_front;
    Node<T>* tempAhead2;
      tempAhead2 = (Node<T>*)malloc(sizeof(Node<T>));
    while (tempAhead->getNext()->getNext() != NULL)
    {
        if (tempAhead->getNext()->getValue() == listValue)
        {
            tempAhead2 -> setValue(newValue);
            tempAhead2 -> setNext(tempAhead->getNext());
            tempAhead2 ->setPrevious(tempAhead);
            tempAhead ->setNext(tempAhead2);
            return;
        }
        tempAhead = tempAhead->getNext();
    }
     /*last element deleted*/
    if (tempAhead->getNext()->getValue() == listValue)
    {
      tempAhead2->setValue(newValue);
      tempAhead2->setNext(tempAhead->getNext());
      tempAhead2->setPrevious(tempAhead);
      tempAhead->setNext(tempAhead2);
      m_back = tempAhead;
      return;
    }
}
throw(std::runtime_error("Not in list"));
    std::cout<<"Value "<< listValue<<" not found"<<std::endl;
}


template<typename T>
    int DoubleLinkedList<T>::size() const
{
    int size = 0;
    Node<T>* temp;
    temp = (Node<T>*)malloc(sizeof(Node<T>));
    temp = m_front;
    while(temp != nullptr)
    {
      size++;
      temp = temp->getNext();
    }
    delete temp;
    return size;
}

template<typename T>
Node<T>* DoubleLinkedList<T>::find(T value) const
{
if(m_front != nullptr)
   {
Node<T>* temp;
    temp = (Node<T>*)malloc(sizeof(Node<T>));
    if (m_front->getValue() == value)
    {
      return m_front;
    }
    if(m_front->getNext() == nullptr)
    return nullptr;
    temp = m_front;
    Node<T>* temp2;
      temp2 = (Node<T>*)malloc(sizeof(Node<T>));
    while (temp->getNext()->getNext() != NULL)
    {
        if (temp->getNext()->getValue() == value)
        {
            temp2 = temp->getNext();
            return temp2;
        }
        temp = temp->getNext();
    }
     /*last element deleted*/
     if (temp->getNext()->getValue() == value)
     {
         temp2 = temp->getNext();
         return temp2;
     }
}
return nullptr;
}


template<typename T>
void DoubleLinkedList<T>::insertBehind(T listValue, T newValue) throw (std::runtime_error)
{
if(m_front != nullptr)
{
Node<T>* tempBehind;
    tempBehind = (Node<T>*)malloc(sizeof(Node<T>));
    if (m_front->getValue() == listValue)
    {
      tempBehind->setValue(newValue);
      tempBehind->setPrevious(m_front);
      tempBehind->setNext(m_front->getNext());
      m_front -> setNext(tempBehind);
      return;
    }
    if(m_front->getNext() == nullptr)
    {
      throw(std::runtime_error("Not in list"));
    return;
    }
    tempBehind = m_front;
    Node<T>* tempBehind2;
      tempBehind2 = (Node<T>*)malloc(sizeof(Node<T>));
      while (tempBehind->getNext()->getNext() != NULL)
      {
          if (tempBehind->getNext()->getValue() == listValue)
          {
            tempBehind = tempBehind->getNext();
            tempBehind2 -> setValue(newValue);
            tempBehind2 -> setNext(tempBehind->getNext());
            tempBehind2 ->setPrevious(tempBehind);
            tempBehind ->setNext(tempBehind2);
              return;
          }
          tempBehind = tempBehind->getNext();
      }
      if (tempBehind->getNext()->getValue() == listValue)
      {
        tempBehind = tempBehind->getNext();
        tempBehind2->setValue(newValue);
        tempBehind2->setNext(tempBehind->getNext());
        tempBehind2->setPrevious(tempBehind);
        tempBehind->setNext(tempBehind2);
        m_back = tempBehind;
        return;
      }
}
std::cout<<"Value "<< listValue<<" not found"<<std::endl;
        throw(std::runtime_error("Not in list"));
}


/*
* Display elements of Doubly Link List
*/
template<typename T>
void DoubleLinkedList<T>::printList()
{
   Node<T>* tempPrint;
    tempPrint = (Node<T>*)malloc(sizeof(Node<T>));
    if (m_front != nullptr)
    {
        tempPrint = m_front;
        while(tempPrint != nullptr)
              {
                     std::cout << tempPrint->getValue() << " ";
                        tempPrint = tempPrint->getNext();
       }
    }
}
#endif

Node.h


#include "DoubleLinkedList.h"
#ifndef DOUBLELINKEDLIST_HPP
#define DOUBLELINKEDLIST_HPP

template<typename T>
DoubleLinkedList<T>::DoubleLinkedList()
{
    m_size = 0;
    m_front = nullptr;
    m_back = nullptr;
}

template<typename T>
DoubleLinkedList<T>::~DoubleLinkedList()
{
while(size() != 0)
   {
       removeFront();
   }
}

template<typename T>
bool DoubleLinkedList<T>::isEmpty() const
{
    if(m_front != nullptr)
   {
       return false;
   }
   else{
       return true;
   }
}


template<typename T>
void DoubleLinkedList<T>::pushFront(T value)
{
   Node<T>* tempFront;
tempFront = (Node<T>*)malloc(sizeof(Node<T>));
if(m_front != nullptr)
   {
   tempFront->setValue(value);
   tempFront->setNext(m_front);
   m_front = tempFront;
   m_back = tempFront;
   }
   else
   {
tempFront->setValue(value);
tempFront->setNext(m_front);
m_front = tempFront;
   }
    std::cout<<"Value Inserted"<<std::endl;

    m_size++;
}

/*
* Insertion of element at a particular position
*/

template<typename T>
void DoubleLinkedList<T>::pushBack(T value)
{
   Node<T>* tempBack;
    tempBack = (Node<T>*)malloc(sizeof(Node<T>));
    if (m_front != nullptr)
    {
       tempBack = m_front;
       while(tempBack->getNext() != nullptr)
       {
           tempBack = tempBack -> getNext();
       }
       Node<T>* tempBack2;
      tempBack2 = (Node<T>*)malloc(sizeof(Node<T>));
       tempBack2->setValue(value);
       tempBack2->setNext(nullptr);
       tempBack2->setPrevious(tempBack);
       tempBack->setNext(tempBack2);
       m_back = tempBack;
        return;
    }
   else{
       tempBack->setValue(value);
       tempBack->setNext(nullptr);
       tempBack->setPrevious(nullptr);
       m_front = tempBack;
   }
    std::cout<<"Value Inserted"<<std::endl;
}

/*
* Deletion of element from the list
*/

template<typename T>
bool DoubleLinkedList<T>::removeBack()
{
   if(m_front != nullptr)
   {
       Node<T>* tempRemove;
      tempRemove = (Node<T>*)malloc(sizeof(Node<T>));
       tempRemove = m_front;
       Node<T>* switcher;
      switcher = (Node<T>*)malloc(sizeof(Node<T>));
       while(tempRemove->getNext() != nullptr)
       {
           switcher = tempRemove;
           tempRemove = tempRemove->getNext();
       }
       m_back = tempRemove->getPrevious();
       if(tempRemove == m_front)
           m_front = nullptr;
       switcher->setNext(nullptr);
       delete tempRemove;
       return true;
   }
   else
       return false;
}

template<typename T>
bool DoubleLinkedList<T>::removeFront(){
   if(m_front != nullptr)
   {
       Node<T>* tempRemove;
      tempRemove = (Node<T>*)malloc(sizeof(Node<T>));
       tempRemove = m_front;
       tempRemove->setPrevious(nullptr);
       m_front = tempRemove->getNext();
       delete tempRemove;
       return true;
   }
   else
       return false;
}

template<typename T>
bool DoubleLinkedList<T>::remove(T value)
{
if(m_front != nullptr)
   {
   Node<T>* tempRemove;
    tempRemove = (Node<T>*)malloc(sizeof(Node<T>));
   Node<T>* tempRemove2;
    tempRemove2 = (Node<T>*)malloc(sizeof(Node<T>));
     /*first element deletion*/
    if (m_front->getValue() == value)
    {
       tempRemove = m_front;
       tempRemove->setPrevious(nullptr);
       m_front = tempRemove->getNext();
       delete tempRemove;
        std::cout<<"Value Deleted"<<std::endl;
        return true;
    }
    if(m_front->getNext() == nullptr)
    return false;
    tempRemove = m_front;
    while (tempRemove->getNext()->getNext() != NULL)
    {
        if (tempRemove->getNext()->getValue() == value)
        {
            tempRemove2 = tempRemove->getNext();
            tempRemove->setNext(tempRemove2->getNext());
            tempRemove2->getNext()->setPrevious(tempRemove);
            std::cout<<"Value Deleted"<<std::endl;
                     delete tempRemove2;
            return true;
        }
        tempRemove = tempRemove->getNext();
    }
     /*last element deleted*/
    if (tempRemove->getNext()->getValue() == value)
    {
       tempRemove2 = tempRemove->getNext();
        delete tempRemove2;
        tempRemove->setNext(nullptr);
        std::cout<<"Value Deleted"<<std::endl;
        return true;
    }
}
else
    std::cout<<"Value "<<value<<" not found"<<std::endl;
   return false;
}

template<typename T>
void DoubleLinkedList<T>::insertAhead(T listValue, T newValue) throw (std::runtime_error)
{
if(m_front != nullptr)
   {
Node<T>* tempAhead;
    tempAhead = (Node<T>*)malloc(sizeof(Node<T>));
    if (m_front->getValue() == listValue)
    {
      tempAhead->setValue(newValue);
      tempAhead->setNext(m_front);
      m_front = tempAhead;
      return;
    }
    if(m_front->getNext() == nullptr)
    {
      throw(std::runtime_error("Not in list"));
    return;
    }
    tempAhead = m_front;
    Node<T>* tempAhead2;
      tempAhead2 = (Node<T>*)malloc(sizeof(Node<T>));
    while (tempAhead->getNext()->getNext() != NULL)
    {
        if (tempAhead->getNext()->getValue() == listValue)
        {
            tempAhead2 -> setValue(newValue);
            tempAhead2 -> setNext(tempAhead->getNext());
            tempAhead2 ->setPrevious(tempAhead);
            tempAhead ->setNext(tempAhead2);
            return;
        }
        tempAhead = tempAhead->getNext();
    }
     /*last element deleted*/
    if (tempAhead->getNext()->getValue() == listValue)
    {
      tempAhead2->setValue(newValue);
      tempAhead2->setNext(tempAhead->getNext());
      tempAhead2->setPrevious(tempAhead);
      tempAhead->setNext(tempAhead2);
      m_back = tempAhead;
      return;
    }
}
throw(std::runtime_error("Not in list"));
    std::cout<<"Value "<< listValue<<" not found"<<std::endl;
}


template<typename T>
    int DoubleLinkedList<T>::size() const
{
    int size = 0;
    Node<T>* temp;
    temp = (Node<T>*)malloc(sizeof(Node<T>));
    temp = m_front;
    while(temp != nullptr)
    {
      size++;
      temp = temp->getNext();
    }
    delete temp;
    return size;
}

template<typename T>
Node<T>* DoubleLinkedList<T>::find(T value) const
{
if(m_front != nullptr)
   {
Node<T>* temp;
    temp = (Node<T>*)malloc(sizeof(Node<T>));
    if (m_front->getValue() == value)
    {
      return m_front;
    }
    if(m_front->getNext() == nullptr)
    return nullptr;
    temp = m_front;
    Node<T>* temp2;
      temp2 = (Node<T>*)malloc(sizeof(Node<T>));
    while (temp->getNext()->getNext() != NULL)
    {
        if (temp->getNext()->getValue() == value)
        {
            temp2 = temp->getNext();
            return temp2;
        }
        temp = temp->getNext();
    }
     /*last element deleted*/
     if (temp->getNext()->getValue() == value)
     {
         temp2 = temp->getNext();
         return temp2;
     }
}
return nullptr;
}


template<typename T>
void DoubleLinkedList<T>::insertBehind(T listValue, T newValue) throw (std::runtime_error)
{
if(m_front != nullptr)
{
Node<T>* tempBehind;
    tempBehind = (Node<T>*)malloc(sizeof(Node<T>));
    if (m_front->getValue() == listValue)
    {
      tempBehind->setValue(newValue);
      tempBehind->setPrevious(m_front);
      tempBehind->setNext(m_front->getNext());
      m_front -> setNext(tempBehind);
      return;
    }
    if(m_front->getNext() == nullptr)
    {
      throw(std::runtime_error("Not in list"));
    return;
    }
    tempBehind = m_front;
    Node<T>* tempBehind2;
      tempBehind2 = (Node<T>*)malloc(sizeof(Node<T>));
      while (tempBehind->getNext()->getNext() != NULL)
      {
          if (tempBehind->getNext()->getValue() == listValue)
          {
            tempBehind = tempBehind->getNext();
            tempBehind2 -> setValue(newValue);
            tempBehind2 -> setNext(tempBehind->getNext());
            tempBehind2 ->setPrevious(tempBehind);
            tempBehind ->setNext(tempBehind2);
              return;
          }
          tempBehind = tempBehind->getNext();
      }
      if (tempBehind->getNext()->getValue() == listValue)
      {
        tempBehind = tempBehind->getNext();
        tempBehind2->setValue(newValue);
        tempBehind2->setNext(tempBehind->getNext());
        tempBehind2->setPrevious(tempBehind);
        tempBehind->setNext(tempBehind2);
        m_back = tempBehind;
        return;
      }
}
std::cout<<"Value "<< listValue<<" not found"<<std::endl;
        throw(std::runtime_error("Not in list"));
}


/*
* Display elements of Doubly Link List
*/
template<typename T>
void DoubleLinkedList<T>::printList()
{
   Node<T>* tempPrint;
    tempPrint = (Node<T>*)malloc(sizeof(Node<T>));
    if (m_front != nullptr)
    {
        tempPrint = m_front;
        while(tempPrint != nullptr)
              {
                     std::cout << tempPrint->getValue() << " ";
                        tempPrint = tempPrint->getNext();
       }
    }
}
#endif

Node.cpp


#include "Node.h"
#ifndef NODE_HPP
#define NODE_HPP


template<typename T>
Node<T>::Node()
{
   m_value = T();
   m_next = nullptr; // Pointer to next node
   m_previous = nullptr;
} // end default constructor

/*
template<typename T>
Node<T>::Node(const T& anItem) : item(anItem), next(nullptr)
{

} // end constructor


template<typename T>
Node<T>::Node(const T& anItem, Node<T>* nextNodePtr) :
item(anItem), next(nextNodePtr) {}*/ // end constructor


template<typename T>
void Node<T>::setValue(T anItem)
{
   m_value = anItem;
} // end setItem


template<typename T>
void Node<T>::setNext(Node<T>* nextNodePtr)
{
   m_next = nextNodePtr;
} // end setNext


template<typename T>
T Node<T>::getValue() const {
return m_value;
} // end getvalue


template<typename T>
Node<T>* Node<T>::getNext() const {
return m_next;
} // end getNext


template<typename T>
Node<T>* Node<T>::getPrevious() const {
   return m_previous;
} // end getNext


template<typename T>
void Node<T>::setPrevious(Node<T>* previousNodePtr)
{
   m_previous = previousNodePtr;
} // end setNext


#endif

Node.h


#ifndef NODE_H
#define NODE_H


template<typename T>
class Node
{
public:
   Node();
   //   Node(const T& anItem);
   //   Node(const T& anItem, Node<T>* nextNodePtr);
   void setValue(T anItem);
   void setNext(Node<T>* nextNodePtr);
   void setPrevious(Node<T>* previousNodePtr);
   T getValue() const ;
   Node<T>* getNext() const ;
   Node<T>* getPrevious() const ;
private:
   T m_value; // A data item
   Node<T>* m_next; // Pointer to next node
   Node<T>* m_previous;

}; // end Node


#include "Node.hpp"
#endif