-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStack.h
121 lines (102 loc) · 2.62 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
// -*- 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 <exception>
#include "Array.h"
/**
* @class Stack
*
* Basic stack for abitrary 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 () { }
};
/// 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.
*/
void 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);
// Reverse the array
void reverse (void);
private:
// add member variable here
// COMMENT There is no need to allocate the array on the heap. Always try to
// allocate on the stack to reduce the ccomplexity of the code.
// Response allocated on stack now
Array <T> arr;
//top of the stack
int head;
};
// include the inline files
#include "Stack.inl"
// include the source file since template class
#include "Stack.cpp"
#endif // !defined _CS507_STACK_H_