FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
algorithms/algorithms/stack/ordered_stack.py at master · pk210700/algorithms · GitHub
pk210700
algorithms
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
algorithms
/
algorithms
/
stack
/
ordered_stack.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
33 lines (26 loc) · 1006 Bytes
Breadcrumbs
algorithms
/
algorithms
/
stack
/
ordered_stack.py
Copy path
File metadata and controls
33 lines (26 loc) · 1006 Bytes
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
#The stack remains always ordered such that the highest value is at the top and the lowest at the bottom
class
OrderedStack
:
def
__init__
(
self
):
self
.
items
=
[]
def
is_empty
(
self
):
return
self
.
items
==
[]
def
push_t
(
self
,
item
):
self
.
items
.
append
(
item
)
def
push
(
self
,
item
):
#push method to maintain order when pushing new elements
temp_stack
=
OrderedStack
()
if
self
.
is_empty
()
or
item
>
self
.
peek
():
self
.
push_t
(
item
)
else
:
while
item
<
self
.
peek
()
and
not
self
.
is_empty
():
temp_stack
.
push_t
(
self
.
pop
())
self
.
push_t
(
item
)
while
not
temp_stack
.
is_empty
():
self
.
push_t
(
temp_stack
.
pop
())
def
pop
(
self
):
if
self
.
is_empty
():
raise
IndexError
(
"Stack is empty"
)
return
self
.
items
.
pop
()
def
peek
(
self
):
return
self
.
items
[
len
(
self
.
items
)
-
1
]
def
size
(
self
):
return
len
(
self
.
items
)
Back
|
FazBrowse Home
|
New Git URL