Python数据结构与算法基础|第三期:代码实现——顺序存储队列与链式存储队列


由于队列的动态由队头指针与队尾指针共同反映,所以我们在实现先入后出的同时还要实现队头元素与队尾元素的访问。对于普通的队列,我们使用列表实现其顺序存储,使用其它方法实现其链式存储。


顺序存储

由于我们使用list作为queue的底层、用Queue类对list进行了简单封装,所以在顺序存储结构中我们可以方便的利用列表的方法。具体代码:

class Queue(object):
	'定义队列。'

	def __init__(self):
		'初始化一个空列表。'

		self.items = []

	def IsEmpty(self):
		'方法:判断队列是否为空。'

		return self.items == []

	def get_size(self):
		'方法:求队列长度。'

		return len(self.items)

	def get_TopValue(self):
		'方法:访问队头元素。'

		if self.IsEmpty():
			return None
		else:
			return self.items[0]

	def get_BottomValue(self):
		'方法:访问队尾元素。'

		if self.IsEmpty():
			return None
		else:
			return self.items[-1]

	def push(self,data):
		'方法:进队。'

		self.items.append(data)

	def pop(self):
		'方法:出队。'

		if self.IsEmpty():
			raise IndexError('队列为空。')
		else:
			self.items.pop(0)

简单测试:

my_queue = Queue()
print('Is my queue empty?\n{}'.format(my_queue.IsEmpty()))

[my_queue.push(i + 1) for i in range(10)]  
print('The top value is {}'.format(my_queue.get_TopValue()))
print('The bottom value is {}'.format(my_queue.get_BottomValue()))

for i in range(11):
	print('第{}次:'.format(i + 1))

	my_queue.pop()
	print('The top value is {}'.format(my_queue.get_TopValue()))
	print('The bottom value is {}'.format(my_queue.get_BottomValue()))

测试结果:

Is my queue empty?
True
The top value is 1
The bottom value is 101次:
The top value is 2
The bottom value is 102次:
The top value is 3
The bottom value is 103次:
The top value is 4
The bottom value is 104次:
The top value is 5
The bottom value is 105次:
The top value is 6
The bottom value is 106次:
The top value is 7
The bottom value is 107次:
The top value is 8
The bottom value is 108次:
The top value is 9
The bottom value is 109次:
The top value is 10
The bottom value is 1010次:
The top value is None
The bottom value is None11次:
Traceback (most recent call last):
  File "带链的队列.py", line 58, in <module>
    my_queue.pop()
  File "带链的队列.py", line 44, in pop
    raise IndexError('队列为空。')
IndexError: 队列为空。

链式存储

我们通过实例化一个Node类来表示节点,其中存储有数据值data和后件next,具体代码为

class Node(object):
	'定义节点。'

	def __init__(self):
		'初始化数据域和指针域。'

		self.data = None
		self.next = None

class Queue(object):
	'定义队列。'

	def __init__(self):
		'初始化队头top与队尾bottom。'

		self.top = None
		self.bottom = None
		self.size = 0

	def IsEmpty(self):
		'方法:判断是否为空队。'

		if self.size == 0:
			return True
		else:
			return False

	def get_size(self):
		'方法:得到队列大小。'

		return self.size

	def get_TopValue(self):
		'方法:访问队头元素。'

		if self.IsEmpty():
			return None
		else:
			return self.top.data

	def get_BottomValue(self):
		'方法:访问队尾元素。'

		if self.IsEmpty():
			return None
		else:
			return self.bottom.data

	def push(self,data):
		'方法:入队。'

		node = Node()
		node.data = data

		if self.IsEmpty():
			self.top = node
			self.bottom = node
		else:
			self.bottom.next = node
			self.bottom = node

		self.size += 1

	def pop(self):
		'方法:出队。'

		if not self.IsEmpty():
			self.top = self.top.next
		else:
			raise IndexError('队列为空。')
			
		self.size -= 1

简单测试:

my_queue = Queue()
print('Is my queue empty?\n{}'.format(my_queue.IsEmpty()))

[my_queue.push(i + 1) for i in range(10)]  
print('The top value is {}'.format(my_queue.get_TopValue()))
print('The bottom value is {}'.format(my_queue.get_BottomValue()))
print('The size of my queue is {}'.format(my_queue.get_size()))

for i in range(11):
	print('第{}次:'.format(i + 1))

	my_queue.pop()
	print('The top value is {}'.format(my_queue.get_TopValue()))
	print('The bottom value is {}'.format(my_queue.get_BottomValue()))
	print('The size of my queue is {}'.format(my_queue.get_size()))

测试结果:

Is my queue empty?
True
The top value is 1
The bottom value is 10
The size of my queue is 101次:
The top value is 2
The bottom value is 10
The size of my queue is 92次:
The top value is 3
The bottom value is 10
The size of my queue is 83次:
The top value is 4
The bottom value is 10
The size of my queue is 74次:
The top value is 5
The bottom value is 10
The size of my queue is 65次:
The top value is 6
The bottom value is 10
The size of my queue is 56次:
The top value is 7
The bottom value is 10
The size of my queue is 47次:
The top value is 8
The bottom value is 10
The size of my queue is 38次:
The top value is 9
The bottom value is 10
The size of my queue is 29次:
The top value is 10
The bottom value is 10
The size of my queue is 110次:
The top value is None
The bottom value is None
The size of my queue is 011次:
Traceback (most recent call last):
  File "带链的队列.py", line 84, in <module>
    my_queue.pop()
  File "带链的队列.py", line 70, in pop
    raise IndexError('队列为空。')
IndexError: 队列为空。
发布了16 篇原创文章 · 获赞 0 · 访问量 669

猜你喜欢

转载自blog.csdn.net/weixin_43628432/article/details/103929964