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]

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章