Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
cosmicing
Prj-Cpps
提交
125136d8
P
Prj-Cpps
项目概览
cosmicing
/
Prj-Cpps
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Prj-Cpps
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
125136d8
编写于
5月 06, 2021
作者:
cosmicing
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
上传新文件
上级
47893847
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
287 addition
and
0 deletion
+287
-0
14-Queue/11-CreateCircularDeque/13-LinkDeque/CreateCircularDequeWithHead.cpp
...ircularDeque/13-LinkDeque/CreateCircularDequeWithHead.cpp
+287
-0
未找到文件。
14-Queue/11-CreateCircularDeque/13-LinkDeque/CreateCircularDequeWithHead.cpp
0 → 100644
浏览文件 @
125136d8
#include<iostream>
#include<string>
#include<string.h>
#include<stack>
#include<assert.h>
#include<stdexcept>
using
namespace
std
;
using
std
::
ostream
;
template
<
class
T
>
class
LinkNode
{
public:
T
data
;
LinkNode
<
T
>
*
pre
;
LinkNode
<
T
>
*
next
;
LinkNode
()
:
next
(
nullptr
)
{}
LinkNode
(
const
T
&
value
)
:
data
(
value
)
{}
//这个函数和下面的函数冲突,有二义性,所以把下面的函数个给屏蔽了。
//LinkNode(const T& item, LinkNode<T> *next = nullptr) : data(item), next(next) {}
};
template
<
class
T
>
//class LinkQueue : public Queue<T> {
class
LinkQueue
{
public:
/** Initialize your data structure here. Set the size of the deque to be k. */
LinkQueue
(
int
k
);
~
LinkQueue
();
/** Adds an item at the front of Deque. Return true if the operation is successful. */
bool
insertFront
(
T
value
);
/** Adds an item at the rear of Deque. Return true if the operation is successful. */
bool
insertLast
(
T
value
);
/** Deletes an item from the front of Deque. Return true if the operation is successful. */
bool
deleteFront
();
/** Deletes an item from the rear of Deque. Return true if the operation is successful. */
bool
deleteLast
();
/** Get the front item from the deque. */
T
getFront
();
/** Get the last item from the deque. */
T
getRear
();
/** Checks whether the circular deque is empty or not. */
bool
isEmpty
();
/** Checks whether the circular deque is full or not. */
bool
isFull
();
bool
printQueue
();
private:
int
capacity
;
int
size
;
LinkNode
<
T
>
*
front
,
*
rear
;
};
/** Initialize your data structure here. Set the size of the deque to be k. */
template
<
typename
T
>
LinkQueue
<
T
>::
LinkQueue
(
int
k
)
{
size
=
0
;
capacity
=
k
;
//由于链队列只要有内存就可以插入新元素,所以这里手动设置下队列的容量;通过size是否等于capacity来判断队列是否为full;
LinkNode
<
T
>
*
head
=
new
LinkNode
<
T
>
();
//LinkNode<T> *head = new LinkNode<T>;
front
=
head
;
rear
=
head
;
//可以根据front是否等于rear来判断队列是否empty;也可以根据size的大小确认队列是否为空;:w
}
template
<
typename
T
>
LinkQueue
<
T
>::~
LinkQueue
()
{
while
(
front
!=
rear
)
{
//LinkNode<T> *p = new LinkNode<T>();
LinkNode
<
T
>
*
p
=
new
LinkNode
<
T
>
;
p
=
front
;
front
=
front
->
next
;
delete
p
;
}
delete
front
;
}
/** Adds an item at the front of Deque. Return true if the operation is successful. */
template
<
typename
T
>
bool
LinkQueue
<
T
>::
insertFront
(
T
value
)
{
if
(
isFull
())
{
return
false
;
}
LinkNode
<
T
>
*
newNode
=
new
LinkNode
<
T
>
(
value
);
newNode
->
next
=
front
->
next
;
if
(
front
->
next
!=
nullptr
)
{
front
->
next
->
pre
=
newNode
;
}
front
->
next
=
newNode
;
newNode
->
pre
=
front
;
size
++
;
return
false
;
}
/** Adds an item at the rear of Deque. Return true if the operation is successful. */
template
<
typename
T
>
bool
LinkQueue
<
T
>::
insertLast
(
T
value
)
{
if
(
isFull
())
{
return
false
;
}
LinkNode
<
T
>
*
newNode
=
new
LinkNode
<
T
>
(
value
);
newNode
->
pre
=
rear
;
if
(
rear
!=
nullptr
)
{
rear
->
next
=
newNode
;
}
rear
=
newNode
;
size
++
;
return
false
;
}
/** Deletes an item from the front of Deque. Return true if the operation is successful. */
template
<
typename
T
>
bool
LinkQueue
<
T
>::
deleteFront
()
{
if
(
isEmpty
())
{
return
false
;
}
LinkNode
<
T
>
*
p
=
front
;
front
=
front
->
next
;
if
(
front
!=
nullptr
)
{
front
->
pre
=
nullptr
;
}
delete
p
;
size
--
;
return
true
;
}
/** Deletes an item from the rear of Deque. Return true if the operation is successful. */
template
<
typename
T
>
bool
LinkQueue
<
T
>::
deleteLast
()
{
if
(
isEmpty
())
{
return
false
;
}
LinkNode
<
T
>
*
p
=
rear
;
rear
=
rear
->
pre
;
if
(
rear
!=
nullptr
)
{
rear
->
next
=
nullptr
;
}
delete
p
;
size
--
;
return
true
;
}
/** Get the front item from the deque. */
template
<
typename
T
>
T
LinkQueue
<
T
>::
getFront
()
{
if
(
isEmpty
())
{
std
::
logic_error
e
(
"The queue is empty"
);
throw
std
::
exception
(
e
);
}
return
front
->
next
->
data
;
}
/** Get the last item from the deque. */
template
<
typename
T
>
T
LinkQueue
<
T
>::
getRear
()
{
if
(
isEmpty
())
{
std
::
logic_error
e
(
"The queue is empty"
);
throw
std
::
exception
(
e
);
}
return
rear
->
data
;
}
/** Checks whether the circular deque is empty or not. */
template
<
typename
T
>
bool
LinkQueue
<
T
>::
isEmpty
()
{
if
(
front
==
rear
)
{
return
true
;
}
return
false
;
}
/** Checks whether the circular deque is full or not. */
template
<
typename
T
>
bool
LinkQueue
<
T
>::
isFull
()
{
if
(
size
==
capacity
)
{
return
true
;
}
return
false
;
}
/** traverse circular deque. */
template
<
typename
T
>
bool
LinkQueue
<
T
>::
printQueue
()
{
if
(
isEmpty
())
{
std
::
logic_error
e
(
"The queue is empty"
);
throw
std
::
exception
(
e
);
}
LinkNode
<
T
>*
p
=
front
;
//while (front != nullptr) => 这句会发生Segmentation fault
while
(
p
!=
rear
)
{
p
=
p
->
next
;
cout
<<
p
->
data
<<
endl
;
}
return
true
;
}
/**
* Your LinkQueue object will be instantiated and called as such:
* LinkQueue* obj = new LinkQueue(k);
* bool param_1 = obj->insertFront(value);
* bool param_2 = obj->insertLast(value);
* bool param_3 = obj->deleteFront();
* bool param_4 = obj->deleteLast();
* int param_5 = obj->getFront();
* int param_6 = obj->getRear();
* bool param_7 = obj->isEmpty();
* bool param_8 = obj->isFull();
*/
string
stringToString
(
string
input
)
{
assert
(
input
.
length
()
>=
2
);
string
result
;
for
(
int
i
=
1
;
i
<
input
.
length
()
-
1
;
i
++
)
{
char
currentChar
=
input
[
i
];
if
(
input
[
i
]
==
'\\'
)
{
char
nextChar
=
input
[
i
+
1
];
switch
(
nextChar
)
{
case
'\"'
:
result
.
push_back
(
'\"'
);
break
;
case
'/'
:
result
.
push_back
(
'/'
);
break
;
case
'\\'
:
result
.
push_back
(
'\\'
);
break
;
case
'b'
:
result
.
push_back
(
'\b'
);
break
;
case
'f'
:
result
.
push_back
(
'\f'
);
break
;
case
'r'
:
result
.
push_back
(
'\r'
);
break
;
case
'n'
:
result
.
push_back
(
'\n'
);
break
;
case
't'
:
result
.
push_back
(
'\t'
);
break
;
default:
break
;
}
i
++
;
}
else
{
result
.
push_back
(
currentChar
);
}
}
return
result
;
}
string
boolToString
(
bool
input
)
{
return
input
?
"True"
:
"False"
;
}
int
main
()
{
#if 0
string line;
while (getline(cin, line)) {
string s = stringToString(line);
}
#endif
LinkQueue
<
int
>
lQueue
(
100
);
lQueue
.
insertLast
(
10
);
lQueue
.
insertLast
(
20
);
lQueue
.
insertLast
(
30
);
lQueue
.
insertLast
(
40
);
lQueue
.
insertLast
(
50
);
lQueue
.
insertLast
(
60
);
lQueue
.
printQueue
();
cout
<<
"printQueue end"
<<
endl
;
cout
<<
"Queue front data = "
<<
lQueue
.
getFront
()
<<
endl
;
cout
<<
"Queue rear data = "
<<
lQueue
.
getRear
()
<<
endl
;
lQueue
.
deleteFront
();
lQueue
.
deleteFront
();
lQueue
.
printQueue
();
cout
<<
"printQueue end"
<<
endl
;
cout
<<
"Queue front data = "
<<
lQueue
.
getFront
()
<<
endl
;
cout
<<
"Queue rear data = "
<<
lQueue
.
getRear
()
<<
endl
;
lQueue
.
insertFront
(
120
);
lQueue
.
insertFront
(
90
);
lQueue
.
insertLast
(
240
);
lQueue
.
insertLast
(
360
);
lQueue
.
printQueue
();
cout
<<
"printQueue end"
<<
endl
;
cout
<<
"Queue front data = "
<<
lQueue
.
getFront
()
<<
endl
;
cout
<<
"Queue rear data = "
<<
lQueue
.
getRear
()
<<
endl
;
return
0
;
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录