3.2 队列的操作python(5组代码)
<h1>队列初始化</h1>
<pre><code class="language-python">class my_queue:
MaxSize = 100
def __init__(self):
self.data = [None]*self.MaxSize
self.front = 0
self.rear = 0</code></pre>
<h1>入队</h1>
<pre><code class="language-python"> def enQueue(self, e):
if ((self.rear+1)%self.MaxSize == self.front):
return 0
self.rear = (self.rear+1)%self.MaxSize
self.data[self.rear] = e
return 1</code></pre>
<h1>出队</h1>
<pre><code class="language-python"> def deQueue(self):
if (self.length() &gt; 0):
self.front = (self.front+1)%self.MaxSize
return self.data[self.front]
else:
return -1</code></pre>
<h1>长度判断</h1>
<pre><code class="language-python"> def length(self):
len = (self.rear+self.MaxSize-self.front) % self.MaxSize;
return len</code></pre>
<h1>格式化输出(不重要)</h1>
<pre><code class="language-python"> def printQueue(self):
i = self.front+1
i = i%self.MaxSize
while(i!=(self.rear+1)%self.MaxSize):
print(self.data[i], end=&#039; &#039;)
i = (i+1)%self.MaxSize
print()</code></pre>
<p> </p>