Skip to content

Commit

Permalink
Реализация S-списъци
Browse files Browse the repository at this point in the history
  • Loading branch information
triffon committed Dec 1, 2022
1 parent 62ca831 commit 77c052c
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 0 deletions.
1 change: 1 addition & 0 deletions lectures/1/polylist/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
#include "doctest.h"

#include "sqlist_tests.hpp"
#include "slist_tests.hpp"
20 changes: 20 additions & 0 deletions lectures/1/polylist/selement.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#ifndef __SELEMENT_HPP
#define __SELEMENT_HPP

#include <iostream>
#include "llist.hpp"

class SElement {
public:
virtual SElement* clone() const = 0;

// извеждане
virtual void print(std::ostream& = std::cout) = 0;

// събиране на елемeнти
virtual void collect(LinkedList<int>&) = 0;

~SElement() {}
};

#endif
25 changes: 25 additions & 0 deletions lectures/1/polylist/sint.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#ifndef __SINT_HPP
#define __SINT_HPP

#include "selement.hpp"

class SInt : public SElement {
private:
int data;
public:
SInt* clone() const { return new SInt(*this); }

SInt(int _data = 0) : data(_data) {}

// извеждане
void print(std::ostream& os) {
os << data;
}

// събиране на елемeнти
void collect(LinkedList<int>& l) {
l.insertLast(data);
}
};

#endif
45 changes: 45 additions & 0 deletions lectures/1/polylist/slist.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#ifndef __SLIST_HPP
#define __SLIST_HPP

#include "selement.hpp"

class SList : public LinkedList<SElement*>,
public SElement {
public:
SList* clone() const { return new SList(*this); }

// TODO: голяма 4

SList& operator<<(SElement const& se) {
LinkedList<SElement*>::insertLast(se.clone());
return *this;
}

// извеждане
void print(std::ostream& os) {
os << '(';
/*
for(SElement* se : *this) {
se->print(os);
os << ' ';
}
*/
SList::I it = begin();
if (it.valid()) {
(*it++)->print(os);
while(it.valid()) {
os << ' ';
(*it++)->print(os);
}
}
os << ')';
}

// събиране на елемeнти
void collect(LinkedList<int>& l) {
for(SElement* se : *this)
se->collect(l);
}
};

#endif
43 changes: 43 additions & 0 deletions lectures/1/polylist/slist_tests.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#ifndef __SLIST_TESTS_HPP
#define __SLIST_TESTS_HPP

#include "doctest.h"
#include <sstream>
#include "sint.hpp"
#include "slist.hpp"

SList SList1(SElement const& x) {
return SList() << x;
}

SList SList2(SElement const& x,
SElement const& y) {
return SList1(x) << y;
}

TEST_CASE("Извеждане и събиране на елементи от дълбок списък") {
SList sl = SList()
<<
SList2(SInt(1), SList1(SInt(2)))
<<
(SList()
<< SList2(SList1(SInt(3)), SInt(4))
<< SList2(SInt(5), SList1(SInt(6)))
<< SList()
<< SList1(SInt(7)))
<< SInt(8);

std::ostringstream os;
sl.print(os);
CHECK_EQ(os.str(), "((1 (2)) (((3) 4) (5 (6)) () (7)) 8)");

LinkedList<int> result;
sl.collect(result);

int i = 1;
for(int x : result)
CHECK_EQ(x, i++);
CHECK_EQ(i, 9);
}

#endif

0 comments on commit 77c052c

Please sign in to comment.