consolidate all repos to one for archive
This commit is contained in:
26
semester_2/programiranje_2/primeri/Example19/Stack.h
Normal file
26
semester_2/programiranje_2/primeri/Example19/Stack.h
Normal file
@@ -0,0 +1,26 @@
|
||||
#ifndef EXAMPLE19_STACK_H
|
||||
#define EXAMPLE19_STACK_H
|
||||
|
||||
template <typename T>
|
||||
class Stack {
|
||||
private:
|
||||
int top;
|
||||
T* impl;
|
||||
public:
|
||||
Stack(int n=5) : top(-1), impl(new T[n]) {
|
||||
}
|
||||
~Stack() {
|
||||
delete[] impl;
|
||||
}
|
||||
bool empty() const {
|
||||
return top == -1;
|
||||
}
|
||||
void push(T el) {
|
||||
impl[++top] = el;
|
||||
}
|
||||
T pop() {
|
||||
return impl[top--];
|
||||
}
|
||||
};
|
||||
|
||||
#endif //EXAMPLE19_STACK_H
|
24
semester_2/programiranje_2/primeri/Example19/main.cpp
Normal file
24
semester_2/programiranje_2/primeri/Example19/main.cpp
Normal file
@@ -0,0 +1,24 @@
|
||||
#include <iostream>
|
||||
#include "Stack.h"
|
||||
|
||||
int main() {
|
||||
Stack<char> myStack1(10); // always explicit instantiation
|
||||
myStack1.push('+');
|
||||
myStack1.push('+');
|
||||
myStack1.push('c');
|
||||
while (!myStack1.empty()) {
|
||||
std::cout << myStack1.pop() << " ";
|
||||
}
|
||||
std::cout << std::endl;
|
||||
|
||||
Stack<int> myStack2;
|
||||
myStack2.push(1);
|
||||
myStack2.push(2);
|
||||
myStack2.push(3);
|
||||
while (!myStack2.empty()) {
|
||||
std::cout << myStack2.pop() << " ";
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user