#!/usr/bin/env python
# ---------------------------------------
# author : Geng Jie
# email  : gengjie@outlook.com
#
# Create Time: 2016/3/16 23:38
# ----------------------------------------


class Node:
	def __init__(self, value):
		self.value = value
		self.next = None


class Stack:
	def __init__(self):
		self.top = None

	def push(self, value):
		node = Node(value)
		node.next = self.top
		self.top = node

	def pop(self):
		node = self.top
		self.top = node.next
		return node.value


if __name__ == '__main__':
	stack = Stack()

	# for i in range(10):
	# 	stack.push(i)
	#
	# while stack.top:
	# 	print(stack.pop())
	exp = '{a * [x/(x+y)]}'
	for c in exp:
		if c in '{[(':
			stack.push(c)
		elif c in '}])':
			v = stack.top.value
			if c == '}' and v != '{':
				raise Exception('failed')
			if c == ']' and v != '[':
				raise Exception('failed')
			if c == ')' and v != '(':
				raise Exception('failed')
			stack.pop()

	if stack.top is not None:
		raise Exception('failed')
	print('ok')

  

相关文章:

  • 2021-10-13
  • 2022-12-23
  • 2021-08-18
  • 2021-11-15
  • 2021-09-10
  • 2021-06-09
  • 2022-03-01
  • 2022-12-23
猜你喜欢
  • 2021-09-19
  • 2021-12-26
  • 2021-10-14
  • 2022-12-23
  • 2021-12-12
  • 2022-12-23
相关资源
相似解决方案