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

Data Structures in C++, Doubly-Linked Lists Hello there, the original prompt for

ID: 3864012 • Letter: D

Question

Data Structures in C++, Doubly-Linked Lists

Hello there, the original prompt for this program is to implement the incompleted header file containing doubly-linked list class

and a source file which contains a main function that tests the implementation of the list class.

I have already finished and turned in the program but my instructor commented on it to fix the following errors.

/*

1. Your insert is not updating previous->next->prev to point to the
new node. Double check your methods to make sure that they are
updating the prev pointers when the list structure is modified.

2. Not sure what your operator+ is doing, but it should concatenate
the lists (return a new list consisting of all the elements of
a, followed by all the elements of b).

Please fix the above and resubmit.

*/

----------------------------------------------------------

And this is my codes for those two functions.

void dlist::insert(node *previous, int value){

node*n = new node{value, previous -> next};
previous -> next = n;

}

void dlist::del(node* which){

node*c= which -> next;
which -> next = which-> next-> next;
delete c;

}

dlist operator+ (dlist& a, dlist& b){
dlist::node* ca = a.head();
dlist::node* cb = b.head();
while(ca&&cb){
int result;
ca = ca -> next;
cb = cb -> next;
result= ca = cb;
}
return result;
}

-------------------------------------------

And this is the prompt for the the functions insert() and operator+ ( What the functions are supposed to do ):

void insert(node *previous, int value);

// Insert a new value, after an existing one

dlist operator+ (dlist& a, dlist& b);

/* reverse(l)
Returns a new list that is the *reversal* of l; that is, a new list
containing the same elements as l but in the reverse order.
*/

------------------------------------------

I am attaching a part of my header file just for your convenience :

#pragma once
/*
dlist.h
Doubly-linked lists of ints
*/
#include <ostream>

class dlist {
public:
dlist() { }

~dlist();

struct node {
int value;
node* next;
node* prev;
};

node* head() const { return _head; }
node* tail() const { return _tail; }

void insert(node *previous, int value);

private:
node* _head = nullptr;
node* _tail = nullptr;
};

dlist operator+ (dlist& a, dlist& b);

-------------------------------------------

I'll really appreciate it if anyone could fix the errors as instructed. You will be my life-saver!

Thank you in advance!!

Explanation / Answer

Here is the solution for you:

/*
1. Your insert is not updating previous->next->prev to point to the
new node. Double check your methods to make sure that they are
updating the prev pointers when the list structure is modified.
2. Not sure what your operator+ is doing, but it should concatenate
the lists (return a new list consisting of all the elements of
a, followed by all the elements of b).
Please fix the above and resubmit.
*/
----------------------------------------------------------
And this is my codes for those two functions.
/*void dlist::insert(node *previous, int value){
node*n = new node{value, previous -> next};
previous -> next = n;
}*/
void dlist::insert(node *previous, int value){
node *n = new node();   //Creates a new node.
n->value = value;       //Sets the specified value with nodes values.
n->next = previous->next;   //Assigns the current node's next to previous node's next.
n->previous = previous;       //Assigns the current node's previous to previous.
n->next->previous = n;       //Assigns the previous nodes next->previous to current node. (This is what your prof. is talking about.)
previous -> next = n;       //Assigns previous nodes next to current node.
}

/* I believe there is no comment on this method.
void dlist::del(node* which){
node*c= which -> next;
which -> next = which-> next-> next;
delete c;
}*/

/*dlist operator+ (dlist& a, dlist& b){
dlist::node* ca = a.head();
dlist::node* cb = b.head();
while(ca&&cb){
int result;
ca = ca -> next;
cb = cb -> next;
result= ca = cb;
}
return result;
}*/

dlist operator+ (dlist& a, dlist& b){
dlist::node* ca = a.head();
dlist::node* cb = b.head();
dlist twoLists;   //Creates a new list.
int index = 0;
while(ca)   //Till you reach the end of list a.
{
twoLists.push_back(ca->value);   //Pushes the value of the current node ca, to the end of newly creating list.
ca = ca -> next;       //Move on to the next node in list a.
}
while(cb)   //Till you reach the end of list b.
{
       twoLists.push_back(cb->value);   //Pushes the value of the current node cb, to the end of newly creating list.
       cb = cb -> next;       //Move on to the next node in list b.
}
return twoLists;   //The final list is now returned.
}

-------------------------------------------
And this is the prompt for the the functions insert() and operator+ ( What the functions are supposed to do ):

void insert(node *previous, int value);
// Insert a new value, after an existing one

dlist operator+ (dlist& a, dlist& b);
/* reverse(l)
Returns a new list that is the *reversal* of l; that is, a new list
containing the same elements as l but in the reverse order.
*/
------------------------------------------
I am attaching a part of my header file just for your convenience :
#pragma once
/*
dlist.h
Doubly-linked lists of ints
*/
#include <ostream>
class dlist {
public:
dlist() { }
~dlist();
struct node {
int value;
node* next;
node* prev;
};
node* head() const { return _head; }
node* tail() const { return _tail; }
void insert(node *previous, int value);
private:
node* _head = nullptr;
node* _tail = nullptr;
};
dlist operator+ (dlist& a, dlist& b);