Answered Essay: Data Structure in C++

Data Structure in C++

Using namespace std;

#pragma once /* dlist.h Doubly-linked lists of ints */
class dlist {
public:
dlist() { }

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

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

// **** Implement ALL the following methods ****
// Returns the node at a particular index (0 is the head). If n >= size()

// return nullptr; if n < 0, return the head of the list.

// Must run in O(n) time.

node* at(int n) const;

// Insert a new value, after an existing one. If previous == nullptr, then

// the list is assumed to be empty.

// Must run in O(1) time.

void insert(node *previous, int value);

// Delete the given node. Should do nothing if which == nullptr.

// Must run in O(1) time.

void remove(node* which);

// Add a new element to the *end* of the list

// Must run in O(1) time.

void push_back(int value);

// Add a new element to the *beginning* of the list

// Must run in O(1) time.

void push_front(int value);

// Remove the first element

// Must run in O(1) time

void pop_front();

// Remove the last element

// Must run in O(1) time

void pop_back();

// Get the size of the list

// Should run in O(n) time at the worst

int size() const;

// Returns true if the list is empty

// Must run in O(1) time

bool empty() const;

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

// **** Implement ALL the following functions ****
/* a == b Compares two lists for equality, returning true if they have the same elements in the same positions. (Hint: it is *not* enough to just compare pointers! You have to compare the values stored in the nodes.) Must run in O(m) time, where m is the length of the shorter of the two lists. */
bool operator== (const dlist& a, const dlist& b);

/* a + b Returns a new list consisting of all the elements of a, followed by all the elements of b (i.e., the list concatenation). Must run in O(n) time in the length of the result. */
dlist operator+ (const dlist& a, const 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. Must run in O(n) time. */
dlist reverse(const dlist& l);

Expert Answer

 

Editable code

Program

Dlist.h

#pragma once

#include <iostream>

class dlist {

public:

dlist() {

_head->next = _tail;

_tail->prev = _head;

_head->prev = nullptr;

_tail->next = nullptr;

};

struct node {

int value;

node* next;

node* prev;

};

node* head() const { return _head; }

node* tail() const { return _tail; }

 

node* at(int index);

 

void insert(node *pre, int v);

 

void del(node* wh);

 

void push_back(int v);

 

void push_front(int v);

 

void pop_front();

 

void pop_back();

 

int getSize();

 

bool empty();

private:

node* _head = new node;

node* _tail = new node;

int size = 0;

};

std::ostream& operator<< (std::ostream& o, dlist& x);

bool operator== (dlist& ax, dlist& bx);

dlist operator+ (dlist& ax, dlist& bx);

dlist reverse(dlist& x);

dlist.cpp

#include “dlist.h”

typedef dlist::node node;

node* dlist::at(int index) {

node* c = _head;

while (index > 0 && c->next) {

c = c->next;

index–;

}

return c;

}

int dlist::getSize() {

return size;

}

bool dlist::empty() {

return size == 0;

}

void dlist::insert(node* pre, int v) {

node* n = new node{ v, pre->next, pre };

pre->next = n;

n->next->prev = n;

size++;

}

void dlist::push_front(int v) {

node* n = new node{ v, _head->next, _head };

n->prev->next = n;

n->next->prev = n;

size++;

}

void dlist::del(node* wh) {

if (wh == _head || wh == _tail) {

std::cout << “Success (Error)n”;

return;

}

node* n = wh;

wh->prev->next = n->next;

wh->next->prev = n->prev;

delete n;

size–;

}

void dlist::push_back(int v) {

node* n = new node{ v, _tail, _tail->prev };

_tail->prev->next = n;

_tail->prev = n;

size++;

}

void dlist::pop_front() {

if (size == 0) {

std::cout << “Success (Error)n”;

return;

}

node* n = _head->next;

_head->next = n->next;

n->next->prev = n->prev;

delete n;

size–;

}

void dlist::pop_back() {

if (size == 0) {

std::cout << “Success (Error)n”;

return;

}

node* n = _tail->prev;

_tail->prev = n->prev;

n->prev->next = _tail;

delete n;

size–;

}

std::ostream& operator<< (std::ostream& o, dlist& x) {

node* cx = x.head()->next;

while (cx != x.tail()) {

o << cx->value;

o << “,”;

cx = cx->next;

}

o << “n”;

return o;

}

bool operator== (dlist&ax, dlist&bx) {

if (ax.getSize() != bx.getSize()) {

return false;

}

node* cx1 = ax.head();

node* cx2 = bx.head();

while (cx1 != ax.tail() && cx2 != bx.tail()) {

if (cx1->value != cx2->value) {

return false;

}

cx1 = cx1->next;

cx2 = cx2->next;

}

return true;

}

dlist operator+ (dlist&ax, dlist&bx) {

dlist x;

node* z = ax.head();

node* v = bx.head();

while (z->next != ax.tail()) {

x.push_back(z->next->value);

z = z->next;

}

while (v->next != bx.tail()) {

x.push_back(v->next->value);

v = v->next;

}

return x;

}

dlist reverse(dlist& x) {

dlist ax;

node* z = x.head();

while (z->next != x.tail()) {

ax.push_front(z->next->value);

z = z->next;

}

return ax;

}

Main.cpp

#include “dlist.h”

using std::cout;

int main() {

dlist x;

x.push_front(5);

x.push_front(3);

x.insert(x.at(1), 6);

cout << “–previous pointer check–n”;

if (x.at(1)->prev == x.head()) {

cout << “Successn”;

}

if (x.at(2)->prev == x.at(1)) {

cout << “Successn”;

}

if (x.at(3)->prev == x.at(2)) {

cout << “Successn”;

}

if (x.tail()->prev == x.at(3)) {

cout << “Successn”;

cout << x;

}

cout << “n”;

cout << “–next pointer check–n”;

if (x.head()->next == x.at(1)) {

cout << “Successn”;

}

if (x.at(1)->next == x.at(2)) {

cout << “Successn”;

}

if (x.at(2)->next == x.at(3)) {

cout << “Successn”;

}

cout << “n”;

cout << “–deletion check–n”;

x.del(x.at(3));

x.del(x.at(1)->prev);

if (x.at(2)->next == x.tail()) {

cout << “Successn”;

}

if (x.tail()->prev == x.at(2)) {

cout << “Successn”;

cout << x;

}

cout << “n”;

cout << “–push_back check–n”;

x.push_back(2);

if (x.tail()->prev->value == 2) {

cout << “Successn”;

}

if (x.at(2)->next->value == 2) {

cout << “Successn”;

cout << x;

}

cout << “n”;

cout << “–pop_front check–n”;

int tmp = x.at(1)->value;

x.pop_front();

if (x.at(1)->value != tmp) {

cout << “Successn”;

}

x.pop_front();

if (x.at(1)->value == 2) {

cout << “Successn”;

}

if (x.at(1)->prev == x.head() && x.head()->next == x.at(1)) {

cout << “Successn”;

}

x.pop_front();

x.pop_front();

cout << x;

cout << “n”;

cout << “–pop_back–n”;

x.pop_back();

x.push_front(2);

x.push_front(5);

x.pop_back();

if (x.tail()->prev->value == 2) {

cout << “Successn”;

}

if (x.at(1)->next == x.tail()) {

cout << “Successn”;

}

cout << “n”;

cout << “–list output check–n”;

cout << x;

x.push_front(1);

x.push_back(3);

x.insert(x.at(2), 4);

cout << x;

cout << “n”;

cout << “–list comparison check–n”;

dlist b;

b.push_front(3);

b.push_front(4);

b.push_front(5);

b.push_front(1);

cout << b;

if (x == b) {

cout << “Success (Same)n”;

}

b.pop_back();

b.push_back(5);

cout << b;

if (!(x == b)) {

cout << “Success (Not Same)n”;

}

cout << “n”;

cout << “–list concatenation check–n”;

cout << “adding l and bn”;

cout << “this is the “concat” listn”;

dlist concat = x + b;

cout << concat;

cout << “n”;

cout << “–reverse check–n”;

cout << “reversed “concat”n”;

dlist rev = reverse(concat);

cout << rev;

//Test check

cout << “nn”;

cout << “All the tests checked out! The double linked list is fully functionaln”;

system(“PAUSE”);

return 0;

}

Buy Essay
Calculate your paper price
Pages (550 words)
Approximate price: -

Help Me Write My Essay - Reasons:

Best Online Essay Writing Service

We strive to give our customers the best online essay writing experience. We Make sure essays are submitted on time and all the instructions are followed.

Our Writers are Experienced and Professional

Our essay writing service is founded on professional writers who are on stand by to help you any time.

Free Revision Fo all Essays

Sometimes you may require our writers to add on a point to make your essay as customised as possible, we will give you unlimited times to do this. And we will do it for free.

Timely Essay(s)

We understand the frustrations that comes with late essays and our writers are extra careful to not violate this term. Our support team is always engauging our writers to help you have your essay ahead of time.

Customised Essays &100% Confidential

Our Online writing Service has zero torelance for plagiarised papers. We have plagiarism checking tool that generate plagiarism reports just to make sure you are satisfied.

24/7 Customer Support

Our agents are ready to help you around the clock. Please feel free to reach out and enquire about anything.

Try it now!

Calculate the price of your order

Total price:
$0.00

How it works?

Follow these simple steps to get your paper done

Place your order

Fill in the order form and provide all details of your assignment.

Proceed with the payment

Choose the payment system that suits you most.

Receive the final file

Once your paper is ready, we will email it to you.

HOW OUR ONLINE ESSAY WRITING SERVICE WORKS

Let us write that nagging essay.

STEP 1

Submit Your Essay/Homework Instructions

By clicking on the "PLACE ORDER" button, tell us your requires. Be precise for an accurate customised essay. You may also upload any reading materials where applicable.

STEP 2

Pick A & Writer

Our ordering form will provide you with a list of writers and their feedbacks. At step 2, its time select a writer. Our online agents are on stand by to help you just in case.

STEP 3

Editing (OUR PART)

At this stage, our editor will go through your essay and make sure your writer did meet all the instructions.

STEP 4

Receive your Paper

After Editing, your paper will be sent to you via email.