-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsyntaxGenStack.h
More file actions
106 lines (91 loc) · 1.89 KB
/
Copy pathsyntaxGenStack.h
File metadata and controls
106 lines (91 loc) · 1.89 KB
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
100
101
102
103
104
105
106
#ifndef SYNTAXGENSTACK_H
#define SYNTAXGENSTACK_H
#include <iostream>
#include <stdexcept>
#include <fstream>
//#include "stackNotes.cpp"
using namespace std;
template <class T>
class syntaxGenStack{
public:
syntaxGenStack(); // constructor
syntaxGenStack(int maxSize); //overloaded constructor
~syntaxGenStack(); //destructor
void push(const T& d); //pushes the &object of class T
T pop();
T peek();
void resize();
bool isFull();
bool isEmpty();
//int maxSize;
int size;
int top;
T *myArray; //pointer because the over loaded constructor is not set to how many elements there will be
};
template <class T>
syntaxGenStack<T>::syntaxGenStack(){
myArray = new T[128];
size = 128;
top = -1;
}
template <class T>
syntaxGenStack<T>::syntaxGenStack(int maxSize){
myArray = new T[maxSize];
size = maxSize;
top = -1;
}
template <class T>
syntaxGenStack<T>::~syntaxGenStack(){
delete myArray;
}
template <class T>
void syntaxGenStack<T>::push(const T& d){
if(top >= size-1){
this->resize();
myArray[++top] = d;
}
else{
myArray[++top] = d;
}
}
template<class T>
T syntaxGenStack<T>::peek(){
//check if empty
if(top<0){
std::cout << "Stack is empty" << endl;
return '\0';
}
return myArray[top];
}
template <class T>
T syntaxGenStack<T>::pop(){
if(top<0){
throw std::runtime_error("Stack is empty.");
return '\0';
}
else{
return myArray[top--];
}
}
template <class T>
bool syntaxGenStack<T>::isEmpty(){
return (top == -1);
}
template <class T>
bool syntaxGenStack<T>::isFull(){
return (top == size-1);
}
template <class T>
void syntaxGenStack<T>::resize(){
syntaxGenStack stack2(size);
while(!this->isEmpty()){
stack2.push(this->pop());
}
delete myArray;
size *= 2;
myArray = new T[size];
while(!stack2.isEmpty()){
this->push(stack2.pop());
}
}
#endif