Skip to content

Commit

Permalink
Операция за присвояване за двете реализации на стека
Browse files Browse the repository at this point in the history
  • Loading branch information
triffon committed Apr 11, 2022
1 parent cbcbd3c commit bf0c018
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 13 deletions.
27 changes: 22 additions & 5 deletions lectures/1/stack/lstack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,21 @@ LinkedStack::LinkedStack() : top(nullptr) {}

// копиране на стек
LinkedStack::LinkedStack(LinkedStack const& other) : top(nullptr) {
copyStack(other);
}

// деструктор
LinkedStack::~LinkedStack() {
erase();
}

void LinkedStack::erase() {
// !!! delete top;
while (!empty()) pop();
// домашно: реализирайте го директно с представянето
}

void LinkedStack::copyStack(LinkedStack const& other) {
if (other.top != nullptr) {
// other не е празен
StackElement* lastAdded = top = new StackElement{other.top->data, nullptr};
Expand All @@ -18,13 +33,15 @@ LinkedStack::LinkedStack(LinkedStack const& other) : top(nullptr) {
}
}

// деструктор
LinkedStack::~LinkedStack() {
// !!! delete top;
while (!empty()) pop();
// домашно: реализирайте го директно с представянето
LinkedStack& LinkedStack::operator=(LinkedStack const& other) {
if (this != &other) {
erase();
copyStack(other);
}
return *this;
}


// проверка за празнота
bool LinkedStack::empty() const {
return top == nullptr;
Expand Down
9 changes: 9 additions & 0 deletions lectures/1/stack/lstack.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,22 @@ class LinkedStack {
// указател към двойната кутия, която е връх на стека
StackElement* top;

// копиране на стек
void copyStack(LinkedStack const&);

// изтриване на стек
void erase();

public:
// създаване на празен стек
LinkedStack();

// конструктор за копиране
LinkedStack(LinkedStack const&);

// операция за присвояване
LinkedStack& operator=(LinkedStack const&);

// деструктор
~LinkedStack();

Expand Down
15 changes: 9 additions & 6 deletions lectures/1/stack/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
#include "rstack.hpp"
#include "lstack.hpp"

typedef ResizingStack Stack;
//typedef LinkedStack Stack;
//typedef ResizingStack Stack;
typedef LinkedStack Stack;

void convertBase() {
unsigned n;
Expand Down Expand Up @@ -150,10 +150,13 @@ void autoTestParentheses() {
}

void testCopyStack() {
LinkedStack s1;
Stack s1;
for(int i = 1; i <= 10; i++)
s1.push(i);
LinkedStack s2 = s1;
//Stack s2 = s1;
Stack s2;
s2 = s1;
s2 = s2;
std::cout << s2.pop() << std::endl;
// !!! std::cout << s1.pop() << std::endl;
s2.push(20);
Expand All @@ -179,6 +182,6 @@ int main(int, char**) {
//convertBase();
//testExpression();
//autoTestParentheses();
// testCopyStack();
testDestroyStack();
testCopyStack();
// testDestroyStack();
}
18 changes: 16 additions & 2 deletions lectures/1/stack/rstack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,27 @@ ResizingStack::ResizingStack() : capacity(INITSTACK), top(EMPTY_STACK) {

ResizingStack::ResizingStack(ResizingStack const& other)
: top(other.top), capacity(other.capacity) {
copyData(other);
}

ResizingStack::~ResizingStack() {
delete[] stack;
}

void ResizingStack::copyData(ResizingStack const& other) {
stack = new int[capacity];
for(int i = 0; i <= top; i++)
stack[i] = other.stack[i];
}

ResizingStack::~ResizingStack() {
delete[] stack;
ResizingStack& ResizingStack::operator=(ResizingStack const& other) {
if (this != &other) {
delete[] stack;
top = other.top;
capacity = other.capacity;
copyData(other);
}
return *this;
}

// проверка за празнота
Expand Down
6 changes: 6 additions & 0 deletions lectures/1/stack/rstack.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,19 @@ class ResizingStack {

// разширяване на стека
void resize();

// копиране на стека
void copyData(ResizingStack const&);
public:
// създаване на празен стек
ResizingStack();

// конструктор за копиране
ResizingStack(ResizingStack const&);

// операция за присвояване
ResizingStack& operator=(ResizingStack const&);

// деструктор
~ResizingStack();

Expand Down

0 comments on commit bf0c018

Please sign in to comment.