-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack.h
More file actions
137 lines (114 loc) · 3.42 KB
/
Stack.h
File metadata and controls
137 lines (114 loc) · 3.42 KB
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
// -*- C++ -*-
// $Id: Stack.h 380 2010-02-08 05:10:33Z hillj $
//==============================================================================
/**
* Honor Pledge:
*
* I pledge that I have neither given nor received any help
* on this assignment.
*/
//==============================================================================
#ifndef _CS507_STACK_H_
#define _CS507_STACK_H_
#include "Array.h"
#include <exception>
/**
* @class Stack
*
* Basic stack for arbitrary elements.
*/
template <typename T>
class Stack
{
public:
/// Type definition of the type.
typedef T type;
/**
* @class empty_exception
*
* Exception thrown to indicate the stack is empty.
*/
class empty_exception : public std::exception
{
public:
/// Default constructor.
empty_exception (void)
: std::exception () { }
// this method gives an error saying that no constructor that takes in a const char * msg is defined for std::exception (line 50)
// didn't delete the code because it was pre-defined in the file
/**
* Initializing constructor.
*
* @param[in] msg Error message.
*/
//empty_exception (const char * msg)
// : std::exception (msg) { }
const char * what() const throw ()
{
return "empty_exception: The stack is empty.";
} // end what()
};
/// Default constructor.
Stack (void);
/// Copy constructor.
Stack (const Stack & s);
/// Destructor.
~Stack (void);
/**
* Assignment operator
*
* @param[in] rhs Right-hand side of operator
* @return Reference to self
*/
const Stack & operator = (const Stack & rhs);
/**
* Push a new \a element onto the stack. The element is inserted
* before all the other elements in the list.
*
* @param[in] element Element to add to the list
*/
void push (T element);
/**
* Remove the top-most element from the stack.
*
* @exception empty_exception The stack is empty.
*/
T pop (void);
/**
* Get the top-most element on the stack. If there are no element
* on the stack, then the stack_is_empty exception is thrown.
*
* @return Element on top of the stack.
* @exception empty_exception The stack is empty
*/
T top (void) const;
/**
* Test if the stack is empty
*
* @retval true The stack is empty
* @retval false The stack is not empty
*/
bool is_empty (void) const;
/**
* Number of element on the stack.
*
* @return Size of the stack.
*/
size_t size (void) const;
/// Remove all elements from the stack.
void clear (void);
private:
// COMMENT There is no need to allocate the array on the heap. Always try to
// allocate on the stack to reduce the complexity of your code.
// SOLUTION Dr. Hill, I resolved this comment by allocating the array on the stack. I included
// it on the heap initially because I thought if it would be on stack, then since the object
// will destroy itself once it is out of a particular scope, it would affect the return values.
// However, I understand how that is not true and return values are not affected.
// aggregation
Array <T> array_; // pointer that points to the data on the stack
};
// include the inline files
#include "Stack.inl"
// include the source file since template class
#include "Stack.cpp"
#endif // !defined _CS507_STACK_H_