-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.py
More file actions
38 lines (29 loc) · 713 Bytes
/
stack.py
File metadata and controls
38 lines (29 loc) · 713 Bytes
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
# Stack Implementation
# 1. Using List
class Stack:
def __init__(self):
self.size = 0
self.arr = []
def push(self, value):
self.size += 1
self.arr.append(value)
def pop(self):
if self.size:
self.arr.pop()
class Node:
def __init__(self, val):
self.val = val
self.next = None
class LinkedListStack:
def __init__(self):
self.head = None
def push(self, x):
node = Node(x)
node.next = self.head
self.head = node
def pop(self):
if not self.head:
raise IndexError("empty")
val = self.head.val
self.head = self.head.next
return val