FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
Data-Structures-Algorithms/Arrays/QueueArray.py at master · NILESHMITTAL/Data-Structures-Algorithms · GitHub
NILESHMITTAL
/
Data-Structures-Algorithms
Public
forked from
CodersForLife/Data-Structures-Algorithms
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
Data-Structures-Algorithms
/
Arrays
/
QueueArray.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
68 lines (60 loc) · 1.94 KB
Breadcrumbs
Data-Structures-Algorithms
/
Arrays
/
QueueArray.py
Copy path
File metadata and controls
68 lines (60 loc) · 1.94 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
# Implementation of a queue using an array
# Python 3
# Aitor Alonso (https://github.com/tairosonloa)
class
QueueArray
:
def
__init__
(
self
):
self
.
items
=
[]
def
isEmpty
(
self
):
return
self
.
items
==
[]
def
enqueue
(
self
,
item
):
# We always enqueue into last position
self
.
items
.
append
(
item
)
def
dequeue
(
self
):
if
not
self
.
isEmpty
():
# We always dequeue from first position
return
self
.
items
.
pop
(
0
)
else
:
print
(
"The queue is empty. Can't dequeue."
)
return
None
def
size
(
self
):
return
len
(
self
.
items
)
def
printQueue
(
self
):
print
(
"FIRST>"
,
end
=
" "
)
for
item
in
self
.
items
:
print
(
item
,
end
=
" "
)
print
(
"<LAST"
)
if
__name__
==
"__main__"
:
# execute only if run as a script, small demostration of working, just run 'python3 QueueArray.py' in a terminal
queue
=
QueueArray
()
while
True
:
print
(
"What do you want to do?"
)
print
(
"
\t
1 - Enqueue"
)
print
(
"
\t
2 - Dequeue"
)
print
(
"
\t
3 - Check empty"
)
print
(
"
\t
4 - Check size"
)
print
(
"
\t
5 - Print queue"
)
print
(
"
\t
6 - Exit"
)
option
=
input
()
print
()
if
option
==
'1'
:
item
=
input
(
"Type your item "
)
queue
.
enqueue
(
item
)
print
(
"Item enqueued successfully!
\n
"
)
elif
option
==
'2'
:
item
=
queue
.
dequeue
()
print
(
"Dequeue item"
,
item
,
"
\n
"
)
elif
option
==
'3'
:
if
queue
.
isEmpty
():
print
(
"Queue is empty
\n
"
)
else
:
print
(
"Queue is not empty
\n
"
)
elif
option
==
'4'
:
print
(
"The queue size is"
,
queue
.
size
(),
"
\n
"
)
elif
option
==
'5'
:
queue
.
printQueue
()
print
()
elif
option
==
'6'
:
print
(
"Bye!"
)
break
else
:
print
(
"Please, choose an option between 1 and 6
\n
"
)
Back
|
FazBrowse Home
|
New Git URL