FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
Java/DataStructures/Queues/LinkedQueue.java at master · loisoft/Java · GitHub
loisoft
/
Java
Public
forked from
TheAlgorithms/Java
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
Java
/
DataStructures
/
Queues
/
LinkedQueue.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
159 lines (135 loc) · 3.05 KB
Breadcrumbs
Java
/
DataStructures
/
Queues
/
LinkedQueue.java
Copy path
File metadata and controls
159 lines (135 loc) · 3.05 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package
DataStructures
.
Queues
;
import
java
.
util
.
NoSuchElementException
;
public
class
LinkedQueue
{
class
Node
{
int
data
;
Node
next
;
public
Node
() {
this
(
0
);
}
public
Node
(
int
data
) {
this
(
data
,
null
);
}
public
Node
(
int
data
,
Node
next
) {
this
.
data
=
data
;
this
.
next
=
next
;
}
}
/** Front of Queue */
private
Node
front
;
/** Rear of Queue */
private
Node
rear
;
/** Size of Queue */
private
int
size
;
/** Init LinkedQueue */
public
LinkedQueue
() {
front
=
rear
=
new
Node
();
}
/**
* Check if queue is empty
*
* @return <tt>true</tt> if queue is empty, otherwise <tt>false</tt>
*/
public
boolean
isEmpty
() {
return
size
==
0
;
}
/**
* Add element to rear of queue
*
* @param data insert value
* @return <tt>true</tt> if add successfully
*/
public
boolean
enqueue
(
int
data
) {
Node
newNode
=
new
Node
(
data
);
rear
.
next
=
newNode
;
rear
=
newNode
;
/* make rear point at last node */
size
++;
return
true
;
}
/**
* Remove element at the front of queue
*
* @return element at the front of queue
*/
public
int
dequeue
() {
if
(
isEmpty
()) {
throw
new
NoSuchElementException
(
"queue is empty"
);
}
Node
destroy
=
front
.
next
;
int
retValue
=
destroy
.
data
;
front
.
next
=
front
.
next
.
next
;
destroy
=
null
;
/* clear let GC do it's work */
size
--;
if
(
isEmpty
()) {
front
=
rear
;
}
return
retValue
;
}
/**
* Peek element at the front of queue without removing
*
* @return element at the front
*/
public
int
peekFront
() {
if
(
isEmpty
()) {
throw
new
NoSuchElementException
(
"queue is empty"
);
}
return
front
.
next
.
data
;
}
/**
* Peek element at the rear of queue without removing
*
* @return element at the front
*/
public
int
peekRear
() {
if
(
isEmpty
()) {
throw
new
NoSuchElementException
(
"queue is empty"
);
}
return
rear
.
data
;
}
/**
* Return size of queue
*
* @return size of queue
*/
public
int
size
() {
return
size
;
}
/** Clear all nodes in queue */
public
void
clear
() {
while
(!
isEmpty
()) {
dequeue
();
}
}
@
Override
public
String
toString
() {
if
(
isEmpty
()) {
return
"[]"
;
}
StringBuilder
builder
=
new
StringBuilder
();
Node
cur
=
front
.
next
;
builder
.
append
(
"["
);
while
(
cur
!=
null
) {
builder
.
append
(
cur
.
data
).
append
(
", "
);
cur
=
cur
.
next
;
}
builder
.
replace
(
builder
.
length
() -
2
,
builder
.
length
(),
"]"
);
return
builder
.
toString
();
}
/* Driver Code */
public
static
void
main
(
String
[]
args
) {
LinkedQueue
queue
=
new
LinkedQueue
();
assert
queue
.
isEmpty
();
queue
.
enqueue
(
1
);
/* 1 */
queue
.
enqueue
(
2
);
/* 1 2 */
queue
.
enqueue
(
3
);
/* 1 2 3 */
System
.
out
.
println
(
queue
);
/* [1, 2, 3] */
assert
queue
.
size
() ==
3
;
assert
queue
.
dequeue
() ==
1
;
assert
queue
.
peekFront
() ==
2
;
assert
queue
.
peekRear
() ==
3
;
queue
.
clear
();
assert
queue
.
isEmpty
();
System
.
out
.
println
(
queue
);
/* [] */
}
}
Back
|
FazBrowse Home
|
New Git URL