<div class="markdown-here-wrapper" data-md-url="https://i.cnblogs.com/EditPosts.aspx?postid=10332235">
<h1 id="-" style="margin: 20px 0px 10px; padding: 0px; font-weight: bold; color: black; font-size: 24px; border-bottom: 2px solid #aaaaaa;">队列
<p style="margin: 0px 0px 1.2em !important; font-size: 16px; line-height: 1.75em; padding-right: 0.5em; padding-left: 0.5em;">队列与栈的类型很相似,但它遵循的原则是先进先出(FIFO),也就是元素插入的时候只能在该数据结构的末端,而删除只能删除最前面的元素。队列同样应用广泛,例如打印机的队列或者是一个web服务器响应请求。
<h1 id="python-" style="margin: 20px 0px 10px; padding: 0px; font-weight: bold; color: black; font-size: 24px; border-bottom: 2px solid #aaaaaa;">Python实现
<h3 id="-" style="margin: 20px 0px 10px; padding: 0px; font-weight: bold; color: black; font-size: 20px; border-bottom: 1px solid #aaaaaa;">关于队列的方法
<p style="margin: 0px 0px 1.2em !important; font-size: 16px; line-height: 1.75em; padding-right: 0.5em; padding-left: 0.5em;">作为一个队列,同样要满足一下几个方法:
<ul style="margin: 1.2em 0px; padding-left: 2em; list-style-type: square; font-size: 16px;">
<li style="margin: 0.5em 0px; font-size: 16px;">
<p style="margin: 0.5em 0px !important; font-size: 16px; line-height: 1.75em; padding-right: 0.5em; padding-left: 0.5em;">Q.enqueue(e):向队列Q的队尾添加一个元素
:
<span class="hljs-class"><span class="hljs-keyword" style="color: #ebbbff;">class <span class="hljs-title" style="color: #7285b7;">Queue<span class="hljs-params" style="color: #ffc58f;">():
<span class="hljs-string" style="color: #d1f1a9;">"""
基于循环列表的队列
"""</span>
DEFAULT_CAPACITY = <span class="hljs-number" style="color: #ffc58f;">10</span>
<span class="hljs-function" style="color: #bbdaff;"><span class="hljs-keyword" style="color: #ebbbff;">def</span> <span class="hljs-title" style="color: #7285b7;">__init__</span><span class="hljs-params" style="color: #ffc58f;">(self)</span>:</span>
self._data = [<span class="hljs-keyword" style="color: #ebbbff;">None</span>] * Queue.DEFAULT_CAPACITY
self._size = <span class="hljs-number" style="color: #ffc58f;">0</span>
self._front = <span class="hljs-number" style="color: #ffc58f;">0</span>
<span class="hljs-function" style="color: #bbdaff;"><span class="hljs-keyword" style="color: #ebbbff;">def</span> <span class="hljs-title" style="color: #7285b7;">__len__</span><span class="hljs-params" style="color: #ffc58f;">(self)</span>:</span>
<span class="hljs-keyword" style="color: #ebbbff;">return</span> self._size
<span class="hljs-function" style="color: #bbdaff;"><span class="hljs-keyword" style="color: #ebbbff;">def</span> <span class="hljs-title" style="color: #7285b7;">is_empty</span><span class="hljs-params" style="color: #ffc58f;">(self)</span>:</span>
<span class="hljs-keyword" style="color: #ebbbff;">return</span> self._size == <span class="hljs-number" style="color: #ffc58f;">0</span>
<span class="hljs-function" style="color: #bbdaff;"><span class="hljs-keyword" style="color: #ebbbff;">def</span> <span class="hljs-title" style="color: #7285b7;">first</span><span class="hljs-params" style="color: #ffc58f;">(self)</span>:</span>
<span class="hljs-keyword" style="color: #ebbbff;">if</span> self.is_empty():
<span class="hljs-keyword" style="color: #ebbbff;">raise</span> Empty(<span class="hljs-string" style="color: #d1f1a9;">'Queue is empty'</span>)
<span class="hljs-keyword" style="color: #ebbbff;">return</span> self._data[self._front]
<span class="hljs-function" style="color: #bbdaff;"><span class="hljs-keyword" style="color: #ebbbff;">def</span> <span class="hljs-title" style="color: #7285b7;">dequeue</span><span class="hljs-params" style="color: #ffc58f;">(self)</span>:</span>
<span class="hljs-keyword" style="color: #ebbbff;">if</span> self.is_empty():
<span class="hljs-keyword" style="color: #ebbbff;">raise</span> Empty(<span class="hljs-string" style="color: #d1f1a9;">'Queue is empty'</span>)
temp = self._data[self._front]
self._front = (self._front + <span class="hljs-number" style="color: #ffc58f;">1</span>) % len(self._data)
self._size -= <span class="hljs-number" style="color: #ffc58f;">1</span>
<span class="hljs-keyword" style="color: #ebbbff;">return</span> temp
<span class="hljs-function" style="color: #bbdaff;"><span class="hljs-keyword" style="color: #ebbbff;">def</span> <span class="hljs-title" style="color: #7285b7;">enqueue</span><span class="hljs-params" style="color: #ffc58f;">(self,e)</span>:</span>
<span class="hljs-keyword" style="color: #ebbbff;">if</span> self._size == len(self._data):
self._resize(<span class="hljs-number" style="color: #ffc58f;">2</span> * len(self._data))
temp = (self._front + self._size) % len(self._data)
self._data[temp] = e
self._size += <span class="hljs-number" style="color: #ffc58f;">1</span>
<span class="hljs-function" style="color: #bbdaff;"><span class="hljs-keyword" style="color: #ebbbff;">def</span> <span class="hljs-title" style="color: #7285b7;">_resize</span><span class="hljs-params" style="color: #ffc58f;">(self,cap)</span>:</span>
<span class="hljs-string" style="color: #d1f1a9;">"""
默认cap是大于原队列长度的
"""
old = self._data
self._data = [<span class="hljs-keyword" style="color: #ebbbff;">None</span>] * cap
front = self._front
<span class="hljs-keyword" style="color: #ebbbff;">for</span> i <span class="hljs-keyword" style="color: #ebbbff;">in</span> range(self._size):
self._data[i] = old[front]
front = (<span class="hljs-number" style="color: #ffc58f;">1</span> + front) % len(old)
self._front = <span class="hljs-number" style="color: #ffc58f;">0</span>
<p style="margin: 0px 0px 1.2em !important; font-size: 16px; line-height: 1.75em; padding-right: 0.5em; padding-left: 0.5em;">代码中的_data为存储数据的列表,_size为队列的长度,_front为队列首位置的索引。代码认真读一下还是非常容易理解的,主要利用了%求余的方式来判断队列的头部或者要插入元素的位置。其中的_resize方法可以改变队列的长度,并且将队列的首部放到了列表的首位置。
<h3 id="-" style="margin: 20px 0px 10px; padding: 0px; font-weight: bold; color: black; font-size: 20px; border-bottom: 1px solid #aaaaaa;">双端队列
<p style="margin: 0px 0px 1.2em !important; font-size: 16px; line-height: 1.75em; padding-right: 0.5em; padding-left: 0.5em;">双端队列其实已经不属于队列了,它既可以从左进从右出,也可以从右进从左出,这种灵活性其实使它使用的更加广泛。其实他只是在队列的基础上又做了一些增加与修改:
<p style="margin: 0px 0px 1.2em !important; font-size: 16px; line-height: 1.75em; padding-right: 0.5em; padding-left: 0.5em;">双端队列拥有的基本的方法有:
<ul style="margin: 1.2em 0px; padding-left: 2em; list-style-type: square; font-size: 16px;">
<li style="margin: 0.5em 0px; font-size: 16px;">
<p style="margin: 0.5em 0px !important; font-size: 16px; line-height: 1.75em; padding-right: 0.5em; padding-left: 0.5em;">add_first(e)
:
self._size == len(self._data):
self._resize( * len(self._data))
temp = (self._front - ) % len(self._data)
self._data[temp] = e
self._front = temp
self._size +=
<span class="hljs-function" style="color: #bbdaff;"><span class="hljs-keyword" style="color: #ebbbff;">def <span class="hljs-title" style="color: #7285b7;">delete_last<span class="hljs-params" style="color: #ffc58f;">(self):
<span class="hljs-keyword" style="color: #ebbbff;">if self.is_empty():
<span class="hljs-keyword" style="color: #ebbbff;">raise Empty(<span class="hljs-string" style="color: #d1f1a9;">'Queue is empty')
temp = self._data[(self._front + self._size - <span class="hljs-number" style="color: #ffc58f;">1) % len(self._data)]
self._size -= <span class="hljs-number" style="color: #ffc58f;">1
<span class="hljs-keyword" style="color: #ebbbff;">return temp
<p style="margin: 0px 0px 1.2em !important; font-size: 16px; line-height: 1.75em; padding-right: 0.5em; padding-left: 0.5em;">如果要使用双端队列,其实python标准库已经有现成的双端队列类供使用了,就是collections模块中的deque类。
<p style="margin: 0px 0px 1.2em !important; font-size: 16px; line-height: 1.75em; padding-right: 0.5em; padding-left: 0.5em;">deque类的常用方法有:
<ul style="margin: 1.2em 0px; padding-left: 2em; list-style-type: square; font-size: 16px;">
<li style="margin: 0.5em 0px; font-size: 16px;">
<p style="margin: 0.5em 0px !important; font-size: 16px; line-height: 1.75em; padding-right: 0.5em; padding-left: 0.5em;">len(D)
(编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|