Skip to content

Commit

Permalink
Интерфейс за абстрактен стек
Browse files Browse the repository at this point in the history
  • Loading branch information
triffon committed May 8, 2019
1 parent b475a8d commit dd7d5da
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 24 deletions.
20 changes: 20 additions & 0 deletions stack/abstract_stack.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#ifndef __ABSTRACT_STACK_H
#define __ABSTRACT_STACK_H

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

// включване на елемент и връща дали е било успешно
virtual bool push(T const& x) = 0;

// изключване на елемент
virtual T pop() = 0;

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

#endif
4 changes: 3 additions & 1 deletion stack/lstack.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#ifndef __LSTACK_CPP
#define __LSTACK_CPP

#include "abstract_stack.h"

#include <iostream>

template <typename T>
Expand All @@ -10,7 +12,7 @@ struct StackElement {
};

template <typename T>
class LinkedStack {
class LinkedStack : public AbstractStack<T> {
// представяне
StackElement<T>* top; // указател към връх на стека

Expand Down
36 changes: 23 additions & 13 deletions stack/main.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
#include <iostream>
#include <cstring>
//#include "stack.h"
//#include "rstack.h"
#include "stack.h"
#include "rstack.h"
#include "lstack.cpp"

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

void testStack() {
Stack s;
MyStack s;
std::cout << "s.empty() == " << s.empty() << std::endl;
s.push(42);
std::cout << "s.empty() == " << s.empty() << std::endl;
Expand All @@ -29,7 +29,7 @@ char printDigit(int d) {

// извежда n в k-ична бройна система
void printInBase(int n, int k) {
Stack s;
MyStack s;
std::cout << n << "(10) --> ";
while (n > 0) {
s.push(n % k);
Expand Down Expand Up @@ -79,8 +79,8 @@ int todigit(char c) {
*/

int calculate_expr(char const* s) {
Stack args;
Stack ops;
MyStack args;
MyStack ops;
while(*s) {
// magic
if (isdigit(*s))
Expand Down Expand Up @@ -123,7 +123,7 @@ bool matchParentheses(char open, char close) {
}

bool checkParentheses(char const* expr) {
Stack pstack;
MyStack pstack;
while(*expr) {
if (isOpenParenthesis(*expr))
pstack.push(*expr);
Expand Down Expand Up @@ -184,10 +184,10 @@ void testParentheses() {
}

void testCopy() {
Stack s1;
MyStack s1;
for(int i = 0; i < 10; i++)
s1.push(i);
Stack s2;
MyStack s2;
s2 = s1;
s2.pop();
s2.push(20);
Expand All @@ -199,20 +199,30 @@ void testCopy() {

void testCreateDestroy() {
for(int i = 0; i < 1E8; i++) {
// Stack s;
Stack* s = new Stack;
// MyStack s;
MyStack* s = new MyStack;
for(int j = 0; j < 10; j++)
s->push(j);
delete s;
}
}

void testAbstractStack() {
AbstractStack<int>* s = new Stack;
s = new ResizingStack;
s = new LinkedStack<int>;
s->push(1);
std::cout << s->pop();
AbstractStack<AbstractStack<double> >* ss;
}

int main() {
// testStack();
// testPrintInBase();
// testExpression();
// testParentheses();
testCopy();
// testCopy();
// testCreateDestroy();
testAbstractStack();
return 0;
}
6 changes: 5 additions & 1 deletion stack/rstack.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
#include <iostream>
#include "rstack.h"

const int INITIAL_CAPACITY = 10;
const int EMPTY_STACK = -1;
const int RESIZE_FACTOR = 2;

ResizingStack::ResizingStack() {
capacity = INITIAL_CAPACITY;
a = new int[capacity];
Expand All @@ -11,7 +15,7 @@ bool ResizingStack::empty() const {
return top == EMPTY_STACK;
}

bool ResizingStack::push(int x) {
bool ResizingStack::push(int const& x) {
if (full()) {
resize();
}
Expand Down
8 changes: 3 additions & 5 deletions stack/rstack.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
#ifndef __RSTACK_H
#define __RSTACK_H

const int INITIAL_CAPACITY = 10;
const int EMPTY_STACK = -1;
const int RESIZE_FACTOR = 2;
#include "abstract_stack.h"

class ResizingStack {
class ResizingStack : public AbstractStack<int> {
// представяне
int* a; // указател към масив в динамичната памет за стека
int top; // връх на стека
Expand Down Expand Up @@ -35,7 +33,7 @@ class ResizingStack {
bool empty() const;

// включване на елемент и връща дали е било успешно
bool push(int x);
bool push(int const& x);

// изключване на елемент
int pop();
Expand Down
4 changes: 3 additions & 1 deletion stack/stack.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include <iostream>
#include "stack.h"

const int EMPTY_STACK = -1;

Stack::Stack() {
top = EMPTY_STACK;
}
Expand All @@ -9,7 +11,7 @@ bool Stack::empty() const {
return top == EMPTY_STACK;
}

bool Stack::push(int x) {
bool Stack::push(int const& x) {
if (full()) {
std::cerr << "Опит за добавяне в пълен стек!\n";
return false;
Expand Down
7 changes: 4 additions & 3 deletions stack/stack.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#ifndef __STACK_H
#define __STACK_H

#include "abstract_stack.h"

const int MAX_STACK = 200;
const int EMPTY_STACK = -1;

class Stack {
class Stack : public AbstractStack<int> {
// представяне
int a[MAX_STACK]; // памет за стека
int top; // връх на стека
Expand All @@ -21,7 +22,7 @@ class Stack {
bool empty() const;

// включване на елемент и връща дали е било успешно
bool push(int x);
bool push(int const& x);

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

0 comments on commit dd7d5da

Please sign in to comment.