python--栈的顺序存储和链式存储

栈stack操作:后进先出,只允许在一短进行插入删除操作,
顺序存储为顺序栈,sequential stack有栈满数组溢出问题,
链式存储linked stack没有设置头结点,data+next 栈底结点next域为null。

class SeqStack(object):
	def __init__(self,size):
		self.top = -1
		self.max_size =  size
		self.data = [None for _ in range(size)]

	def isEmpty(self):
		print self.top == -1
	def getLength(self):
		print self.top + 1

	def push(self,element):
		if self.top + 1 == self.max_size:
			print 'stack is full!'
		else:
			self.top = self.top + 1
			self.data[self.top] = element
 #pop the top element,and stack length - 1
	def pop(self):
		if self.top == -1:
			print 'stack is empty!'
		else:
			self.top = self.top  -1
			print self.data[self.top + 1]
#show the pop element
	def get_pop(self):
		if self.top == -1:
			print 'stack is empty!'
		else:
			print self.data[self.top]

	def init_stack(self):
		data = input('please input the element,enter # end:')
		while data != '#':
			self.push(data)
			data = input('please input the element,enter # end:')

	def show_stack(self):
		index = self.top
		store = []
		while index >= 0 :
			store.append(self.data[index])
			index  = index - 1
		print store

if __name__ == '__main__':
	stack = SeqStack(10)
	#stack.isEmpty()
	#stack.getLength()
	stack.init_stack()
	stack.show_stack()
	stack.push('one')
	stack.push('two')
	stack.getLength()
	stack.show_stack()
	stack.get_pop()
	stack.pop()
	stack.show_stack()

输出:
please input the element,enter # end:1
please input the element,enter # end:2
please input the element,enter # end:3
please input the element,enter # end:’#’
[3, 2, 1]
5
[‘two’, ‘one’, 3, 2, 1]
two
two
[‘one’, 3, 2, 1]

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

class LinkStack(object):
	def __init__(self):
		self.top = Node(None)
		self.num = 0

	def isEmpty(self):
		print self.num == 0

	def getLength(self):
		print self.num

	def get_pop(self):
		print self.top.data

	def push(self,element):
		temp = Node(element)
		if self.num == 0:
			self.top = temp
		else:
			temp.next = self.top
			self.top = temp
		self.num = self.num + 1

	def pop(self):
		if self.num == 0:
			print 'stack is empty!'
		else:
			self.num = self.num - 1
			old_top = self.top
			old_top_data = old_top.data
			self.top = old_top.next
			print old_top_data

	def init_stack(self):
		data = input('please input the element,enter # end:')
		while data != '#':
			self.push(data)
			data = input('please input the element,enter # end:')

	def show_stack(self):
		if self.num == 0:
			print 'stack is null !'
		else:
			count = self.num
			temp = self.top
			store = []
			while count > 0:
				store.append(temp.data)
				temp = temp.next
				count  = count - 1
			print store


if __name__ == '__main__':
	stack = LinkStack()
	stack.isEmpty()
	stack.getLength()
	stack.init_stack()
	stack.show_stack()
	stack.push('end')
	stack.getLength()
	stack.show_stack()
	stack.get_pop()
	stack.pop()
	stack.show_stack()

输出:
True
0
please input the element,enter # end:1
please input the element,enter # end:2
please input the element,enter # end:3
please input the element,enter # end:4
please input the element,enter # end:5
please input the element,enter # end:’#’
[5, 4, 3, 2, 1]
6
[‘end’, 5, 4, 3, 2, 1]
end
end
[5, 4, 3, 2, 1]

你可能感兴趣的:(Python数据结构)