-
Notifications
You must be signed in to change notification settings - Fork 0
/
Stack.cpp
79 lines (73 loc) · 1.29 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
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <iostream>
#include "Stack.h"
using namespace std;
CharStack::CharStack()
{
top = NULL;
}
CharStack::~CharStack()
{
StackNode *nodePtr, *nextNode;
// position nodePtr at the top of the stack
nodePtr = top;
// traverse the list deleting each node
while (nodePtr != NULL)
{
nextNode = nodePtr->next;
delete nodePtr;
nodePtr = nextNode;
}
}
void CharStack::push(char item)
{
StackNode *newNode; // pointer to a new node
// allocate a new node and store num there.
newNode = new StackNode;
newNode->value = item;
// if there are no nodes in the list
// make newNode the first node.
if (isEmpty())
{
top = newNode;
newNode->next = NULL;
}
else // otherwise, insert NewNode before top
{
newNode->next = top;
top = newNode;
}
}
bool CharStack::pop(char &item)
{
StackNode *temp; // temporary pointer
// first make sure the stack isn't empty
if (isEmpty())
{
return false;
}
else // pop value off top of the stack
{
item = top->value;
temp = top->next;
delete top;
top = temp;
return true;
}
}
bool CharStack::isEmpty(void)
{
bool status;
if (!top)
{
status = true;
}
else
{
status = false;
}
return status;
}
char CharStack::getTop()
{
return top->value;
}