mirror of
https://github.com/13hannes11/UU_la_parallel_programming_practical_assignments.git
synced 2024-09-04 00:50:58 +02:00
add abstract datatype datastructure for stack
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
21
Assignment_3/lib/ADT_Stack.h
Normal file
21
Assignment_3/lib/ADT_Stack.h
Normal 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);
|
||||
};
|
||||
@@ -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 )
|
||||
|
||||
Reference in New Issue
Block a user