Data Structure in C++ Doubly Linked Lists of ints This is my code below: dlist.c
ID: 3863875 • 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
--------------------------------------------------------------------------------
Output Example:
In this assignment, you will implement a doubly-linked list class. together with some list operations. To make things easier, you'll implement a list of int rather than a template class pragma once dlist. h Doubly linked lists of ints include Kostream class dlist public: d list struct node int value node next node prev; node' head() const return -head; node" tail() const t return -tail Implement ALL the following methods Returns the node at a particular index (0 is the head node at(int) Insert a new value, after an existing one void insert(node *previous int value Delete the given node void del (node which)Explanation / Answer
#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;
}
while(c->next != a.tail()){
l.push_back(c->next->value);
c = c->next;
}
bool dlist::empty(){
return size == 0;
}
/
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;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.