树
0.1
数据结构_第6章
linkStack.hpp
Go to the documentation of this file.
1
/****************************************************
2
* @file linkStack.hpp
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_LINKSTACK_HPP_
16
#define INCLUDE_LINKSTACK_HPP_
17
18
#include "
Stack.h
"
19
20
namespace
Stack
21
{
22
23
template
<
typename
T>
24
class
linkStack
:
public
Stack
<T>
25
{
26
private
:
27
struct
node
28
{
29
T
data
;
30
node
*
next
;
31
32
node
(
const
T &x = T(),
node
*_next =
nullptr
)
33
:
data
(x),
next
(_next) {}
34
node
(T &&x,
node
*_next =
nullptr
)
35
:
data
(std::move(x)),
next
(_next) {}
36
37
~node
() =
default
;
38
};
39
40
node *
top_p
;
// 指向栈顶结点
41
42
public
:
43
linkStack
();
44
linkStack
(std::initializer_list<T> &&il);
// 支持列表初始化
45
virtual
bool
isEmpty
()
const
;
// 判栈空
46
virtual
void
push
(
const
T &elem);
// 进栈
47
virtual
void
push
(T &&elem);
// 进栈(move)
48
virtual
T
pop
();
// 出栈
49
virtual
T
top
()
const
;
// 读栈顶元素
50
virtual
~linkStack
();
51
};
52
53
template
<
class
T>
54
linkStack<T>::linkStack
()
55
: top_p(nullptr)
56
{
57
}
58
59
template
<
class
T>
60
linkStack<T>::linkStack
(std::initializer_list<T> &&il)
61
:
linkStack
()
62
{
63
for
(
auto
&&i : il)
64
push
(i);
65
}
66
67
template
<
class
T>
68
linkStack<T>::~linkStack
()
69
{
70
while
(
top_p
)
71
{
72
auto
tmp =
top_p
->
next
;
73
delete
top_p
;
74
top_p
= tmp;
75
}
76
}
77
78
template
<
class
T>
79
void
linkStack<T>::push
(
const
T &elem)
80
{
81
top_p
=
new
node
(elem,
top_p
);
82
}
83
84
template
<
class
T>
85
void
linkStack<T>::push
(T &&elem)
86
{
87
top_p
=
new
node(std::move(elem),
top_p
);
88
}
89
90
template
<
class
T>
91
T
linkStack<T>::pop
()
92
{
93
T retVal =
top_p
->
data
;
94
95
auto
tmp =
top_p
->
next
;
96
delete
top_p
;
97
top_p
= tmp;
98
99
return
retVal;
100
}
101
102
template
<
class
T>
103
T
linkStack<T>::top
()
const
104
{
105
return
top_p
->
data
;
106
}
107
108
template
<
class
T>
109
bool
linkStack<T>::isEmpty
()
const
110
{
111
return
!
top_p
;
112
}
113
114
}
// namespace Stack
115
116
#endif
/* INCLUDE_LINKSTACK_HPP_ */
Stack::linkStack::node::next
node * next
Definition:
linkStack.hpp:66
Stack::linkStack::node
Definition:
linkStack.hpp:51
Stack::linkStack::~linkStack
virtual ~linkStack()
Definition:
linkStack.hpp:80
Stack::linkStack::node::~node
~node()=default
Stack::linkStack::push
virtual void push(const T &elem)
Definition:
linkStack.hpp:91
Stack::linkStack::isEmpty
virtual bool isEmpty() const
Definition:
linkStack.hpp:121
Stack::linkStack::top
virtual T top() const
Definition:
linkStack.hpp:115
Stack::linkStack::linkStack
linkStack()
Definition:
linkStack.hpp:66
Stack::linkStack::pop
virtual T pop()
Definition:
linkStack.hpp:103
Stack::linkStack::node::data
T data
Definition:
linkStack.hpp:65
Stack::linkStack
Definition:
linkStack.hpp:36
Stack::linkStack::top_p
node * top_p
Definition:
linkStack.hpp:64
Stack::linkStack::node::node
node(const T &x=T(), node *_next=nullptr)
Definition:
linkStack.hpp:68
Stack
Definition:
linkStack.hpp:20
Stack.h
include
linkStack.hpp
Generated on Mon Jul 6 2020 21:04:45 for 树 by
1.8.17