Queue
来源:互联网 发布:js拼接html样式丢失 编辑:程序博客网 时间:2024/05/17 06:16
The most recently added item in the queue must wait at the end of the collection. The item that has been in the collection the longest is at the front. This ordering principle is sometimes called FIFO, first-in first-out. It is also known as “first-come first-served.”
Queue() creates a new queue that is empty. It needs no parameters and returns an empty queue.
enqueue(item) adds a new item to the rear of the queue. It needs the item and returns nothing.
dequeue() removes the front item from the queue. It needs no parameters and returns the item. The queue is modified.
isEmpty() tests to see whether the queue is empty. It needs no parameters and returns a boolean value.
size() returns the number of items in the queue. It needs no parameters and returns an integer.
Implementing a Queue in Python
class Queue: def __init__(self): self.items = [] def isEmpty(self): return self.items == [] def enqueue(self, item): self.items.insert(0,item) #Insert an item at a given position. # The first argument is the index of the element before which to insert, so a.insert(0, x) # inserts at the front of the list, and a.insert(len(a), x) is equivalent to a.append(x). def dequeue(self): return self.items.pop() def size(self): return len(self.items)q=Queue()q.enqueue(4)q.enqueue('dog')q.dequeue()print(q.size())
Q-33: Suppose you have the following series of queue operations.
q = Queue()
q.enqueue(‘hello’)
q.enqueue(‘dog’)
q.enqueue(3)
q.dequeue()
What items are left on the queue?
‘dog’, 3
- Queue
- queue
- Queue
- QUEUE ~
- queue
- queue
- queue
- queue
- queue
- queue
- Queue
- Queue
- queue
- queue
- Queue
- Queue
- queue
- Queue
- 第十三天
- 在ListView中使用多样的布局
- hdoj 1528 Card Game Cheater 【贪心模拟田忌赛马】
- Android volly解析(一) 初始Volley
- getResourceAsStream和getResource的用法及Demo实例
- Queue
- c++类成员函数后边加const
- Java并发编程:Lock
- 如何学习linux
- Python ConfigParser的使用
- android周报175期
- 高级控件Listview
- 静态修饰符和主函数
- “ fatal error C1083: 无法打开包括文件:“extensions/ExtensionMacros.h”: No such file or directory”。