Four basic data structure - the queue

table of Contents

1. Queue

2. queue in python

 

A Queue

  - The concept: The queue is an ordered combination of terms of which one end to add a new item called the tail, remove items at one end called the head of the queue. When an element into the queue from the tail, has been moved to the first team until it reaches a next element to be removed. Recently added elements have to wait in the tail. The longest set of elements survive in the tail, this sort become FIFO, FIFO, also known as first-served basis.

  - Case: The simplest example is the queue from time to time we will participate in the column level. Movies waiting in line, waiting to close the camp in Taiwan grocery store, waiting in line at the cafeteria (we eject tray stack). Good behavior line or queue is limited because it has only one way, only one way out. Can not jump the queue, you can not leave. You have to wait for a certain amount of time to get to the front.

 

  - Application queue: Our computer lab has 30 computers and a printer networking. When students want to print their print jobs with all other print job is waiting for the "consistent." The first task is to complete the entry. If you are the last one, you must wait for all other tasks in front of you print. We will explore this interesting example in more detail later.

  In addition to the print queue, the operating system uses a plurality of different queues within a computer to control the process. What to do next is usually based on scheduling the implementation of the program as quickly as possible and as many service users queuing algorithm. In addition, when we tap the keyboard, sometimes characters appear on the screen will be delayed. This is due to the computer to do other work at the moment. Content key is placed in the queue buffer similar, such that they can be eventually displayed on the screen in the correct order.

II. Queue in python

- abstract data type definitions queues: queue abstract data types should be defined by the following structure and operation. Queue operation is as follows: 

Queue () to create a new empty queue. It takes no parameters and returns an empty queue. 
enqueue (item) to add a new item to the tail. It requires item as an argument, it does not return anything. 
dequeue () Removes an item from the head of the queue. It takes no arguments and returns the item. Queue is modified. 
isEmpty () to see if the queue is empty. It takes no arguments and returns a Boolean value. 
size () Returns the number of items in the queue. It takes no parameters and returns an integer.

 

class Queue():
    def __init__(self):
        self.items = []
    def enqueue(self,item):
        self.items.insert(0,item)
    def isEmpty(self):
        return self.items == []
    def dequeue(self):
        if self.isEmpty():
            return ''
        else:
            return self.items.pop()
    def size(self):
        return len(self.items)

 

q = Queue()
q.enqueue(1)
q.enqueue(2)
q.enqueue(3)

print(q.dequeue())
print(q.dequeue())
print(q.dequeue())
print(q.dequeue())
The results:
 . 1 
2 
. 3 
empty

 

>>> q.size()
3
>>> q.isEmpty()
False
>>> q.enqueue(8.4)
>>> q.dequeue()
4
>>> q.dequeue()
'dog'
>>> q.size()
2
View Code

 

Guess you like

Origin www.cnblogs.com/studybrother/p/11141257.html