-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack brackets.py
More file actions
39 lines (38 loc) · 1.05 KB
/
stack brackets.py
File metadata and controls
39 lines (38 loc) · 1.05 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
class stackNode:
def __init__(self,data):
self.data = data
self.next = None
class stack:
def __init__(self):
self.top = None
self.brackets = {'}':'{',')':'(',']':'['}
def push(self,data):
newnode = stackNode(data)
newnode.next = self.top
self.top = newnode
def pop(self):
if(self.top.next !=None):
self.top = self.top.next
else:
self.top = None
def isempty(self):
if(self.top==None):
return True
def check(self,x):
for i in x:
if x[0] in self.brackets.values() :
if i in self.brackets.values():
obj.push(i)
elif(self.top.data == self.brackets[i]):
obj.pop()
else:
print('Not Balanced')
else:
obj.push()
print('Not Balanced')
if(isempty()):
print("Balanced")
if __name__ == '__main__':
obj = stack()
x= input()
obj.check(x)