stack = Stack()
stack.isEmpty()
它给出了:
AttributeError:堆栈实例没有属性'isEmpty'
和
stack.checkBalanceSymbols("SomeString")
给出:
AttributeError:堆栈实例没有属性'checkBalancedSymbols'
stack.push(someData)
,stack.pop()
和stack.peek()
正常工作。
from LLNode import Node
class Stack():
def __init__(self,data):
self.head = None
self.data = None
def push(self,data):
temp = Node()
temp.setData(data)
temp.setNext(self.head)
self.head = temp
def pop(self):
if self.head is None:
raise IndexError
else:
temp = self.head.getData()
self.head = self.head.getNext()
return temp
def peek(self):
if self.head is None:
raise IndexError
else:
return self.head.getData()
def isEmpty(self):
return self.head == None
def checkBalancedSymbols(self, input):
symbolsStack = Stack()
balanced = 0
for symbol in input:
if symbol in ["{","[","("]:
symbolsStack.push(symbol)
else:
if symbolsStack.isEmpty():
balanced = 0
return balanced
else:
topSymbol = symbolsStack.pop()
if topSymbol != symbol:
balanced = 0
return balanced
else:
balanced = 1
return balanced