-
Notifications
You must be signed in to change notification settings - Fork 0
/
LStack.cpp
125 lines (105 loc) · 2.02 KB
/
LStack.cpp
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
//LStack.cpp
// Written By Ian Reitmaier
// 02/03/2020
#include "LStack.hpp"
#include <iostream>
#include <stdexcept>
//Ctor
template <typename T>
LStack<T>::LStack()
{
//Build the LStack Here....
arraySize = 10;
//Initialize Array of ints
parr = new T [arraySize];
//Set the top to Zero
top = 0;
}
//Dtor
template <typename T>
LStack<T>::~LStack()
{
//Delete the array here
delete[] parr;
}
template <typename T>
bool LStack<T>::IsOdd(int num)
{
if(num % 2 == 0)
{
return false;
}
else
{
return true;
}
}
template <typename T>
int LStack<T>::Size()
{
return top;
}
//CTOR
template <typename T>
void LStack<T>::Push(const T& x)
{
if(top != arraySize)
{
if(top < arraySize)
{
parr[top++] = x;
}
else
{
size_t newArraysize = arraySize * 2;
T *tmparr = new T [newArraysize];
for(int i = 0; i < arraySize; i++){
tmparr[i] = parr[i];
}
delete [] parr;
parr = tmparr;
arraySize = newArraysize;
parr[top++] = x;
}
}
}
//CTOR
template <typename T>
T& LStack<T>::Pop()
{
if(top > 0)
{
return parr[--top];
}
else throw std::out_of_range("Called Pop on an empty LStack");
}
template <typename T>
const T& LStack<T>::Top()
{
if(top > 0)
{
return parr[top-1];
}
else throw std::out_of_range("Called Top() on empty LStack");
}
template <typename T>
bool LStack<T>::IsEmpty()
{
return !(top > 0);
}
template <typename T>
void LStack<T>::PrettyPrint()
{
if (top > 0)
{
std::cout << "$<" << parr[top-1] << "<-- Top" << std::endl;
for(int i = top-2; i >= 0; i--)
{
std::cout << " " << parr[(size_t)i] << std::endl;
}
}
}
template class LStack<int>;
template class LStack<float>;
template class LStack<std::string>;
//one for each type of LStack we need to use later....