Python data structure 2-----queue

1. Linear structure: stack, queue, deque, list

2. Queue type: FIFO, LIFO, double-ended queue, circular queue

FIFO: first in first out

LIFO: last in first out (equivalent to stack)

Double-ended queue: The combination of LIFO and FIFO, that is, elements can be added and removed from the head and tail of the queue.

Circular queue: the head of the queue and the tail of the queue are connected (can solve false overflow)

3. The Queue module that comes with Python (import queue [python3], import Queue [Python2]) [thread is not safe and needs to be locked]

  There are three queue types: FIFO (queue.queue), LIFO (queue.Lifoqueue), priority queue (queue.Priorityqueue) [the lower the priority queue, the first to come out]

FIFO:import queue

Create an object with a queue length of 10: q=queue.queue(maxsize=10)

Insert data x at the end of the queue: q.put(x)

The team leader deletes the data: q.get(), and returns the data

Return the size of the queue: q.qsize()

Determine whether the queue is empty: q.empty(), if it is empty, return True, otherwise FALSE

Determine whether it is full: q.full echoes maxsize

 

4. Python's own double-ended queue Deque module (from collections import deque) [thread safety, because GIL]

(1) Create a new deque object: q=deque('abcdefgh'), or create a queue of size 5 q=deque(5)

(1) q.appendleft(x): insert [O(1)] on the left side of the list

(2) q.popleft(): pop up the value on the left side of the list [O(1)]

(3) extendleft: extend on the left [O(k)]

(4) q.append(x): Insert [O(1)] on the right side of the list

(5) q.pop(): pop up the value on the right side of the list [O(1)]

(6) extend: extend on the right side [O(k)]

(7) q.rotate(-n): Move n elements from the left end to the right end [O(k)]

  q.rotate(n): Move n elements from the right end to the left end

  q=deque('abcdef')

  q.rotate(-2)

  print(q)//deque(['c','d','e','f','a','b'])
  q.rotate(2)

  print(q)//deque(['e','f','a','b','c','d'])

(8) q.remove('c'): delete a specified element [O(n)]

(9) q.clear(): clears all elements of the linked list, making its length 0

(10) q.reverse(): reverse the queue

(11) q.count(x): returns the number of x in q

5. Python implements a circular queue:

The tail of the queue comes out and enters the head of the queue, and the rotate of the double-ended queue can realize a circular queue.

The following example uses the FIFO in the queue to implement a function, 6 people pass potatoes, and every time the number reaches 7, the people who have potatoes in their hands are eliminated, until there is only one person left.

#Circular queue, num must be more than the number of people 
import queue
 def hotpotato(namelist,num):
    que=queue.Queue(len(namelist))
    for name in namelist:
        que.put(name)
    while que.qsize()>1:
        for item in range(num):
            que.put(que.get())
        que.get()
    return que.get()



namelist=['a','b','c','d','e','f']
print(hotpotato(namelist,7))

 6. Lists, queues, and deques can all be used as queues. Their comparisons:

List Disadvantages: insert(0,x) is required when inserting the end of the queue, [because the head of the queue is at the end of the queue], other elements have to be moved backward, and the time complexity is large]

Disadvantages of queue: queue can only do put, and the put direction can only be at the end of the queue.

Disadvantages of deque: slow when removing(x) and fetching the index, because multiple data blocks need to be executed. list is relatively fast.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324935081&siteId=291194637