Python data structure ----- queue

Table of contents

Foreword:

queue

Python implements queue

The complete code is as follows:

1. Create an initialization queue

2. Determine whether the queue is empty/full'

3. Join the team

 4. Dequeue

5. Get the queue head element

6. Output queue

7. Get the size of the queue

Third-party modules implement queues

Example usage:


 

Foreword:

       Queue is the most basic part of the data structure. It is a type of data structure that satisfies first-in-first-out data, just like queuing for dinner, first-come-first-served. Then I will use Python to explain the implementation of queues and related functions.

queue

        Queue is a special linear table , which is special in that it only allows deletion at the front end (front) of the table, and insert operation at the back end (rear) of the table. Like a stack, a queue is an operation subject to A linear list of constraints. The end of the insertion operation is called the tail of the queue, and the end of the deletion operation is called the head of the queue. When there are no elements in the queue, it is called an empty queue.

        The data elements of a queue are also called queue elements. Inserting a queue element into the queue is called enqueuing, and removing a queue element from the queue is called dequeuing. Because the queue is only allowed to be inserted at one end and deleted at the other end, only the earliest elements entering the queue can be deleted from the queue first, so the queue is also called FIFO—first in first out (FIFO) linear table . [1] 

Python implements queue

The complete code is as follows:

class Queue(object):
    def __init__(self,size):
        '''队列属性初始化'''
        self.__li=[]
        self.__size=size
        self.__cur=0
    def isFull(self):
        '''判断队列是否满'''
        return self.__cur==self.__size
    def isEmpty(self):
        '''判断队列是否空'''
        return self.__cur==0
    def enQueue(self,data):
        '''入队'''
        if not self.isFull():
            self.__li.append(data)
            self.__cur+=1
        else:
            print('the Queue is full')
    def deQueue(self):
        '''出队'''
        if not self.isEmpty():
            return self.__li.pop(0)
        else:
            print('the Queue is empty')
    def Queue_head(self):
        '''返回对头的元素'''
        return self.__li(0)
    def showQueue(self):
        '''输出队列全部数据'''
        print(self.__li)
    def size(self):
        '''返回队列的大小'''
        return self.__cur

1. Create an initialization queue

Create and initialize a queue, which can be initialized through the object-oriented __init__ method in Python, with the list as the container, defining the size of the queue and the cursor cur

class Queue(object):
    def __init__(self,size):
        '''队列属性初始化'''
        self.__li=[]
        self.__size=size
        self.__cur=0

2. Determine whether the queue is empty/full'

    def isFull(self):
        '''判断队列是否满'''
        return self.__cur==self.__size
    def isEmpty(self):
        '''判断队列是否空'''
        return self.__cur==0

3. Join the team

Before entering the queue, it is necessary to judge whether the queue is full. If it is full, an error message will be thrown. Otherwise, the queue is successfully entered.

    def enQueue(self,data):
        '''入队'''
        if not self.isFull():
            self.__li.append(data)
            self.__cur+=1
        else:
            print('the Queue is full')

 4. Dequeue

The same way to go out of the queue is to judge whether the queue is empty, and if it is empty, an error message will be thrown.

    def deQueue(self):
        '''出队'''
        if not self.isEmpty():
            return self.__li.pop(0)
        else:
            print('the Queue is empty')

5. Get the queue head element

    def Queue_head(self):
        '''返回对头的元素'''
        return self.__li(0)

6. Output queue

    def showQueue(self):
        '''输出队列全部数据'''
        print(self.__li)

7. Get the size of the queue

    def size(self):
        '''返回队列的大小'''
        return self.__cur

Third-party modules implement queues

import module

from queue import Queue
method meaning
Queue.put(item) Write to the queue, timeout waiting time
Queue.get([block[, timeout]]) Get queue, timeout waiting time
Queue.get_nowait() Equivalent to Queue.get(False), non-blocking method
Queue.qsize() Returns the size of the queue
Queue.empty() If the queue is empty, return True, otherwise False
Queue.full() If the queue is full, return True, otherwise False, Queue.full corresponds to the size of maxsize
Queue.task_done() After completing a piece of work, the Queue.task_done() function sends a signal to the queue that the task has completed. Each get() call gets a task, and the next task_done() call tells the queue that the task has been processed.
Queue.join() It actually means waiting until the queue is empty before performing other operations

Example usage:

from queue import Queue
if __name__ == '__main__':
    queue=Queue()
    for i in range(10):
        queue.put(i)
    print(queue.qsize())
#输出:10

Well, that's all for today, see you in the next issue!

Share a wallpaper:

 (Irena)

Guess you like

Origin blog.csdn.net/m0_73633088/article/details/130034474