FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
pygorithm/pygorithm/data_structures/queue.py at master · OmkarPathak/pygorithm · GitHub
OmkarPathak
/
pygorithm
Public
Notifications
You must be signed in to change notification settings
Fork
504
Star
4.4k
Code
Issues
3
Pull requests
6
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
pygorithm
/
pygorithm
/
data_structures
/
queue.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
142 lines (122 loc) · 3.09 KB
Breadcrumbs
pygorithm
/
pygorithm
/
data_structures
/
queue.py
Copy path
File metadata and controls
142 lines (122 loc) · 3.09 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
"""
Author: OMKAR PATHAK
Created On: 3rd August 2017
"""
import
inspect
class
Queue
(
object
):
"""Queue
Queue implementation
"""
def
__init__
(
self
,
limit
=
10
):
"""
:param limit: Queue limit size, default @ 10
"""
self
.
queue
=
[]
self
.
front
=
None
self
.
rear
=
None
self
.
limit
=
limit
self
.
size
=
0
def
__str__
(
self
):
return
' '
.
join
([
str
(
i
)
for
i
in
self
.
queue
])
def
size
(
self
):
"""
returns the current size of the queue
"""
return
self
.
size
def
is_empty
(
self
):
"""
checks if the queue is empty
"""
return
self
.
size
<=
0
def
enqueue
(
self
,
data
):
"""
inserts an item into the queue
"""
if
self
.
size
>=
self
.
limit
:
# queue overflow
return
-
1
else
:
self
.
queue
.
append
(
data
)
# assign the rear as size of the queue and front as 0
if
self
.
front
is
None
:
self
.
front
=
self
.
rear
=
0
else
:
self
.
rear
=
self
.
size
self
.
size
+=
1
def
dequeue
(
self
):
"""
pops an item from the queue which was first inserted
"""
if
self
.
is_empty
():
# queue underflow
return
-
1
else
:
self
.
size
-=
1
if
self
.
size
==
0
:
self
.
front
=
self
.
rear
=
0
else
:
self
.
rear
=
self
.
size
-
1
return
self
.
queue
.
pop
(
0
)
def
get_code
(
self
):
"""
Return source code for Queue class
:return:
"""
return
inspect
.
getsource
(
Queue
)
class
Deque
(
object
):
"""Deque
Deque implementation
"""
def
__init__
(
self
,
limit
=
10
):
self
.
queue
=
[]
self
.
limit
=
limit
def
__str__
(
self
):
return
' '
.
join
([
str
(
i
)
for
i
in
self
.
queue
])
def
is_empty
(
self
):
"""
checks whether the deque is empty
"""
return
len
(
self
.
queue
)
<=
0
def
is_full
(
self
):
"""
checks whether the deque is full
"""
return
len
(
self
.
queue
)
>=
self
.
limit
def
insert_rear
(
self
,
data
):
"""
inserts an element at the rear end of the deque
"""
if
self
.
is_full
():
return
else
:
self
.
queue
.
insert
(
0
,
data
)
def
insert_front
(
self
,
data
):
"""
inserts an element at the front end of the deque
"""
if
self
.
is_full
():
return
-
1
else
:
self
.
queue
.
append
(
data
)
def
delete_rear
(
self
):
"""
deletes an element from the rear end of the deque
"""
if
self
.
is_empty
():
return
-
1
else
:
return
self
.
queue
.
pop
(
0
)
def
delete_front
(
self
):
"""
deletes an element from the front end of the deque
"""
if
self
.
is_full
():
return
else
:
return
self
.
queue
.
pop
()
@
staticmethod
def
get_code
():
"""
returns the code of the current class
"""
return
inspect
.
getsource
(
Deque
)
Back
|
FazBrowse Home
|
New Git URL