FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
algo/java/09_queue/DynamicArrayQueue.java at master · open-source-backup/algo · GitHub
open-source-backup
/
algo
Public
forked from
wangzheng0822/algo
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
algo
/
java
/
09_queue
/
DynamicArrayQueue.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
56 lines (50 loc) · 1.32 KB
Breadcrumbs
algo
/
java
/
09_queue
/
DynamicArrayQueue.java
Copy path
File metadata and controls
56 lines (50 loc) · 1.32 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
package
queue
;
/**
* Created by wangzheng on 2018/10/9.
*/
public
class
DynamicArrayQueue
{
// 数组:items,数组大小:n
private
String
[]
items
;
private
int
n
=
0
;
// head表示队头下标,tail表示队尾下标
private
int
head
=
0
;
private
int
tail
=
0
;
// 申请一个大小为capacity的数组
public
DynamicArrayQueue
(
int
capacity
) {
items
=
new
String
[
capacity
];
n
=
capacity
;
}
// 入队操作,将item放入队尾
public
boolean
enqueue
(
String
item
) {
// tail == n表示队列末尾没有空间了
if
(
tail
==
n
) {
// tail ==n && head==0,表示整个队列都占满了
if
(
head
==
0
)
return
false
;
// 数据搬移
for
(
int
i
=
head
;
i
<
tail
; ++
i
) {
items
[
i
-
head
] =
items
[
i
];
}
// 搬移完之后重新更新head和tail
tail
-=
head
;
head
=
0
;
}
items
[
tail
] =
item
;
tail
++;
return
true
;
}
// 出队
public
String
dequeue
() {
// 如果head == tail 表示队列为空
if
(
head
==
tail
)
return
null
;
// 为了让其他语言的同学看的更加明确,把--操作放到单独一行来写了
String
ret
=
items
[
head
];
++
head
;
return
ret
;
}
public
void
printAll
() {
for
(
int
i
=
head
;
i
<
tail
; ++
i
) {
System
.
out
.
print
(
items
[
i
] +
" "
);
}
System
.
out
.
println
();
}
}
Back
|
FazBrowse Home
|
New Git URL