1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
|
THIS IS THE HEADER FILS mystack.h
#include <iostream>
using namespace std;
template<typename T, int N>
class Stack {
private:
T stackArray[N];
public:
int topLevel;
void push(T x);
int isEmpty();
T pop();
T empty();
T peek();
Stack();
~Stack();
void fill(T x);
T getTopLevel();
int isFull();
};
template<typename T, int N>
void Stack<T, N>::push(T x) {
if (topLevel >= N - 1)
{
cout<<"The stack is full!\n";
return;
}
stackArray[++topLevel] = x;
}
template<typename T, int N>
int Stack<T, N>::isEmpty() {
return (topLevel < 0);
}
template<typename T, int N>
int Stack<T, N>::isFull() {
return (topLevel = N - 1);
}
template<typename T, int N>
T Stack<T, N>::pop() {
if (isEmpty()) {
cout<<"The stack is empty! \n";
T x;
return x;
}
return stackArray[topLevel--];
}
template<typename T, int N>
T Stack<T, N>::peek() {
if (isEmpty()) {
cout<<"The stack is empty! \n";
T x;
return x;
}
return stackArray[topLevel];
}
template<typename T, int N>
Stack<T, N>::Stack() { // constructor
topLevel = -1;
}
template<typename T, int N>
Stack<T, N>::~Stack() { // destructor
}
template<typename T, int N>
void Stack<T, N>::fill(T x){
while (topLevel <N-1) {
stackArray[++topLevel] = x;
}
}
template<typename T, int N>
T Stack<T, N>::empty(){
while (isEmpty()) {
return stackArray[topLevel--];
}
}
template<typename T, int N, int M>
void transfer(Stack<T, N>& st, Stack<T, M>& other) {
while ((!st.isEmpty())||(!other.isFull())) {
other.push(st.peek());
st.pop();
}
}
template<typename T, int N>
T Stack<T, N>::getTopLevel() {
int x = topLevel;
return x+1;
}
|