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

Data Structure in C++ Doubly Linked Lists of ints This is my code below: dlist.c

ID: 3804459 • Letter: D

Question

Data Structure in C++

Doubly Linked Lists of ints

This is my code below: dlist.cc

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

#include <iostream>
#include "dlist.h"

typedef dlist::node node;

dlist::node* dlist::at(int n){
node* c = _head;
while(n > 0 && c){
c = c->next;
n--;
}
return c;
}

void dlist::insert(node* previous, int value){
node* n = new node{value, previous->next, previous};
previous->next = n;
n->next->prev = n;
}

void dlist::del(node* which){
node* nn = which->next;
which->next = nn->next;
delete nn;
}

void dlist::push_back(int value){
node* n = new node{value, _tail, _tail->prev};
_tail->prev->next = n;
_tail->prev-> n;
}

void dlist::push_front(int value){
node* n = new node(value, nullptr);
_head->prev = n;
_head-> n;
}

void dlist::pop_front(){
node* n = _head;
_head = _head->next;
delete n;
}

void dlist::pop_back(){
node* n = _tail;
_tail = _tail->prev;
delete n;
}

bool dlist::empty(){
return size == 0;
}

/* out << l
Prints a list to the ostream out. This is mostly for your convenience in
testing your code; it's much easier to figure out what's going on if you
can easily print out lists!
*/
std::ostream& operator<< (std::ostream& out, dlist& l){
node* c = l.head()->next;

while (c != l.tail()){
out << c->value;
out << ",";
c = c->next;
}
out << " ";
return out;
}

bool operator== (dlist&a, dlist&b){
if(a.getSize() != b.getSize()){
return false;
}

node* c1 = a.head();
node* c2 = b.head();

while(c1 != a.tail() && c2 != b.tail()){
if(c1->value != c2->value){
return false;
}

c1 = c1->next;
c2 = c2->next;
}

return true;
}

dlist operator+ (dlist&a, dlist&b){
dlist l;
node* c = a.head();
node* d = b.head();

while(c->next != a.tail()){
l.push_back(c->next->value);
c = c->next;
}

return l;
}

dlist reverse(dlist& l){
dlist a;
node* c = l.head();

while(c->next != l.tail()){
a.push_front(c->next->value);
c = c->next;
}

return a;
}

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

I have two problems.

1. del() doesn't update the prev pointer.

2. see several syntax errors that would prevent it from compiling successfully.

Please fix and complete my code, and I also need whole code containing main function(list_tests.cc) for testing dlist.cc

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

Sample output:

Enter your list values, separated by spaces 1 2 5 8 You entered 4 elements. List 1, 2,5, 8, Verifying Reversed: 8,5, 2,1, Appended 1,2, 5, 8, 1,2, 3, I 1, 2, 3,1, 2, 5, 8 Your list is not 1 2 3. The empty list is empty. Testing list operations. at 1:2 2:5 3:8 insert 1, 104, 103, 102, 101, 100, 2, 5, 8, del 100, 2, 5, 8, push back 100, 2,5, 8,0, push front ,100, 2,5, 8,0, pop back 0, 100, 2, 5, 8, pop front 100, 2, 5, 8, Size is now 4 Verifying List structure is OK

Explanation / Answer

corrected cpp file

#include <iostream>
#include "dlist.h"
typedef dlist::node node;
dlist::node* dlist::at(int n){
node* c = _head;
while(n > 0 && c){
c = c->next;
n--;
}
return c;
}
void dlist::insert(node* previous, int value){
node* n = new node{value, previous->next, previous};
previous->next = n;
n->next->prev = n;
}
void dlist::del(node* which){
node* nn = which->next;
which->next = nn->next;
delete nn;
}
void dlist::push_back(int value){
node* n = new node{value, _tail, _tail->prev};
_tail->prev->next = n;
_tail->prev= n;
}
void dlist::push_front(int value){
node* n = new node{value, _head -> next,_head};
_head->next = n;
_head->next->prev = n;
}
void dlist::pop_front(){
node* n = _head;
_head = _head->next;
delete n;
}
void dlist::pop_back(){
node* n = _tail;
_tail = _tail->prev;
delete n;
}
bool dlist::empty(){
if(size() == 0)
return true;
else
return false;
}
/* out << l
Prints a list to the ostream out. This is mostly for your convenience in
testing your code; it's much easier to figure out what's going on if you
can easily print out lists!
*/
std::ostream& operator<< (std::ostream& out, dlist& l){
node* c = l.head()->next;
while (c != l.tail()){
out << c->value;
out << ",";
c = c->next;
}
out << " ";
return out;
}
bool operator== (dlist&a, dlist&b){

node* c1 = a.head();
node* c2 = b.head();
while(c1 != a.tail() && c2 != b.tail()){
if(c1->value != c2->value){
return false;
}
c1 = c1->next;
c2 = c2->next;
}
return true;
}
dlist operator+ (dlist&a, dlist&b){
dlist l;
node* c = a.head();
node* d = b.head();
while(c->next != a.tail()){
l.push_back(c->next->value);
c = c->next;
}
return l;
}
dlist reverse(dlist& l){
dlist a;
node* c = l.head();
while(c->next != l.tail()){
a.push_front(c->next->value);
c = c->next;
}
return a;
}

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

#include <iostream>

using namespace std;
#include "dlist.h"
int main()
{
dlist obj;
//pusing values
obj.push_back(12);
obj.push_back(13);
obj.push_back(14);
obj.push_back(15);
// printing list
cout<<"Content of list"<<obj;
dlist reverseObj = obj.reverse(obj);
cout<<"Content of list after reverse"<<reverseObj;

return 0;
}