add abstract datatype datastructure for stack

This commit is contained in:
2021-01-09 18:43:31 +01:00
parent 6485a3b3d6
commit 5b1a978049
5 changed files with 172 additions and 6 deletions

View File

@@ -1,5 +1,67 @@
class Stack {
void push(int element, bool output);
void pop(int element, bool output);
void size(int element, bool output);
};
#include <stack>
#include<stdexcept>
#include <sstream>
#include <stdlib.h>
#include "ADT_Stack.h"
operation create_operation(methodname name, int value) {
operation op;
op.method = name;
op.value = value;
return op;
}
void ADTStack::push(int element){
stack.push(element);
}
void ADTStack::pop(int output) {
if (stack.empty() == false) {
int top = stack.top();
if (output == top){
stack.pop();
} else {
std::ostringstream oss;
oss << "Invalid operation: pop " << output << " (actual top: " << top << ")";
throw std::invalid_argument(oss.str());
}
} else {
std::ostringstream oss;
oss << "Invalid operation: pop " << output << " (stack is empty)";
throw std::invalid_argument(oss.str());
}
}
void ADTStack::size(int output){
int size = stack.size();
if (output != size) {
std::ostringstream oss;
oss << "Invalid operation: size " << output << " (actual size: " << size << ")";
throw std::invalid_argument(oss.str());
}
}
void ADTStack::do_op(operation * op) {
switch (op->method) {
case methodname::push:
push(op->value);
break;
case methodname::pop:
pop(op->value);
break;
case methodname::size:
size(op->value);
break;
default:
break;
}
}
void ADTStack::do_ops(std::vector<operation> ops) {
for (operation op : ops) {
do_op(&op);
}
}

View File

@@ -0,0 +1,21 @@
#include <stack>
#include <vector>
enum methodname {push, pop, size, noop};
typedef struct _operation{
methodname method;
int value;
} operation;
operation create_operation(methodname name, int value);
class ADTStack {
private:
std::stack<int> stack;
public:
void push(int element);
void pop(int output);
void size(int output);
void do_op(operation * op);
void do_ops(std::vector<operation> ops);
};

View File

@@ -1 +1 @@
add_library(sets STATIC Node.h Node.cpp Stack.h Stack.cpp ADT_Stack.cpp )
add_library(sets STATIC ADT_Stack.h ADT_Stack.cpp Node.h Node.cpp Stack.h Stack.cpp )