Data Structure in C++ Doubly Linked Lists of ints This is my code below: dlist.c
ID: 3843859 • Letter: D
Question
Data Structure in C++
Doubly Linked Lists of ints
This is my code below: dlist.cc
-------------------------------------------------------------------------------------------------------
#include <iostream>
#include "dlist.h"
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->next = n;
}//update tail
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, nullptr};
_tail->next = n;
_tail-> 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;
}
int dlist::size() {
node* c = _head;
int s = 0;
while(c){
s++;
c = c->next;
}
return s;
}
--------------------------------------------------------------------------------
Please fix and complete my code, and I also need whole code contatining main function(list_tests.cc) for testing dlist.cc
--------------------------------------------------------------------------------
Output I need:
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"
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->next = n;
}//update tail
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, nullptr};
_tail->next = n;
_tail-> 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;
}
int dlist::size() {
node* c = _head;
int s = 0;
while(c){
s++;
c = c->next;
}
return s;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.