树  0.1
数据结构_第6章
seqStack.hxx
Go to the documentation of this file.
1 /****************************************************
2  * @file seqStack.hxx
3  * @author Guorui Wei (313017602@qq.com)
4  * @brief 顺序栈类的定义和实现
5  * @version 0.1
6  * @date 2020-04-05
7  *
8  * @copyright Copyright (c) 2020
9  *
10  * See the file LICENSE in the top directory of this distribution for
11  * more information.
12  *
13  ****************************************************/
14 
15 #ifndef INCLUDE_SEQSTACK_HXX_
16 #define INCLUDE_SEQSTACK_HXX_
17 
18 #include "Stack.h"
19 #include <climits>
20 
21 namespace Stack
22 {
23 
24 template <class T>
25 class seqStack : public Stack<T>
26 {
27 private:
28  T *elem; // 数组名
29  int top_p; // 栈顶元素的下标。-1表示栈空
30  int maxSize; // 数组规模
31  void doubleSpace(); // 扩展空间
32 
33 public:
34  seqStack(int initSize = 10);
35  seqStack(std::initializer_list<T> &&il); // 支持列表初始化
36  virtual bool isEmpty() const; // 判栈空
37  virtual void push(const T &elem); // 进栈
38  virtual void push(T &&elem); // 进栈(move)
39  virtual T pop(); // 出栈
40  virtual T top() const; // 读栈顶元素
41  int size() const; // Returns the number of elements in the stack.
42  int elemMem() const; // 返回动态数组的大小(Bytes)
43  virtual ~seqStack();
44  bool empty() const { return isEmpty(); }
45 };
46 
47 template <class T>
48 seqStack<T>::seqStack(int initSize)
49  : elem(new T[initSize]), top_p(-1), maxSize(initSize)
50 {
51 }
52 
53 template <class T>
54 seqStack<T>::seqStack(std::initializer_list<T> &&il)
55  : maxSize(il.size), elem(new T[il.size]), top_p(-1)
56 {
57  for (auto &&i : il)
58  elem[++top_p] = i;
59 }
60 
61 template <class T>
63 {
64  delete[] elem;
65 }
66 
67 template <class T>
69 {
70  return top_p == -1;
71 }
72 
73 template <class T>
74 void seqStack<T>::push(const T &_elem)
75 {
76  if (top_p == maxSize - 1)
77  doubleSpace();
78 
79  seqStack<T>::elem[++top_p] = _elem;
80 }
81 
82 template <class T>
83 void seqStack<T>::push(T &&_elem)
84 {
85  if (top_p == maxSize - 1)
86  doubleSpace();
87 
88  seqStack<T>::elem[++top_p] = std::move(_elem);
89 }
90 
91 template <class T>
93 {
94  return elem[top_p--];
95 }
96 
97 template <class T>
98 T seqStack<T>::top() const
99 {
100  return elem[top_p];
101 }
102 
103 template <class T>
105 {
106  T *old = elem;
107 
108  elem = new T[maxSize *= 2];
109  for (int i = 0; i <= top_p; ++i)
110  elem[i] = old[i];
111 
112  delete[] old;
113 }
114 
115 template <class T>
116 int seqStack<T>::size() const
117 {
118  return top_p + 1;
119 }
120 
121 template <class T>
122 int seqStack<T>::elemMem() const
123 {
124  return sizeof(T) * maxSize + sizeof(*this);
125 }
126 
127 } // namespace Stack
128 
129 #endif /* INCLUDE_SEQSTACK_HXX_ */
Stack::seqStack::doubleSpace
void doubleSpace()
Definition: seqStack.hxx:116
Stack::seqStack::isEmpty
virtual bool isEmpty() const
Definition: seqStack.hxx:80
Stack::seqStack::elem
T * elem
Definition: seqStack.hxx:52
Stack::seqStack::pop
virtual T pop()
Definition: seqStack.hxx:104
Stack::seqStack::top_p
int top_p
Definition: seqStack.hxx:53
Stack::seqStack::empty
bool empty() const
Definition: seqStack.hxx:68
Stack::seqStack::elemMem
int elemMem() const
Definition: seqStack.hxx:134
Stack::seqStack::seqStack
seqStack(int initSize=10)
Definition: seqStack.hxx:60
Stack::seqStack::size
int size() const
Definition: seqStack.hxx:128
Stack::seqStack::top
virtual T top() const
Definition: seqStack.hxx:110
Stack::seqStack::push
virtual void push(const T &elem)
Definition: seqStack.hxx:86
Stack
Definition: linkStack.hpp:20
Stack::seqStack::~seqStack
virtual ~seqStack()
Definition: seqStack.hxx:74
Stack.h
Stack::seqStack
Definition: seqStack.hxx:37
Stack::seqStack::maxSize
int maxSize
Definition: seqStack.hxx:54