implement lazy list

This commit is contained in:
2020-12-31 15:33:21 +01:00
parent a73410882a
commit 55b7225952
2 changed files with 93 additions and 0 deletions

91
Assignment_2/LazyList.cpp Normal file
View File

@@ -0,0 +1,91 @@
#include <bits/stdc++.h>
#include"Node.cpp"
class LazySet {
protected:
Node* first;
std::tuple<Node*, Node*> locate(int element);
public:
LazySet();
bool add(int element);
bool rmv(int element);
bool ctn(int element);
};
LazySet::LazySet(){
first = Node::Dummy(); // dummy node;
Node* last = Node::Dummy();
first->next = last;
}
bool LazySet::add(int element) {
auto tuple = locate(element);
Node* p = std::get<0>(tuple);
Node* c = std::get<1>(tuple);
if (c->is_equal(element)) {
p->unlock();
c->unlock();
return false;
} else {
Node* n = new Node(element);
p->next = n;
p->unlock();
c->unlock();
return true;
}
}
bool LazySet::rmv(int element) {
auto tuple = locate(element);
Node* p = std::get<0>(tuple);
Node* c = std::get<1>(tuple);
if (c->is_equal(element)) {
c->deleted = true;
p->next = c->next;
p->unlock();
c->unlock();
return true;
} else {
p->unlock();
c->unlock();
return false;
}
}
bool LazySet::ctn(int element) {
Node* c = first;
while (c->is_smaller_than(element)) {
c = c->next;
}
if (!c->is_equal(element) || c->deleted) {
return false;
} else {
return true;
}
}
std::tuple<Node*, Node*> LazySet::locate(int element) {
Node* p = first;
Node* c = p->next;
while (c->is_smaller_than(element)) {
p = c;
c = c->next;
}
p->lock();
c->lock();
if (!p->deleted && !c->deleted && p->next == c) {
p->unlock();
c->unlock();
return {p, c};
}
p->unlock();
c->unlock();
return {Node::Dummy(), Node::Dummy()};
}

View File

@@ -8,6 +8,7 @@ class Node {
public: public:
Node* next; Node* next;
bool deleted;
Node(int element); Node(int element);
static Node* Dummy(); static Node* Dummy();
@@ -24,6 +25,7 @@ class Node {
Node::Node(int element){ Node::Node(int element){
data = element; data = element;
is_dummy = false; is_dummy = false;
deleted = false;
} }
Node* Node::Dummy(){ Node* Node::Dummy(){