diff --git a/Assignment_2/FineList.cpp b/Assignment_2/FineList.cpp index 242f44e..2d0e7bb 100644 --- a/Assignment_2/FineList.cpp +++ b/Assignment_2/FineList.cpp @@ -14,9 +14,8 @@ class FineSet { }; FineSet::FineSet(){ - first = new Node(); // dummy node; - Node* last = new Node(); - last->data = INT_MAX; // end node; + first = Node::Dummy(); // dummy node; + Node* last = Node::Dummy(); first->next = last; } @@ -25,20 +24,19 @@ bool FineSet::add(int element) { Node* p = this->first; Node* c = p->next; c->lock(); - while (c->data < element) { + while (c->is_smaller_than(element)) { p->unlock(); p = c; c = c->next; c->lock(); } - if (c->data == element) { + if (c->is_equal(element)) { c->unlock(); p->unlock(); return false; } else { - Node* n = new Node(); - n->data = element; + Node* n = new Node(element); n->next = c; p->next = n; @@ -54,7 +52,7 @@ bool FineSet::rmv(int element) { Node* c = p->next; c->lock(); - while (c->data < element) { + while (c->is_smaller_than(element)) { p->unlock(); c = c->next; c->lock(); @@ -78,13 +76,13 @@ bool FineSet::ctn(int element) { Node* c = p->next; c->lock(); - while (c->data < element) { + while (c->is_smaller_than(element)) { p->unlock(); c = c->next; c->lock(); } - if (c->data == element) { + if (c->is_equal(element)) { c->unlock(); p->unlock(); return true; diff --git a/Assignment_2/Node.cpp b/Assignment_2/Node.cpp index edd4ee6..6800d6e 100644 --- a/Assignment_2/Node.cpp +++ b/Assignment_2/Node.cpp @@ -3,12 +3,38 @@ class Node { public: int data; + bool is_dummy; Node* next; std::mutex mut; public: + Node(int element); + static Node* Dummy(); void lock(); void unlock(); + bool is_smaller_than(int n); + bool is_equal(int n); }; +Node::Node(int element){ + this->data = element; + this->is_dummy = false; +} + +Node* Node::Dummy(){ + Node* n = new Node(0); + n->is_dummy = true; + return n; +} + +bool Node::is_smaller_than(int n) { + // Everything is smaller than a dummy -> false + return this->data < n && !this->is_dummy; +} + +bool Node::is_equal(int n) { + // Dummies are not equal to any number -> false + return this->data == n && !this->is_dummy; +} + void Node::lock() { this->mut.lock(); }