-
Notifications
You must be signed in to change notification settings - Fork 0
/
Stack.cpp
66 lines (58 loc) · 1.55 KB
/
Stack.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
/**
* Stack.cpp
* Implementation of the Stack class.
*
* This file provides the implementation of the Stack class. It defines methods to
* manipulate the stack such as pushing and popping Person objects, checking if the
* stack is empty, and destroying the stack. It utilizes dynamic memory management
* to ensure proper resource allocation and deallocation.
*
* Justin Harris
* 05-16-2024
* COSC 350 - Advanced Algorithms and Data Structures
* Programming Assignment 1
* Columbia College of Missouri
*/
#include "Stack.h"
#include <iostream>
// Constructor initializes the stack
Stack::Stack() : top(nullptr) {}
// Destructor clears all nodes
Stack::~Stack() {
while (!isEmpty()) {
pop();
}
}
// Push a Person onto the stack
void Stack::push(Person p) {
Node* newNode = new Node(p);
newNode->next = top;
top = newNode;
}
// Pop a Person from the stack
Person Stack::pop() {
if (isEmpty()) {
throw std::out_of_range("Stack is empty");
}
Node* temp = top;
Person poppedPerson = top->data;
top = top->next;
delete temp;
return poppedPerson;
}
// Check if the stack is full (always false for dynamic structures)
bool Stack::isFull() const {
return false;
}
// Check if the stack is empty
bool Stack::isEmpty() const {
return top == nullptr;
}
// Print all persons in the stack
void Stack::print() {
Node* current = top;
while (current != nullptr) {
current->data.print();
current = current->next;
}
}