-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCSC162_Lab05_SBK.py
164 lines (130 loc) · 3.9 KB
/
CSC162_Lab05_SBK.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
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# sarah kingan
# CSC162
# summer 2015
# LAB05
#
# some code from Miller and Ranum (2014)
# and from lecture 3 notes
#
# http://www.codeskulptor.org/#user40_m929ZLvYFY_21.py
#
# INSTRUCTIONS: Using your own doubly linked list class
# rather than a Python listimplement a Stack.
#
# Test your stack by running the ballanced symbol checker
# example from the text.
#
# def parChecker(symbolString):
# print(parChecker('{{([][])}()}'))
# print(parChecker('[{()]'))
class DNode:
def __init__(self):
self.data = None
self.next = None
self.prev = None
def getData(self):
return self.data
def getNext(self):
return self.next
def getPrev(self):
return self.prev
def setData(self,newdata):
self.data = newdata
def setNext(self,newnext):
self.next = newnext
def setPrev(self,newprev):
self.prev = newprev
class DLinkedList:
def __init__(self):
self.head = DNode()
self.tail = DNode()
self.head.setNext(self.tail)
self.tail.setPrev(self.head)
def printForward(self):
current = self.head.getNext()
while current != self.tail:
print(current.getData())
current = current.getNext()
def printReverse(self):
current = self.tail.getPrev()
while current != self.head:
print current.getData()
current = current.getPrev()
def addAtHead(self,item):
tmp = DNode()
tmp.setData(item)
tmp.setPrev(self.head)
tmp.setNext(self.head.getNext())
self.head.getNext().setPrev(tmp)
self.head.setNext(tmp)
def addAtTail(self,item):
tmp = DNode()
tmp.setData(item)
tmp.setNext(self.tail)
tmp.setPrev(self.tail.getPrev())
self.tail.getPrev().setNext(tmp)
self.tail.setPrev(tmp)
def popHead(self):
if self.head.getNext() == self.tail:
return None
tmp = self.head.getNext()
self.head.setNext(tmp.getNext())
tmp.getNext().setPrev(self.head)
return tmp.getData()
def popTail(self):
if self.tail.getPrev() == self.head:
return None
tmp = self.tail.getPrev()
self.tail.setPrev(tmp.getPrev())
tmp.getPrev().setNext(self.tail)
return tmp.getData()
def rmNode(self,node):
node.getNext().setPrev(node.getPrev())
node.getPrev().setNext(node.getNext())
def remove(self,item):
current = self.head.getNext()
while current != self.tail:
if current.getData() == item:
self.rmNode(current)
return current.getData()
else:
current = current.getNext()
return None
class Stack:
def __init__(self):
self.dlist = DLinkedList()
def isEmpty(self):
return self.dlist.head.getNext() == self.dlist.tail
def push(self, item):
self.dlist.addAtHead(item)
def pop(self):
return self.dlist.popHead()
def peek(self):
return self.dlist.head.getNext().getData()
def size(self):
size = 0
current = self.dlist.head.getNext()
while current != self.dlist.tail:
size = size +1
current = current.getNext()
return size
def parChecker(symbolString):
s = Stack()
balanced = True
index = 0
while index < len(symbolString) and balanced:
symbol = symbolString[index]
if symbol == "(" or symbol == "{" or symbol == "[":
s.push(symbol)
else:
if s.isEmpty():
balanced = False
else:
s.pop()
index = index + 1
if balanced and s.isEmpty():
return True
else:
return False
print(parChecker('{{([][])}()}'))
print(parChecker('[{()]'))