-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack_implementation.py
More file actions
104 lines (81 loc) · 2.17 KB
/
stack_implementation.py
File metadata and controls
104 lines (81 loc) · 2.17 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
class Node:
def __init__(self):
self.data=None
self.next=None
def set_data(self,data):
self.data=data
def get_data(self):
return self.data
def set_next(self,next):
self.next=next
def get_next(self):
return self.next
class stack_implementation(object):
def __init__(self):
self.head=None9
self.stk=[]
'''if data:
for data in data:
self.push(data)'''
def is_Empty(self):
return len(self.stk)<=0
def push(self,data):
temp=Node()
temp.set_data(data)
temp.set_next(self.head)
self.head=temp
def pop(self):
if self.head is None:
raise IndexError
temp=self.head.get_data()
self.head=self.head.get_next()
return temp
def peek(self):
if self.head is None:
raise IndexError
return self.head.get_data()
'''def check_bal(input1):
sym=stack_implementation(0)
bal=0
for i in input1:
if i in["(","{","["]:
sym.push(i)
else:
if i.is_Empty():
bal=0
else:
ts=sym.pop()
if not matches(ts,sym):
bal=0
else:
bal=1
return bal '''
def parChecker(symbolString):
s = []
balanced = True
index = 0
print(len(symbolString))
while index < len(symbolString) and balanced:
symbol = symbolString[index]
if symbol == "(":
s.append(symbol)
elif symbol==")":
if len(s)==0:
balanced = False
else:
s.pop()
index = index + 1
if balanced and len(s)==0:
return True
else:
return False
print(parChecker("(()))"))
print(parChecker('(())'))
li=["first","second","third"]
'''
s=stack_implementation(li)
print(s.pop())
print(s.pop())d
print(s.peek())
print(check_bal("([)"))
print(check_bal("{{[][]}()}"))'''