-
Notifications
You must be signed in to change notification settings - Fork 0
/
stacks.py
63 lines (56 loc) · 1.56 KB
/
stacks.py
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
class Node:
def __init__(self,value):
self.value = value
self.next = None
# every operation is done in head because remove and add at head is O(1)
# where at end need to traver the whole list which cost O(n)
#LIFO
class Stack:
def __init__(self,value):
new_node =Node(value)
self.top = new_node
self.height = 1
def push(self,value):
new_node = Node(value)
if self.top is None:
self.top = new_node
self.height += 1
return True
new_node.next = self.top
self.top = new_node
self.height += 1
def Pop(self):
if self.top is None:
return None
temp = self.top
self.top = temp.next
self.height -= 1
return temp
def PrintStack(self):
temp = self.top
while temp is not None:
print(temp.value)
temp = temp.next
mynode = Stack(3)
mynode.Pop()
mynode.push(33)
mynode.push(4)
mynode.PrintStack()
def checkforparanthesis(strs):
open_paran = ['[', '{', '(']
close_paran = [']', '}', ')']
stack =[]
for i in strs:
if i in open_paran:
stack.append(i)
elif i in close_paran:
pos = close_paran.index(i)
if len(stack) > 0 and stack[len(stack)-1] == open_paran[pos]:
stack.pop()
else:
return False
if len(stack) == 0:
return True
else:
return False
print(checkforparanthesis("{[]}"))