Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
Xingyue MA
xepTemplateLibrary
Commits
539b24bd
Commit
539b24bd
authored
Jun 03, 2021
by
CDK6182CHR
Browse files
2021.6.2
parent
f13126cc
Changes
4
Hide whitespace changes
Inline
Side-by-side
BST.cpp
0 → 100644
View file @
539b24bd
#include <iostream>
#include <utility>
#include <stdint.h>
/// <summary>
/// 2021.06.03
/// 标准二岔搜索树。暂不考虑数据域
/// </summary>
template
<
typename
K
>
class
BST
{
public:
struct
BSTNode
{
K
key
;
BSTNode
*
left
,
*
right
;
BSTNode
(
const
K
&
k
)
:
key
(
k
),
left
(
nullptr
),
right
(
nullptr
)
{}
};
BST
()
:
root
(
nullptr
)
{}
BST
(
const
BST
&
t
);
BST
(
BST
&&
t
);
~
BST
();
BST
&
operator
=
(
const
BST
&
t
);
BST
&
operator
=
(
BST
&&
t
);
const
BSTNode
*
get_insert_parent
(
const
K
&
key
)
const
;
const
BSTNode
*
find_parent
(
const
K
&
key
)
const
;
BSTNode
*
insert
(
const
K
&
key
);
BSTNode
*
find
(
const
K
&
key
);
inline
const
BSTNode
*
find
(
const
K
&
key
)
const
{
return
const_cast
<
BST
*>
(
this
)
->
find
(
key
);
}
BSTNode
*
copy
()
const
;
inline
const
BSTNode
*
get_root
()
const
{
return
root
;
}
private:
BSTNode
*
root
;
BSTNode
*
copy_nodes
(
const
BSTNode
*
r
)
const
;
void
destruct_node
(
BSTNode
*
r
);
const
BSTNode
*
get_insert_parent
(
const
K
&
key
,
const
BSTNode
*
n
)
const
;
const
BSTNode
*
find_parent
(
const
K
&
key
,
const
BSTNode
*
n
)
const
;
BSTNode
*
insert
(
const
K
&
key
,
BSTNode
*
n
);
BSTNode
*
find
(
const
K
&
key
,
BSTNode
*
n
);
};
template
<
typename
K
>
BST
<
K
>::
BST
(
const
BST
<
K
>&
t
)
:
root
(
t
.
copy
())
{
}
template
<
typename
K
>
BST
<
K
>::
BST
(
BST
&&
t
)
:
root
(
t
.
root
)
{
t
.
root
=
nullptr
;
}
template
<
typename
K
>
BST
<
K
>::~
BST
()
{
destruct_node
(
root
);
root
=
nullptr
;
}
template
<
typename
K
>
BST
<
K
>&
BST
<
K
>::
operator
=
(
const
BST
&
t
)
{
root
=
t
.
copy
();
return
*
this
;
}
template
<
typename
K
>
BST
<
K
>&
BST
<
K
>::
operator
=
(
BST
&&
t
)
{
root
=
t
.
root
;
t
.
root
=
nullptr
;
}
template
<
typename
K
>
const
typename
BST
<
K
>::
BSTNode
*
BST
<
K
>::
get_insert_parent
(
const
K
&
key
)
const
{
return
get_insert_parent
(
key
,
root
);
}
template
<
typename
K
>
const
typename
BST
<
K
>::
BSTNode
*
BST
<
K
>::
find_parent
(
const
K
&
key
)
const
{
return
find_parent
(
key
,
root
);
}
template
<
typename
K
>
typename
BST
<
K
>::
BSTNode
*
BST
<
K
>::
insert
(
const
K
&
key
)
{
if
(
!
root
)
{
root
=
new
BSTNode
(
key
);
return
root
;
}
return
insert
(
key
,
root
);
}
template
<
typename
K
>
typename
BST
<
K
>::
BSTNode
*
BST
<
K
>::
find
(
const
K
&
key
)
{
return
find
(
key
,
root
);
}
template
<
typename
K
>
typename
BST
<
K
>::
BSTNode
*
BST
<
K
>::
copy
()
const
{
return
copy_nodes
(
root
);
}
template
<
typename
K
>
typename
BST
<
K
>::
BSTNode
*
BST
<
K
>::
copy_nodes
(
const
BSTNode
*
r
)
const
{
if
(
!
r
)
return
nullptr
;
auto
*
n
=
new
BSTNode
(
r
->
key
);
n
->
left
=
copy_nodes
(
r
->
left
);
n
->
right
=
copy_nodes
(
r
->
right
);
return
n
;
}
template
<
typename
K
>
void
BST
<
K
>::
destruct_node
(
BSTNode
*
r
)
{
if
(
!
r
)
return
;
destruct_node
(
r
->
left
);
destruct_node
(
r
->
right
);
delete
r
;
}
\ No newline at end of file
forward_list.cpp
0 → 100644
View file @
539b24bd
/// <summary>
/// 2021.06.03 带附加头结点的单向链表
/// 参考std::forward_list 设计API
/// </summary>
template
<
typename
T
>
class
ForwardList
{
public:
struct
Node
{
T
data
;
Node
*
next
;
Node
(
const
T
&
d
)
:
data
(
d
),
next
(
nullptr
)
{}
Node
()
:
data
(),
next
(
nullptr
)
{}
};
private:
Node
head
;
public:
ForwardList
()
=
default
;
ForwardList
(
const
ForwardList
&
lst
);
ForwardList
(
ForwardList
&&
lst
);
~
ForwardList
();
ForwardList
&
operator
=
(
const
ForwardList
&
lst
);
ForwardList
&
operator
=
(
ForwardList
&&
lst
);
void
push_front
(
const
T
&
data
);
void
remove_after
(
Node
*
node
);
void
insert_after
(
Node
*
node
,
const
T
&
data
);
void
clear
();
int
get_size
()
const
;
inline
Node
*
before_begin
()
{
return
&
head
;
}
inline
const
Node
*
before_begin
()
const
{
return
&
head
;
}
inline
Node
*
begin
()
{
return
head
.
next
;
}
inline
const
Node
*
begin
()
const
{
return
head
.
next
;
}
inline
bool
empty
()
const
{
return
head
.
next
==
nullptr
;
}
//返回一个删除了指定节点的副本
ForwardList
remove_copy
(
const
Node
*
node
)
const
;
void
show
()
const
;
private:
//附加头结点
Node
*
copy_nodes
()
const
;
};
template
<
typename
T
>
ForwardList
<
T
>::
ForwardList
(
const
ForwardList
&
lst
)
{
head
.
next
=
lst
.
copy_nodes
();
}
template
<
typename
T
>
ForwardList
<
T
>::
ForwardList
(
ForwardList
&&
lst
)
{
head
.
next
=
lst
.
head
.
next
;
lst
.
head
.
next
=
nullptr
;
}
template
<
typename
T
>
ForwardList
<
T
>::~
ForwardList
()
{
clear
();
}
template
<
typename
T
>
ForwardList
<
T
>&
ForwardList
<
T
>::
operator
=
(
const
ForwardList
&
lst
)
{
head
.
next
=
lst
.
copy_nodes
();
return
*
this
;
}
template
<
typename
T
>
ForwardList
<
T
>&
ForwardList
<
T
>::
operator
=
(
ForwardList
&&
lst
)
{
head
.
next
=
lst
.
head
.
next
;
lst
.
head
.
next
=
nullptr
;
return
*
this
;
}
template
<
typename
T
>
void
ForwardList
<
T
>::
push_front
(
const
T
&
data
)
{
auto
*
node
=
new
Node
(
data
);
node
->
next
=
head
.
next
;
head
.
next
=
node
;
}
template
<
typename
T
>
void
ForwardList
<
T
>::
remove_after
(
Node
*
node
)
{
if
(
!
node
||
!
(
node
->
next
))
return
;
auto
*
tmp
=
node
->
next
;
node
->
next
=
node
->
next
->
next
;
delete
tmp
;
}
template
<
typename
T
>
void
ForwardList
<
T
>::
insert_after
(
Node
*
node
,
const
T
&
data
)
{
if
(
!
node
)
return
;
auto
*
n
=
new
Node
(
data
);
n
->
next
=
node
->
next
;
node
->
next
=
n
;
}
template
<
typename
T
>
void
ForwardList
<
T
>::
clear
()
{
if
(
!
head
.
next
)
return
;
auto
*
p0
=
head
.
next
;
auto
*
p1
=
head
.
next
->
next
;
while
(
p1
)
{
p1
=
p1
->
next
;
delete
p0
;
p0
=
p1
;
}
delete
p0
;
head
.
next
=
nullptr
;
}
template
<
typename
T
>
int
ForwardList
<
T
>::
get_size
()
const
{
auto
*
p
=
head
.
next
;
int
n
=
0
;
for
(;
p
;
p
=
p
->
next
)
n
++
;
return
n
;
}
template
<
typename
T
>
ForwardList
<
T
>
ForwardList
<
T
>::
remove_copy
(
const
Node
*
node
)
const
{
ForwardList
<
T
>
res
;
Node
*
n
=
&
(
res
.
head
);
auto
*
p
=
head
.
next
;
for
(;
p
;
p
=
p
->
next
)
{
if
(
p
==
node
)
{
p
=
p
->
next
;
if
(
!
p
)
break
;
}
n
->
next
=
new
Node
(
p
->
data
);
n
=
n
->
next
;
}
return
res
;
}
template
<
typename
T
>
void
ForwardList
<
T
>::
show
()
const
{
std
::
cout
<<
"[ "
;
for
(
auto
*
p
=
head
.
next
;
p
;
p
=
p
->
next
)
{
std
::
cout
<<
p
->
data
<<
", "
;
}
std
::
cout
<<
" ]"
<<
std
::
endl
;
}
//不包含头结点
template
<
typename
T
>
typename
ForwardList
<
T
>::
Node
*
ForwardList
<
T
>::
copy_nodes
()
const
{
if
(
!
head
.
next
)
return
nullptr
;
Node
*
n
=
new
Node
(
head
.
next
->
data
);
Node
*
n0
=
n
;
auto
*
p
=
head
.
next
->
next
;
for
(;
p
;
p
=
p
->
next
)
{
n
->
next
=
new
Node
(
p
->
data
);
n
=
n
->
next
;
}
return
n0
;
}
\ No newline at end of file
queue.cpp
0 → 100644
View file @
539b24bd
/// <summary>
/// 链式队列的最简实现
/// </summary>
template
<
typename
T
>
class
Queue
{
public:
struct
Node
{
T
data
;
Node
*
next
;
Node
()
=
delete
;
Node
(
const
T
&
data_
)
:
data
(
data_
),
next
(
nullptr
)
{}
};
private:
Node
*
head
,
*
tail
;
int
_count
;
public:
Queue
()
:
head
(
nullptr
),
tail
(
nullptr
),
_count
(
0
)
{}
~
Queue
();
Queue
(
const
Queue
&
)
=
delete
;
Queue
(
Queue
&&
)
=
delete
;
Queue
&
operator
=
(
const
Queue
&
)
=
delete
;
Queue
&
operator
=
(
Queue
&&
)
=
delete
;
void
clear
();
void
push
(
const
T
&
t
);
void
pop
();
T
&
front
();
inline
int
count
()
const
{
return
_count
;
}
inline
bool
empty
()
const
{
return
head
==
nullptr
;
}
};
template
<
typename
T
>
Queue
<
T
>::~
Queue
()
{
clear
();
}
template
<
typename
T
>
void
Queue
<
T
>::
clear
()
{
while
(
!
empty
())
pop
();
}
template
<
typename
T
>
void
Queue
<
T
>::
push
(
const
T
&
t
)
{
Node
*
p
=
new
Node
(
t
);
if
(
!
tail
)
{
//插入第一个元素
head
=
tail
=
p
;
}
else
{
tail
->
next
=
p
;
tail
=
p
;
}
_count
++
;
}
template
<
typename
T
>
void
Queue
<
T
>::
pop
()
{
if
(
!
head
)
return
;
auto
*
p
=
head
;
if
(
head
->
next
)
{
head
=
head
->
next
;
}
else
{
head
=
tail
=
nullptr
;
}
delete
p
;
_count
--
;
}
template
<
typename
T
>
T
&
Queue
<
T
>::
front
()
{
if
(
empty
())
{
throw
"EMPTY (FRONT)"
;
}
return
head
->
data
;
}
\ No newline at end of file
seqlist.cpp
0 → 100644
View file @
539b24bd
/// <summary>
/// 2021.06.03
/// 具有固定运行时常量大小的数组
/// </summary>
template
<
typename
T
>
class
SeqList
{
T
*
data
;
const
int
size
;
public:
SeqList
(
int
sz
)
:
size
(
sz
)
{
data
=
new
T
[
size
];
}
SeqList
(
const
SeqList
&
s
)
:
size
(
s
.
size
)
{
data
=
new
T
[
size
];
std
::
copy
(
s
.
data
,
s
.
data
+
size
,
data
);
}
SeqList
(
SeqList
&&
s
)
:
size
(
s
.
size
),
data
(
s
.
data
)
{
s
.
data
=
nullptr
;
}
~
SeqList
()
{
if
(
data
)
{
delete
[]
data
;
data
=
nullptr
;
}
}
inline
T
&
operator
[](
int
i
)
{
return
data
[
i
];
}
inline
const
T
&
operator
[](
int
i
)
const
{
return
const_cast
<
SeqList
*>
(
this
)
->
operator
[](
i
);
}
inline
void
fill
(
const
T
&
t
)
{
std
::
fill
(
data
,
data
+
size
,
t
);
}
inline
int
get_size
()
const
{
return
size
;
}
};
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment