-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack.h
More file actions
200 lines (146 loc) · 4.21 KB
/
Stack.h
File metadata and controls
200 lines (146 loc) · 4.21 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// Author: Jerome Richards
// Stack
/*---------------------------------------------------------------
| The class is a replication of the Stack class in C++ and is a |
| demonstration of employing my logic to recreate it. |
---------------------------------------------------------------*/
#include <iostream>
template <class type>
class Stack {
private:
Stack* head;
Stack* next;
Stack* end;
type* content; // The actual content of the element
int length; // number of elements in the stack
public:
Stack();
void Push(type&); // Push element if paramter is lvalue
void Push(type&&); // Push element if paramter is rvalue
void Pop(); // Pops element from top of the stack
void Traverse(); // Lists all elements in the stack
type& getElementAt(int); // Returns element at index
int getLength(); // Returns the number of elements in the stack
};
/*-----------------------------------------------------
| The constructor initializes all pointers to nullptr |
-----------------------------------------------------*/
template <class type>
Stack<type>::Stack()
{
head = nullptr;
next = nullptr;
end = nullptr;
content = nullptr;
length = 0;
}
/*-----------------------------------------------------------------------
| Receives an lvalue parameter and adds element to the top of the stack |
-----------------------------------------------------------------------*/
template <class type>
void Stack<type>::Push(type& s)
{
if (head == nullptr)
{
head = new Stack();
head->content = &s;
end = head;
}
else
{
Stack* temp = head;
head = new Stack();
head->next = temp;
head->content = &s;
}
length++;
}
/*-----------------------------------------------------------------------
| Receives an rvalue parameter and adds element to the top of the stack |
-----------------------------------------------------------------------*/
template <class type>
void Stack<type>::Push(type&& s)
{
if (head == nullptr)
{
head = new Stack();
head->content = new type;
*head->content = s;
end = head;
}
else
{
Stack* temp = head;
head = new Stack();
head->next = temp;
head->content = new type;
*head->content = s;
}
length++;
}
/*----------------------------------------
| Removes the top element from the stack |
----------------------------------------*/
template <class type>
void Stack<type>::Pop()
{
if (head != nullptr)
{
Stack* temp = head;
head = head->next;
delete temp;
length--;
}
else
{
std::cout << "There stack is already empty" << std::endl;
}
}
/*-------------------------------------------------------------------------------
| Lists all elements in the stack starting from the top and going to the bottom |
-------------------------------------------------------------------------------*/
/*--- IMPORTANT ---*/
//
// This particular transverse function assumes the content is a primitive data type - int, float, double, string, etc.,
// and can print the content without issue. However, if the stack contains class objects or structs then the function
// will need to be modified in order to print out the desired attributes of the content.
//
template <class type>
void Stack<type>::Traverse()
{
if (head != nullptr)
{
Stack* temp = head;
while (temp != end)
{
std::cout << *temp->content << std::endl;
temp = temp->next;
}
std::cout << *temp->content << std::endl;
}
else
{
std::cout << "The stack is empty" << std::endl;
}
}
/*------------------------------------------------------------------------------
| Returns element at the specified index where the top of the stack is index 0 |
------------------------------------------------------------------------------*/
template <class type>
type& Stack<type>::getElementAt(int n)
{
Stack* temp = head;
for (int i = 0; i < n; i++)
{
temp = temp->next;
}
return *temp->content;
}
/*---------------------------------------------
| Returns the number of elements in the stack |
---------------------------------------------*/
template <class type>
int Stack<type>::getLength()
{
return length;
}