Question: Main.cpp ********************************************************** #include DoublyList.h #include int main() { DoublyList intList; intList.insertInOrder(8); intList.insertInOrder(5); intList.insertInOrder(37); intList.insertInOrder(53); intList.insertInOrder(21); intList.insertInOrder(18); intList.insertInOrder(73); intList.insertInOrder(49); std::cout std::cout std::cout std::cout std::cout std::cout *********************************************************

 Main.cpp ********************************************************** #include "DoublyList.h" #include int main() { DoublyList intList; intList.insertInOrder(8);

Main.cpp

**********************************************************

#include "DoublyList.h"

#include

int main() { DoublyList intList;

intList.insertInOrder(8); intList.insertInOrder(5); intList.insertInOrder(37); intList.insertInOrder(53); intList.insertInOrder(21); intList.insertInOrder(18); intList.insertInOrder(73); intList.insertInOrder(49);

std::cout

std::cout

std::cout

std::cout

std::cout

std::cout

*********************************************************

DoublyList.cpp

*****************************************************************************

#include "DoublyList.h"

using namespace std;

void DoublyList::insertFront(int newData) { if (first == nullptr) { first = new Node(newData, nullptr, nullptr); last = first; // Common error: Forgetting to re-set pointer last. } else { first = new Node(newData, nullptr, first); first->getNext()->setPrev(first); // Common error: Forgetting to connect pointer // prev of what is now the second node to the // new first node. }

++count; }

int DoublyList::front() const { return first->getData(); }

int DoublyList::back() const { return last->getData(); }

void DoublyList::printForward() const { Node* current = first; while (current != nullptr) { cout getData() getNext(); } }

void DoublyList::printReverse() const { Node* current = last; while (current != nullptr) { cout getData() getPrev(); } }

void DoublyList::clearList() { Node* temp = first;

while (first != nullptr) { first = first->getNext(); delete temp; temp = first; }

last = nullptr; // Don't forget to reset the last pointer to nullptr. count = 0; }

DoublyList::~DoublyList() { clearList(); }

*******************************************************

DoublyList.h

*************************************************************

#ifndef DOUBLYLIST_H #define DOUBLYLIST_H

#include #include

class Node { public: Node() : data(0), prev(nullptr), next(nullptr) {} Node(int theData, Node* prevLink, Node* nextLink) : data(theData), prev(prevLink), next(nextLink) {} int getData() const { return data; } Node* getPrev() const { return prev; } Node* getNext() const { return next; } void setData(int theData) { data = theData; } void setPrev(Node* prevLink) { prev = prevLink; } void setNext(Node* nextLink) { next = nextLink; } ~Node(){} private: int data; // To simplify, we are using only one piece of data. Node* prev; Node* next; };

class DoublyList { public: DoublyList() : first(nullptr), last(nullptr), count(0) {}

void insertFront(int newData);

int front() const; int back() const;

void printForward() const; void printReverse() const; void clearList(); ~DoublyList();

//************************************************

// Declaration function insertInOrder

private: Node *first; // Pointer to the first node on the list. Node *last; // Pointer to the last node on the list. int count; // Number of nodes in the list. };

#endif

***********************************************************************************

Functions.cpp

*********************************************************************************************

#include "DoublyList.h"

using namespace std;

// Definition function insertInOrder

DoublyList.h DoublyList.cpp D Functions.cpp Main.cpp Implement the function insertinOrder as a member of the DoublyList class by writing the declaration in the DoublyList.h file and the definition in the Functions.cpp file. Parameter: An int storing the value to insert in the list. The function inserts the item in ascending order. The calling list is either empty or, if it contains elements, they are already in order, which means that you need to find the correct position where to insert. Example: o Function call: obj.insertInOrder(7); If the list is empty, the element to insert, which in this case is 7, will be stored in a new node that will become the first and only node in the list. If the list has one or more nodes, the element to insert will be stored in a new node that needs to be positioned in the correct order: List is: 8 => Resulting list: 7 8 List is: 1 3 4 8 9 => Resulting list: 1 3 4 7 8 9 The file Main.cpp contains a few test cases

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!