Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

[C++] Need help with doubly linked list program ... My copy constructor, operator=, and explicit constructor are not working as I'd like them to. After

[C++] Need help with doubly linked list program ...

My copy constructor, operator=, and explicit constructor are not working as I'd like them to. After some debugging:

---upon creating a node in main, it is given a data value of 0; this creates problems when using combinations of push_front() and push_back() as 0 gets stuck in the middle of the list

---I am not totally sure how the explicit constructor is being used (it should initialize the list with node values, but it is not doing that)

---my assignment operator= is broken (when it did work, print() functioned correctly but rprint() showed the OLD data in reverse order)

---also, the Node struct is required to stay in the private section of the dll class

Thank you; here is my code (sorry about formatting, I am not sure how to fix that):

#include #include #include using namespace std;

typedef int T;

template class dll { private: struct Node{ Node* next; Node* prev; T data; }; Node* head; Node* tail; unsigned int count;

public:

dll(); // default dll(const dll& otherList); // copy constr ~dll(); // delete dynamically created nodes bool empty() const; void clear(); dll& operator=(const dll& l); void pop_back(); //remove back void pop_front(); //remove front void push_back(const T& x); //add back void push_front(const T& x); //add front

explicit dll(unsigned int n);// initialize list w/ n nodes, values from 0 to n-1 void print() const; //print void rprint() const; // reverse print };

template dll::dll() { head = new Node; tail = new Node; head->data = 0; head->prev = NULL; head->next = NULL; tail = head; count = 0; }

//Copy constructor??? template dll::dll(const dll& other){ head = NULL, tail = NULL, count = 0; if(other.head != NULL) { Node* current = other.head; do { Node* newNode = new Node(*current); const T& x = newNode->data; push_back(x); }while((current->next) != NULL); } }

template dll::~dll() { while(head) { Node *temp = head; head = head->next; delete temp; } }

template bool dll::empty() const { if (count == 0) return true; else return false; }

template void dll::clear() { if(!empty) { while(head != NULL) pop_front(); } }

template dll& dll::operator=(const dll& l) { cout

/*this->head = l.head; return *this;

//need to update count here */ dll temp(l); swap(temp.head, head); return *this; }

template void dll::pop_back() { Node *delBack = tail; Node *nodeToDelete = delBack; delBack = delBack->prev; delBack->next = NULL; delete nodeToDelete; tail = delBack; count--; }

template void dll::pop_front() { Node *delFront = head; Node *nodeToDelete = delFront; delFront = delFront->next; delFront->prev = NULL; delete nodeToDelete; head = delFront; count--; }

template void dll::push_back(const T& x) { Node *addBack = new Node; addBack->data = x; if(head == NULL) { addBack->prev = NULL; addBack->next = NULL; head = addBack; tail = addBack; cout } else { addBack->next = NULL; tail->next = addBack; addBack->prev = tail; tail = addBack; //cout } count++; }

template void dll::push_front(const T& x) { Node *addFront = new Node; addFront->data = x; if(head == NULL) { addFront->prev = NULL; addFront->next = NULL; head = addFront; tail = addFront; cout } else { addFront->prev = NULL; head->prev = addFront; addFront->next = head; head = addFront; //cout } count++; }

template dll::dll(unsigned int n) { for (int i = 0; i => { // Create the new node Node* node = new Node; node->data = i; node->prev = tail; node->next = NULL;

// Fold it into the list tail->next = node;

// Move the tail tail = node; count++; } }

template void dll::print() const { Node *current = head; while (current != NULL) { cout data current = current->next; } cout cout }

template void dll::rprint() const { Node *current = tail; while (current != NULL) { cout data current = current->prev; } cout cout }

int main() { dll node; // not sure how to use explicit constructor here

cout node1.print();

node1.push_front(3); node1.push_front(2); node1.push_front(1);

cout node1.print();

dll node2; node2.push_back(1); node2.push_back(2); node2.push_back(3); node2.push_back(4); node2.push_back(5);

cout node2.print();

node = node2; // Not working!!!

cout node.print();

cout node.rprint();

return 0; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Introduction to Wireless and Mobile Systems

Authors: Dharma P. Agrawal, Qing An Zeng

4th edition

1305087135, 978-1305087132, 9781305259621, 1305259629, 9781305537910 , 978-130508713

More Books

Students also viewed these Programming questions