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

[C++] Need help with doubly linked list program ... My copy constructor, operato

ID: 3573216 • Letter: #

Question

[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 << "Calling operator= ... " << endl;

/*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 << "List created " << endl;
}
else {
addBack->next = NULL;
tail->next = addBack;
addBack->prev = tail;
tail = addBack;
//cout << "Node inserted at end of list " << endl;
}
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 << "List created " << endl;
}
else {
addFront->prev = NULL;
head->prev = addFront;
addFront->next = head;
head = addFront;
//cout << "Node inserted at beginning of list " << endl;
}
count++;
}

template
dll::dll(unsigned int n)
{
for (int i = 0; i <= n-1; 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 << current->data << " ";
current = current->next;
}
cout << " Elements: " << count << endl;
cout << " " << endl;
}

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

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

cout << "node1: ";
node1.print();

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

cout << "node1: ";
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: ";
node2.print();

node = node2; // Not working!!!

cout << "node1: ";
node.print();

cout << "node1 reversed: ";
node.rprint();


return 0;
}

Explanation / Answer

#include<iostream>
using namespace std;

/* Linked list structure */
struct list {
   struct list *prev;
   int data;
   struct list *next;
} *node = NULL, *first = NULL, *last = NULL, *node1 = NULL, *node2 = NULL;

class linkedlist {
   public:

       /* Function for create/insert node at the beginning of Linked list */
       void push_front() {
           list *addBeg = new list;
           cout << "Enter value for the node:" << endl;
           cin >> addBeg->data;
           if(first == NULL) {
               addBeg->prev = NULL;
               addBeg->next = NULL;
               first = addBeg;
               last = addBeg;
               cout << "Linked list Created!" << endl;
           }
           else {
               addBeg->prev = NULL;
               first->prev = addBeg;
               addBeg->next = first;
               first = addBeg;
               cout << "Data Inserted at the beginning of the Linked list!" << endl;
           }
       }

       /* Function for create/insert node at the end of Linked list */
       void push_back() {
           list *addEnd = new list;
           cout << "Enter value for the node:" << endl;
           cin >> addEnd->data;
           if(first == NULL) {
               addEnd->prev = NULL;
               addEnd->next = NULL;
               first = addEnd;
               last = addEnd;
               cout << "Linked list Created!" << endl;
           }
           else {
               addEnd->next = NULL;
               last->next = addEnd;
               addEnd->prev = last;
               last = addEnd;
               cout << "Data Inserted at the end of the Linked list!" << endl;
           }
       }

       /* Function for Display Linked list */
       void display() {
           node = first;
           cout << "List of data in Linked list in Ascending order!" << endl;
           while(node != NULL) {
               cout << node->data << endl;
               node = node->next;
           }
           node = last;
           cout << "List of data in Linked list in Descending order!" << endl;
           while(node != NULL) {
               cout << node->data << endl;
               node = node->prev;
           }
       }
  
       /* Function for delete node from Linked list */
       void pop() {
           int count = 0, number, i;
           node = node1 = node2 = first;
           for(node = first; node != NULL; node = node->next)
               cout << "Enter value for the node:" << endl;
           count++;
           display();
           cout << count << " nodes available here!" << endl;
           cout << "Enter the node number which you want to delete:" << endl;
           cin >> number;
           if(number != 1) {
               if(number < count && number > 0) {
                   for(i = 2; i <= number; i++)
                       node = node->next;
                   for(i = 2; i <= number-1; i++)
                       node1 = node1->next;
                   for(i = 2; i <= number+1; i++)
                       node2 = node2->next;
                   node2->prev = node1;
                   node1->next = node2;
                   node->prev = NULL;
                   node->next = NULL;
                   node = NULL;
               }
               else if(number == count) {
                   node = last;
                   last = node->prev;
                   last->next = NULL;
                   node = NULL;
               }
               else
                   cout << "Invalid node number!" << endl;
           }
           else {
               node = first;
               first = node->next;
               first->prev = NULL;
               node = NULL;
           }
           cout << "Node has been deleted successfully!" << endl;
       }
  
};

int main() {
   int op = 0;
   linkedlist llist = linkedlist();
   while(op != 4) {
       cout << "1. Insert at the beginning 2. Insert at the end 3. Delete 4. Display 5. Exit" << endl;
       cout << "Enter your choice:" << endl;
       cin >> op;
       switch(op) {
           case 1:
               llist.push_front();
               break;
           case 2:
               llist.push_back();
               break;
           case 3:
               llist.pop();
               break;
           case 4:
               llist.display();
               break;
           case 5:
               cout << "Bye Bye!" << endl;
               return 0;
               break;
           default:
               cout << "Invalid choice!" << endl;
       }
   }
   return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote