-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLabCycle2-2.cpp
62 lines (55 loc) · 1.27 KB
/
LabCycle2-2.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
#include "LabCycle2-2.h"
template <typename T>
StackLinkedList<T>::StackLinkedList() {
top = nullptr;
}
template <typename T>
StackLinkedList<T>::~StackLinkedList() {
Node<T>* temp;
while (top != nullptr) {
temp = top;
top = top->next;
delete temp;
}
}
template <typename T>
void StackLinkedList<T>::push(T element) {
Node<T>* newNode = new Node<T>;
newNode->data = element;
newNode->next = top;
top = newNode;
}
template <typename T>
T StackLinkedList<T>::pop() {
if (isEmpty()) {
cout << "Stack Underflow!\n";
return T();
}
T poppedElement = top->data;
Node<T>* temp = top;
top = top->next;
delete temp;
return poppedElement;
}
template <typename T>
bool StackLinkedList<T>::isEmpty() {
return top == nullptr;
}
template <typename T>
bool StackLinkedList<T>::isFull() {
return false;
}
template <typename T>
void StackLinkedList<T>::display() {
if (isEmpty()) {
cout << "Stack is empty.\n";
return;
}
cout << "Stack elements: ";
Node<T>* current = top;
while (current != nullptr) {
cout << current->data << " ";
current = current->next;
}
cout << endl;
}