python基礎一 ------順序結構隊列的python實現

來源:互聯網
上載者:User

標籤:obj   queue   順序   color   先進先出   elf   pytho   fifo   python   

隊列:先進先出的線性表(FIFO),只允許在一段插入並在另一端取出

以下是python實現

 1 #-*-coding:utf-8-*- 2 #順序儲存隊列的python實現 3  4 class Queue(object): 5     def __init__(self,length): 6         self.queue = [] 7         self.length = length 8  9 10     def en_queue(self,e):11         #判斷隊列是否假滿,是則刪掉先進入的,對尾插入新的元素12         if len(self.queue)>=self.length:13             del self.queue[0]14             self.queue.append(e)15         else:   16             self.queue.append(e)17     def de_queue(self):18         if len(self.queue):19             del self.queue[0]20         else:21             print("錯誤,隊列為空白")22 23 q = Queue(4)24 q.en_queue(1)25 print(q.queue)26 27 q.en_queue(2)28 print(q.queue)29 30 q.en_queue(3)31 print(q.queue)32 33 q.en_queue(4)34 print(q.queue)35 36 37 q.en_queue(5)38 print(q.queue) 39 40 q.de_queue()41 print(q.queue) 

 

python基礎一 ------順序結構隊列的python實現

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.