Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

History

History
History
73 lines (55 loc) 路 1.63 KB

File metadata and controls

73 lines (55 loc) 路 1.63 KB
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
'''
STACK
LIFO
All push and pop operations are to/from the top of the stack
PUSH an item onto the stack
POP an item off of the stack
PEEK to get item on top of stack, without removing it
CLEAR all items from stack
STACK USE CASE
Undo Command : track which command have been executed. Pop last command off the command stack to undo it
Calling Function: Function Calling in any programming language is managed using stack
Stack Class in Python:
list
collections.deque
queue.LifoQueue
BigO of stack:
Push/Pop element -----> O(1)
Search element by value -----> O(n)
'''
''' CLASS STACK() '''
class Stack():
def __init__(self):
self.stack = list()
def push(self, item):
self.stack.append(item)
def pop(self):
if len(self.stack) > 0:
return self.stack.pop()
def peek(self):
if len(self.stack) > 0:
return self.stack[len(self.stack)-1]
else:
return None
def __str__(self):
return str(self.stack)
''' CLASS STACK() END '''
stack = list()
# .append method to append items to the stack
stack.append(1)
stack.append(4)
stack.append(9)
stack.append(16)
print(f"Stack is {stack}", end="\n\n")
stack.pop()
print(f"Deleted Last item of Stack (LIFO) {stack}", end="\n\n")
# Calling Stack() class
stack = Stack()
stack.push(1)
stack.push(2)
stack.push(3)
stack.push('B')
print(f"New Stack is {stack}")
print(f"Deleted item is {stack.pop()}")
print(f"Seek is at {stack.peek()}")
#TODO: Stack continue ( https://youtu.be/kQDxmjfkIKY?t=3077 )
Morty Proxy This is a proxified and sanitized view of the page, visit original site.