-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStack.h
153 lines (126 loc) · 3.24 KB
/
Stack.h
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
#ifndef STACK_H
#define STACK_H
#include <stdexcept>
using namespace std;
class UnderflowException { };
template <class Object>
class Stack
{
public:
Stack( );
Stack( const Stack & rhs );
~Stack( );
bool isEmpty( ) const;
bool isFull( ) const;
void makeEmpty( );
void pop( );
void push( const Object & x );
Object topAndPop( );
Object & top( );
const Stack & operator=( const Stack & rhs );
private:
struct ListNode
{
Object element;
ListNode *next;
ListNode( const Object & theElement, ListNode * n = nullptr )
: element( theElement ), next( n ) { }
};
ListNode *topOfStack; // list itself is the stack
};
//////////////////////////////////////////////////////////////////////////////////////
/// Constructor, Destructor, Deep Copy, Shallow Copy(copy constructor) ////
//////////////////////////////////////////////////////////////////////////////////////
//Construct the stack
template <class Object>
Stack<Object>::Stack( )
{
topOfStack = nullptr;
}
//Deep copy
template <class Object>
const Stack<Object> & Stack<Object>::
operator=( const Stack<Object> & rhs )
{
if ( this != &rhs )
{
makeEmpty( );
if ( rhs.isEmpty( ) )
return *this;
ListNode *rptr = rhs.topOfStack;
ListNode *ptr = new ListNode( rptr->element );
topOfStack = ptr;
for ( rptr = rptr->next; rptr != nullptr; rptr = rptr->next )
ptr = ptr->next = new ListNode( rptr->element );
}
return *this;
}
// Copy constructor
template <class Object>
Stack<Object>::Stack( const Stack<Object> & rhs )
{
topOfStack = nullptr;
*this = rhs; // deep copy
}
//Destructor
template <class Object>
Stack<Object>::~Stack( )
{
makeEmpty( );
}
//////////////////////////////////////////////////////////////////////////////////////
/// Member Functions ////
//////////////////////////////////////////////////////////////////////////////////////
//Test if the stack is logically full
template <class Object>
bool Stack<Object>::isFull( ) const
{
return false;
}
//Test if the stack is logically empty
template <class Object>
bool Stack<Object>::isEmpty( ) const
{
return topOfStack == nullptr;
}
//Return the most recently inserted item in the stack or throw an exception if empty
//just gets the element, does not delete anything
template <class Object>
Object & Stack<Object>::top( )
{
if ( isEmpty( ) )
throw UnderflowException();
return topOfStack->element;
}
//Remove the most recently inserted item from the stack
template <class Object>
void Stack<Object>::pop( )
{
if ( isEmpty( ) )
throw UnderflowException();
ListNode *oldTop = topOfStack;
topOfStack = topOfStack->next;
delete oldTop;
}
//Insert x into the stack
template <class Object>
void Stack<Object>::push( const Object & x )
{
topOfStack = new ListNode( x, topOfStack );
}
//Return and remove the most recently inserted item from the stack, top and pop together
template <class Object>
Object Stack<Object>::topAndPop( )
{
Object topItem = top( );
pop( );
return topItem;
}
//Make the stack logically empty
template <class Object>
void Stack<Object>::makeEmpty( )
{
while ( ! isEmpty( ) )
pop( );
}
#endif