SoFunction
Updated on 2024-11-20

python queue basic definition and usage [initialization, assignment, judgment, etc.]

In this article, the example tells the basic definition and use of python queue. Shared for your reference, as follows:

Queues are characterized by first-in-first-out

Application scenarios: message communication, collaboration between multiple processes, collaboration between multiple threads, etc.

Instance attributes to be designed in the queue: head node, tail node

There are two example methods that need to be designed: the inbound queue enqueue and the outbound queue dequeue, respectively.

# -*- coding:utf-8 -*-
#! python3
class Node(object):   # node, which includes two attributes, a value for the node and a point to where the node will be next
  def __init__(self,value):
     = value  The value of the # node
     = None   # node's next pointing to
class Queue(object):    #The class Queue
  def __init__(self):   # Initialize this queue
     = None   The nodes pointed to by the head and tail of the # queue are None, initialize the
     = None
  def enter(self,n):
    packNode = Node(n)   # Create a new node instance of Node with value n
    if  == None: # If the first pointer is empty
       = packNode    # Assign the node pointed to by the head to the node passed in
       =    # and assign the node pointed to by the tail to
    else:
       = packNode    # If the queue is not empty, assign the new node to the next position in the current last
       = packNode      # Then move last to point to the node you just added.
  def quit(self):
    if  == None:
      return None
    else:
      tmp =      # If a value exists in the queue, assign the first value in the queue to tmp
       =   # Change first's pointing to next to first pointing to
      return tmp
if __name__ == '__main__':
  print("------------ queue starts --------")
  q = Queue()
  # n1 = Node(1)
  # n2 = Node(2)
  # n3 = Node(3)
  (1)
  (2)
  (3)
  print(())
  print(())
  print(())
  # print(q)

Run results:

------------ queue start --------
1
2
3

Readers interested in more Python related content can check out this site's topic: thePython Data Structures and Algorithms Tutorial》、《Summary of Python encryption and decryption algorithms and techniques》、《Summary of Python coding manipulation techniques》、《Summary of Python function usage tips》、《Summary of Python string manipulation techniquesand thePython introductory and advanced classic tutorials

I hope that what I have said in this article will help you in Python programming.