-
Notifications
You must be signed in to change notification settings - Fork 0
/
Stack.cpp
121 lines (108 loc) · 3.71 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
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
// $Id: Stack.cpp 827 2011-02-07 14:20:53Z hillj $
// Honor Pledge:
//
// I pledge that I have neither given nor received any help
// on this assignment.
//
// Stack
//
template <typename T>
Stack <T>::Stack (void)
: array_ (Array <T> ())
{
} // end default constructor
//
// Stack
//
template <typename T>
Stack <T>::Stack (const Stack & stack)
: array_ (Array <T> (stack.size()))
{
// since the stack is basically the the state of the array object
// just assigning the passed in stack's array to this stack's array
array_ = stack.array_;
} // end copy constructor
//
// ~Stack
//
template <typename T>
Stack <T>::~Stack (void)
{
// nothing allocated on the heap in this class
} // end destructor
//
// push
//
template <typename T>
void Stack <T>::push (T element)
{
// COMMENT You current design of the stack is copying each element
// over one location each time your push a new element onto the stack.
// Unfortunately, this is an expensive operation—--especially if you
// have a large number of elements on the stack. Improve your design
// so that you are not copying each element every time you push a new
// element onto the stack.
// SOLUTION This method was initially a misunderstanding on my part
// about the push method. I mis-imagined the top of the stack to be
// represented by the beginning of the array. Meaning, that instead of
// adding to the top of the stack, I was adding to the bottom of the stack.
// So, I resolved this issue by adding an element to the end of the array
// representing the stack.
array_.resize (array_.size() + 1);
// size of the array, and hence the stack, are now incremented by 1
array_[array_.size() - 1] = element;
} // end push
//
// pop
//
template <typename T>
void Stack <T>::pop (void)
{
if (is_empty()) {
empty_exception ex;
throw ex;
} else {
// COMMENT You current design of the stack is copying each element
// over one location each time your pop a new element from the stack.
// Unfortunately, this is an expensive operation—--especially if you
// have a large number of elements on the stack. Improve your design
// so that you are not copying each element every time you pop a new
// element from the stack.
// SOLUTION This method was initially a misunderstanding on my part
// about the pop method. Instead of deleting the lastly added element
// from the stack, which is its top (following the LIFO access structure),
// I was deleting the first added element from the stack. Hence, Dr. Hill,
// I resolved this issue by deleting the last element from the stack's array,
// basically by resizing the stack to one size less.
array_.resize (array_.size() - 1);
} // end if-else
} // end pop
//
// operator =
//
template <typename T>
const Stack <T> & Stack <T>::operator = (const Stack & rhs)
{
// COMMENT Always check for self-assignment first before continuing.
// SOLUTION Dr. Hill, I resolved this comment by including an if branch to
// return the current object itself if it is being assigned to itself.
// COMMENT Your code will eventually crash since stacks will be sharing
// the same array allocation.
// SOLUTION Dr. Hill, I resolved this comment by allocating the array the
// stack so they don't share the same pointer values when assigning one
// array to the other.
if (this == &rhs) {
// do not modify the state of the object
} else {
array_ = rhs.array_;
} // end if-else
return *this;
} // end operator =
//
// clear
//
template <typename T>
void Stack <T>::clear (void)
{
array_.resize(0);
} // end clear