-
Notifications
You must be signed in to change notification settings - Fork 0
/
LoserStack.cpp
70 lines (68 loc) · 1.79 KB
/
LoserStack.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
/*************************************************************
** Program name: LoserStack.cpp
** Author: Ben Fridkis
** Date: 5/17/2017
** Description: LoserQueue.cpp source file. Provides the
** member function definitions for the
** LoserStack class, which provides a singly-
** linked stack container to hold the losing
** creatures for fantasy battle tournament play
** (project 4).
*************************************************************/
#include "LoserStack.hpp"
/*******************************************
Destructor
LoserStack object destructor. Deallocates
all memory assigned to StackNodes and sets
all pointers null.
*********************************************/
LoserStack::~LoserStack()
{
StackNode* nodeToDelete = top;
while (nodeToDelete != nullptr)
{
top = top->next;
delete nodeToDelete->creature;
delete nodeToDelete;
nodeToDelete = top;
}
}
/*******************************************
push
Adds a new StackNode to the LoserStack
object.
*******************************************/
void LoserStack::push(Creature* loser)
{
top = new StackNode(loser, top);
}
/**********************************************
pop
Prints top node Creature specific name and
then removes top node.
***********************************************/
void LoserStack::pop()
{
if (top == nullptr)
{
throw LoserStack::Underflow();
}
else
{
string loserName = top->creature->getSName();
cout << loserName << endl;
StackNode* popped = top;
top = top->next;
delete popped->creature;
delete popped;
}
}
/*******************************************
isEmpty
Returns boolean value true to designate an
empty stack. Otherwise returns false.
*******************************************/
bool LoserStack::isEmpty() const
{
return top == nullptr;
}