Last active
August 29, 2017 22:13
-
-
Save squiidz/57fb759748b48f0d830cefe20659e3dd to your computer and use it in GitHub Desktop.
Virtual Double Linked List C++
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include "node.h" | |
#include "nodeValue.h" | |
using namespace std; | |
struct animal: public nodeValue { | |
int age; | |
animal(int _age) : age(_age) {}; | |
int value() { return age; } | |
bool equal_to(nodeValue* a) { return a->value() == age; } | |
}; | |
int main() { | |
animal* a = new animal(2); | |
node n = node(a); | |
animal* b = new animal(12); | |
node* nn = new node(b); | |
n.append_node(nn); | |
animal* c = new animal(3); | |
node* nnn = new node(c); | |
n.append_node(nnn); | |
delete n.remove_node(b); | |
n.print_list(); | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include "node.h" | |
#include "nodeValue.h" | |
node::node(nodeValue* value): n_value(value), n_next(NULL){} | |
node::~node() {} | |
void node::set_value(nodeValue* v) { | |
n_value = v; | |
} | |
nodeValue* node::get_value() { | |
return n_value; | |
} | |
bool node::bind_node(node *n) { | |
if (n_next == NULL) { | |
n_next = n; | |
n_next->n_prev = this; | |
return true; | |
} | |
return false; | |
} | |
bool node::append_node(node* n) { | |
if (n_next == NULL) | |
return bind_node(n); | |
return n_next->append_node(n); | |
} | |
node* node::remove_node(nodeValue* v) { | |
if (n_next == NULL) | |
return NULL; | |
if (n_next->n_value->equal_to(v)) { | |
node *to_del = n_next; | |
n_next = n_next->n_next; | |
n_prev = n_next->n_prev; | |
return to_del; | |
} | |
return n_next->remove_node(v); | |
} | |
void node::print_list() { | |
std::cout << n_value->value() << std::endl; | |
if (n_next != NULL) | |
return n_next->print_list(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include "nodeValue.h" | |
class node { | |
public: | |
node(nodeValue*); | |
~node(); | |
nodeValue* get_value(); | |
void set_value(nodeValue*); | |
bool bind_node(node*); | |
bool append_node(node*); | |
node* remove_node(nodeValue*); | |
void print_list(); | |
private: | |
nodeValue* n_value; | |
node *n_next; | |
node *n_prev; | |
}; | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "nodeValue.h" | |
nodeValue::nodeValue(){} | |
nodeValue::~nodeValue(){} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
class nodeValue { | |
public: | |
nodeValue(); | |
~nodeValue(); | |
virtual bool equal_to(nodeValue*) = 0; | |
virtual int value() = 0; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment