Queuing (queue)
The queue is also a collection of sequential elements, the addition of new elements at one end of the queue, called "rear", the removal of existing elements occurs at the other end of the queue, called "Team Head" (front), and the stack is different, the queue can only insert elements at the end of the team, delete elements in the first team. The newly added element must be at the end of the queue, and the element with the longest dwell time is at the head of the team. The queue can be imagined as the front row of the Bank of the crowd, the first of the people to transact the business, the new people can only queue up in the back, until their turn so far. This is an advanced first-out (fifo,first-in-first-out) data structure.
The queue has two main operations: inserting new elements into the queue and deleting elements from the queue. The insert operation is also called the queue, and the delete operation is called the team. The queue operation inserts a new element at the end of the queue, and the team removes elements from the team header.
Another important operation of the queue is to read the elements of the team header. This operation is called PEEK (). The operation returns the team header element, but does not remove it from the queue. In addition to reading the team header element, we also want to know how many elements are stored in the queue, and you can use size () to meet that requirement.
Operations for queue queues:
Queue() defines an empty queue, no parameters, and the return value is an empty queue.
enqueue(item) joins a data item at the end of the queue, the parameter is a data item, and no return value. dequeue() removes data items from the queue header, does not require parameters, the return value is the deleted data, and the queue itself changes. isEmpty() detects if the queue is empty. No parameter, returns a Boolean value. size() returns the number of queue data items. No parameter, returns an integer. Analog Queues
classQueue:" "Analog Queues" " def __init__(self): Self.items= [] defIsEmpty (self):returnSelf.items = = [] defEnqueue (Self,item): Self.items.insert (0,item)defdequeue (self):returnSelf.items.pop ()defsize (self):returnLen (Self.items)
>>> q = Queue ()>>> q.isempty () True>>> q.enqueue ('dog ' )>>> q.enqueue (4)>>> q.isempty () False>>> q.size ()2 >>> q.enqueue (True)>>> q.size ()3>>> q.dequeue ()' dog'>>> q.size (2)
Python Data structure ——— queue