-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCustomStack.py
More file actions
60 lines (45 loc) · 1.1 KB
/
Copy pathCustomStack.py
File metadata and controls
60 lines (45 loc) · 1.1 KB
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
"""
Implementation of stack
push(x) to insert x into stack
pop() to return top most element
peek() to print top most element
isEmpty to check if stack is empty or not
size() to print the size of the stack
**/
"""
class Node(object):
def __init__(self, value):
self.value = value
self.next = None
class CustomStack(object):
def __init__(self):
self.head = None
self.size = 0
def push(self, value):
curr = Node(value)
curr.next = self.head
self.head = curr
self.size += 1
def pop(self):
ret = None
if self.head != None:
ret = self.head.value
self.head = self.head.next
self.size-= 1
return ret
def peek(self):
ret = None
if self.head != None:
ret = self.head.value
return ret
def size(self):
return self.size
if __name__=="__main__":
cs = CustomStack()
cs.push(4)
cs.push(3)
assert cs.peek() == 3
cs.push(2)
assert cs.pop() == 2
assert cs.pop() == 3
assert cs.pop() == 4