-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathDynStack.h
124 lines (104 loc) · 2.33 KB
/
DynStack.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
// Author : XuBenHao
// Version : 1.0.0
// Mail : [email protected]
// Copyright : XuBenHao 2020 - 2030
#ifndef ALLIB_DATASTRUCT_STACK_STACK_H
#define ALLIB_DATASTRUCT_STACK_STACK_H
#include "..\..\stdafx.h"
#include "..\Array\DynArray.h"
namespace AlLib
{
namespace DataStruct
{
namespace Stack
{
template <typename T>
class DynStack
{
public:
DynStack();
DynStack(const DynStack& sA_);
DynStack& operator=(const DynStack& sA_);
virtual ~DynStack();
void Push(const T& nNewValue_);
T Pop();
void Reset();
T Peek(unsigned int nReverseIndex_ = 0) const;
bool IsEmpty() const;
int GetSize() const;
Array::DynArray<T> GetArray() const
{
return m_arrValues;
}
private:
Array::DynArray<T> m_arrValues;
};
template <typename T>
DynStack<T>::DynStack()
{
}
template <typename T>
DynStack<T>::DynStack(const DynStack& sA_)
{
m_arrValues = sA_.m_arrValues;
}
template <typename T>
typename DynStack<T>& DynStack<T>::operator=(const DynStack& sA_)
{
m_arrValues = sA_.m_arrValues;
return *this;
}
template <typename T>
DynStack<T>::~DynStack()
{
}
template <typename T>
void DynStack<T>::Push(const T& nNewValue_)
{
m_arrValues.Add(nNewValue_);
}
template <typename T>
T DynStack<T>::Pop()
{
if (IsEmpty())
{
throw "stack is empty";
}
int _nSize = m_arrValues.GetSize();
T _nTopValue = m_arrValues[_nSize - 1];
m_arrValues.DeleteByIndex(_nSize - 1);
return _nTopValue;
}
template <typename T>
void DynStack<T>::Reset()
{
m_arrValues.Reset();
}
template <typename T>
T DynStack<T>::Peek(unsigned int nReverseIndex_) const
{
if ((unsigned int)m_arrValues.GetSize() < (nReverseIndex_ + 1))
{
throw "out of size";
}
int _nSize = m_arrValues.GetSize();
if (nReverseIndex_ >= (unsigned int)_nSize)
{
throw "the position to peek is not exist";
}
return m_arrValues[_nSize - 1 - nReverseIndex_];
}
template <typename T>
bool DynStack<T>::IsEmpty() const
{
return m_arrValues.GetSize() == 0;
}
template <typename T>
int DynStack<T>::GetSize() const
{
return m_arrValues.GetSize();
}
}
}
}
#endif