add possibility to print a set to a string

This commit is contained in:
2021-01-02 18:39:25 +01:00
parent 297ee216dd
commit e76cc094e5
4 changed files with 29 additions and 5 deletions

View File

@@ -3,13 +3,13 @@
Node::Node(int element){ Node::Node(int element){
data = element; data = element;
is_dummy = false; dummy = false;
deleted = false; deleted = false;
} }
Node* Node::Dummy(){ Node* Node::Dummy(){
Node* n = new Node(0); Node* n = new Node(0);
n->is_dummy = true; n->dummy = true;
return n; return n;
} }
@@ -19,12 +19,12 @@ bool Node::is_smaller_than(Node* n) {
bool Node::is_smaller_than(int n) { bool Node::is_smaller_than(int n) {
// Everything is smaller than a dummy -> false // Everything is smaller than a dummy -> false
return this->data < n && !this->is_dummy; return this->data < n && !this->dummy;
} }
bool Node::is_equal(int n) { bool Node::is_equal(int n) {
// Dummies are not equal to any number -> false // Dummies are not equal to any number -> false
return this->data == n && !this->is_dummy; return this->data == n && !this->dummy;
} }
void Node::lock() { void Node::lock() {
@@ -33,3 +33,11 @@ void Node::lock() {
void Node::unlock() { void Node::unlock() {
this->mut.unlock(); this->mut.unlock();
} }
bool Node::is_dummy() {
return dummy;
}
int Node::get_data(){
return data;
}

View File

@@ -5,7 +5,7 @@
class Node { class Node {
protected: protected:
int data; int data;
bool is_dummy; bool dummy;
std::mutex mut; std::mutex mut;
public: public:
@@ -21,4 +21,6 @@ class Node {
bool is_smaller_than(int n); bool is_smaller_than(int n);
bool is_smaller_than(Node* n); bool is_smaller_than(Node* n);
bool is_equal(int n); bool is_equal(int n);
bool is_dummy();
int get_data();
}; };

View File

@@ -1,6 +1,9 @@
#include "Set.h" #include "Set.h"
#include "Node.h" #include "Node.h"
#include <iostream>
#include<sstream>
Set::Set(std::string _name){ Set::Set(std::string _name){
name = _name; name = _name;
first = Node::Dummy(); first = Node::Dummy();
@@ -11,3 +14,13 @@ bool Set::add(int element) { return false; }
bool Set::rmv(int element) { return false; } bool Set::rmv(int element) { return false; }
bool Set::ctn(int element) { return false; } bool Set::ctn(int element) { return false; }
std::string Set::get_name() { return name; } std::string Set::get_name() { return name; }
std::string Set::print_set(){
Node* c = this->first->next;
std::stringstream ss;
while (!c->is_dummy()) {
ss << c->get_data() << " ";
c = c->next;
}
return ss.str();
}

View File

@@ -12,4 +12,5 @@ class Set {
bool rmv(int element); bool rmv(int element);
bool ctn(int element); bool ctn(int element);
std::string get_name(); std::string get_name();
std::string print_set();
}; };