Skip to content

Commit

Permalink
Шаблон на интерфейс AbstractStack
Browse files Browse the repository at this point in the history
  • Loading branch information
triffon committed May 12, 2022
1 parent aa74466 commit 77842b6
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 19 deletions.
20 changes: 20 additions & 0 deletions lectures/1/stack/abstract_stack.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#ifndef __ABSTRACT_STACK
#define __ABSTRACT_STACK

template <typename T>
class AbstractStack {
public:
// проверка за празнота
virtual bool empty() const = 0;

// включване на елемент
virtual void push(T const& x) = 0;

// намиране на последния включен елемент
virtual T const& peek() const = 0;

// изключване на последния включен елемент
virtual T pop() = 0;
};

#endif
4 changes: 3 additions & 1 deletion lectures/1/stack/lstack.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@
#include <cstddef>
#include <iostream>

#include "abstract_stack.hpp"

template <typename T>
struct StackElement {
T data;
StackElement<T>* next;
};

template <typename T>
class LinkedStack {
class LinkedStack : public AbstractStack<T> {
private:
// указател към двойната кутия, която е връх на стека
StackElement<T>* top;
Expand Down
27 changes: 24 additions & 3 deletions lectures/1/stack/main.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
#include <iostream>
#include <cassert>
//#include "stack.hpp"
#include "stack.hpp"
#include "rstack.hpp"
#include "lstack.hpp"
#include "abstract_stack.hpp"

//using Stack = ResizingStack;
using Stack = LinkedStack<int>;
//using Stack = LinkedStack<int>;

void convertBase() {
unsigned n;
Expand Down Expand Up @@ -179,10 +180,30 @@ void testDestroyStack() {
std::cin >> x;
}

void testAbstractStack() {
AbstractStack<int>* stacks[] = {
new Stack,
new ResizingStack,
new LinkedStack<int>
};
size_t count = sizeof(stacks) / sizeof(stacks[0]);
for(int i = 0; i < count; i++)
for(int x = 1; x <= 10; x++)
stacks[i]->push(x);

for(int i = 0; i < count; i++)
while (!stacks[i]->empty())
std::cout << stacks[i]->pop() << std::endl;

for(int i = 0; i < count; i++)
delete stacks[i];
}

int main(int, char**) {
//convertBase();
//testExpression();
//autoTestParentheses();
//testCopyStack();
testDestroyStack();
//testDestroyStack();
testAbstractStack();
}
4 changes: 2 additions & 2 deletions lectures/1/stack/rstack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ bool ResizingStack::empty() const {
}

// включване на елемент
void ResizingStack::push(int x) {
void ResizingStack::push(int const& x) {
if (full())
resize();
stack[++top] = x;
}

// намиране на последния включен елемент
int ResizingStack::peek() const {
int const& ResizingStack::peek() const {
assert(!empty());
return stack[top];
}
Expand Down
12 changes: 7 additions & 5 deletions lectures/1/stack/rstack.hpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
#ifndef __RSTACK_HPP
#define __RSTACK_HPP
#include <cstddef>
#include "abstract_stack.hpp"

const int INITSTACK = 10;
const int EMPTY_STACK = -1;

class ResizingStack {
class ResizingStack : public AbstractStack<int> {
private:
static const int EMPTY_STACK = -1;
static const int INITSTACK = 10;

// указател към масива на стека
int* stack;

Expand Down Expand Up @@ -41,10 +43,10 @@ class ResizingStack {
bool empty() const;

// включване на елемент
void push(int x);
void push(int const& x);

// намиране на последния включен елемент
int peek() const;
int const& peek() const;

// изключване на последния включен елемент
int pop();
Expand Down
4 changes: 2 additions & 2 deletions lectures/1/stack/stack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ bool Stack::empty() const {
}

// включване на елемент
void Stack::push(int x) {
void Stack::push(int const& x) {
assert(!full());
stack[++top] = x;
}

// намиране на последния включен елемент
int Stack::peek() const {
int const& Stack::peek() const {
assert(!empty());
return stack[top];
}
Expand Down
14 changes: 8 additions & 6 deletions lectures/1/stack/stack.hpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
#ifndef __STACK_HPP
#define __STACK_HPP
#include "abstract_stack.hpp"

const int MAXSTACK = 100;
const int EMPTY_STACK = -1;

class Stack {
class Stack : public AbstractStack<int> {
private:
static const int MAXSTACK = 100;
static const int EMPTY_STACK = -1;


// стекът
int stack[MAXSTACK];

Expand All @@ -22,10 +24,10 @@ class Stack {
bool empty() const;

// включване на елемент
void push(int x);
void push(int const& x);

// намиране на последния включен елемент
int peek() const;
int const& peek() const;

// изключване на последния включен елемент
int pop();
Expand Down

0 comments on commit 77842b6

Please sign in to comment.