Hướng dẫn hàng đợi python

Giống như Stack, Hàng đợi là một cấu trúc dữ liệu tuyến tính lưu trữ các mục theo cách đầu tiên theo cách đầu tiên (FIFO). Với một hàng đợi, mục được thêm gần đây nhất được loại bỏ trước tiên. Một ví dụ điển hình về hàng đợi là bất kỳ hàng đợi người tiêu dùng nào cho một tài nguyên nơi người tiêu dùng đến trước được phục vụ trước. & NBSP;
 

Nội dung chính Show

  • Thực hiện
  • Làm thế nào để bạn tạo một hàng đợi đơn giản trong Python?
  • Làm cách nào để tạo hàng đợi?
  • Hàng đợi () làm gì trong Python?
  • Làm thế nào để bạn tạo một hàng đợi trong FIFO Python?

Hướng dẫn hàng đợi python

Các hoạt động liên quan đến hàng đợi là: & nbsp; & nbsp;
 

  • Enqueue: Thêm một mục vào hàng đợi. Nếu hàng đợi đầy, thì nó được cho là một điều kiện tràn - Độ phức tạp về thời gian: O (1) Adds an item to the queue. If the queue is full, then it is said to be an Overflow condition – Time Complexity : O(1)
  • Dequeue: Loại bỏ một mục khỏi hàng đợi. Các mục được xuất hiện theo cùng một thứ tự mà chúng được đẩy. Nếu hàng đợi trống, thì nó được cho là điều kiện dòng chảy - Độ phức tạp về thời gian: O (1) Removes an item from the queue. The items are popped in the same order in which they are pushed. If the queue is empty, then it is said to be an Underflow condition – Time Complexity : O(1)
  • Mặt trước: Lấy mục trước từ Hàng đợi - Độ phức tạp về thời gian: O (1) Get the front item from queue – Time Complexity : O(1)
  • Phía sau: Nhận mục cuối cùng từ Hàng đợi - Độ phức tạp về thời gian: O (1) Get the last item from queue – Time Complexity : O(1)

Thực hiện

Có nhiều cách khác nhau để thực hiện một hàng đợi trong Python. Bài viết này bao gồm việc triển khai hàng đợi bằng cách sử dụng các cấu trúc và mô -đun dữ liệu từ Thư viện Python.queue trong Python có thể được thực hiện theo các cách sau: & NBSP;
Queue in Python can be implemented by the following ways:
 

  • danh sách
  • collections.deque
  • queue.Queue

Thực hiện bằng danh sách

Danh sách là một cấu trúc dữ liệu tích hợp Python có thể được sử dụng làm hàng đợi. Thay vì enqueue () và dequeue (), hàm append () và pop () được sử dụng. Tuy nhiên, các danh sách khá chậm cho mục đích này vì việc chèn hoặc xóa một phần tử ở đầu đòi hỏi phải chuyển tất cả các phần tử khác theo một, yêu cầu thời gian O (n). & Nbsp;
 

Python3

queue = []

queue.append('a'

Traceback (most recent call last):
  File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in 
    print(queue.pop(0))
IndexError: pop from empty list



0

queue.append(

Traceback (most recent call last):
  File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in 
    print(queue.pop(0))
IndexError: pop from empty list



2
Traceback (most recent call last):
  File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in 
    print(queue.pop(0))
IndexError: pop from empty list



0

queue.append(

Traceback (most recent call last):
  File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in 
    print(queue.pop(0))
IndexError: pop from empty list



5
Traceback (most recent call last):
  File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in 
    print(queue.pop(0))
IndexError: pop from empty list



0
Traceback (most recent call last):
  File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in 
    print(queue.pop(0))
IndexError: pop from empty list



7
Traceback (most recent call last):
  File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in 
    print(queue.pop(0))
IndexError: pop from empty list



8
Traceback (most recent call last):
  File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in 
    print(queue.pop(0))
IndexError: pop from empty list



9
Traceback (most recent call last):
  File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in 
    print(queue.pop(0))
IndexError: pop from empty list



0
Traceback (most recent call last):
  File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in 
    print(queue.pop(0))
IndexError: pop from empty list



7
Initial queue
deque(['a', 'b', 'c'])

Elements dequeued from the queue
a
b
c

Queue after removing elements
deque([])



2
Traceback (most recent call last):
  File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in 
    print(queue.pop(0))
IndexError: pop from empty list



7
Traceback (most recent call last):
  File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in 
    print(queue.pop(0))
IndexError: pop from empty list



8
Initial queue
deque(['a', 'b', 'c'])

Elements dequeued from the queue
a
b
c

Queue after removing elements
deque([])



5
Traceback (most recent call last):
  File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in 
    print(queue.pop(0))
IndexError: pop from empty list



0
Traceback (most recent call last):
  File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in 
    print(queue.pop(0))
IndexError: pop from empty list



7
Initial queue
deque(['a', 'b', 'c'])

Elements dequeued from the queue
a
b
c

Queue after removing elements
deque([])



8
Initial queue
deque(['a', 'b', 'c'])

Elements dequeued from the queue
a
b
c

Queue after removing elements
deque([])



9
Traceback (most recent call last):
  File "/home/b2fa8ce438c2a9f82d6c3e5da587490f.py", line 23, in 
    q.popleft()
IndexError: pop from an empty deque



0
Traceback (most recent call last):
  File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in 
    print(queue.pop(0))
IndexError: pop from empty list



7
Initial queue
deque(['a', 'b', 'c'])

Elements dequeued from the queue
a
b
c

Queue after removing elements
deque([])



8
Initial queue
deque(['a', 'b', 'c'])

Elements dequeued from the queue
a
b
c

Queue after removing elements
deque([])



9
Traceback (most recent call last):
  File "/home/b2fa8ce438c2a9f82d6c3e5da587490f.py", line 23, in 
    q.popleft()
IndexError: pop from an empty deque



0
Traceback (most recent call last):
  File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in 
    print(queue.pop(0))
IndexError: pop from empty list



7
Initial queue
deque(['a', 'b', 'c'])

Elements dequeued from the queue
a
b
c

Queue after removing elements
deque([])



8
Initial queue
deque(['a', 'b', 'c'])

Elements dequeued from the queue
a
b
c

Queue after removing elements
deque([])



9
Traceback (most recent call last):
  File "/home/b2fa8ce438c2a9f82d6c3e5da587490f.py", line 23, in 
    q.popleft()
IndexError: pop from an empty deque



0
Traceback (most recent call last):
  File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in 
    print(queue.pop(0))
IndexError: pop from empty list



7
Traceback (most recent call last):
  File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in 
    print(queue.pop(0))
IndexError: pop from empty list



8
0

Full:  True

Elements dequeued from the queue
a
b
c

Empty:  True

Empty:  False
Full:  False



1
Traceback (most recent call last):
  File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in 
    print(queue.pop(0))
IndexError: pop from empty list



0
Traceback (most recent call last):
  File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in 
    print(queue.pop(0))
IndexError: pop from empty list



7
Initial queue
deque(['a', 'b', 'c'])

Elements dequeued from the queue
a
b
c

Queue after removing elements
deque([])



2

Output:   
 

Initial queue
['a', 'b', 'c']

Elements dequeued from queue
a
b
c

Queue after removing elements
[]




Traceback (most recent call last):
  File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in 
    print(queue.pop(0))
IndexError: pop from empty list



Thực hiện bằng cách sử dụng bộ sưu tập.deque

Hàng đợi trong Python có thể được triển khai bằng cách sử dụng lớp Deque từ mô -đun Bộ sưu tập. Deque được ưu tiên hơn danh sách trong các trường hợp chúng tôi cần các hoạt động POP và nối nhanh hơn từ cả hai đầu của container, vì Deque cung cấp độ phức tạp thời gian O (1) cho các hoạt động nối và pop so với danh sách cung cấp độ phức tạp thời gian O (n) . Thay vì enqueue và deque, các hàm append () và popleft () được sử dụng. & Nbsp;
 

Python3

0

Full:  True

Elements dequeued from the queue
a
b
c

Empty:  True

Empty:  False
Full:  False



5
0

Full:  True

Elements dequeued from the queue
a
b
c

Empty:  True

Empty:  False
Full:  False



6
0

Full:  True

Elements dequeued from the queue
a
b
c

Empty:  True

Empty:  False
Full:  False



7
0

Full:  True

Elements dequeued from the queue
a
b
c

Empty:  True

Empty:  False
Full:  False



8
0

Full:  True

Elements dequeued from the queue
a
b
c

Empty:  True

Empty:  False
Full:  False



9= queue 1

queue 2'a'

Traceback (most recent call last):
  File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in 
    print(queue.pop(0))
IndexError: pop from empty list



0

queue 2

Traceback (most recent call last):
  File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in 
    print(queue.pop(0))
IndexError: pop from empty list



2
Traceback (most recent call last):
  File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in 
    print(queue.pop(0))
IndexError: pop from empty list



0

queue 2

Traceback (most recent call last):
  File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in 
    print(queue.pop(0))
IndexError: pop from empty list



5
Traceback (most recent call last):
  File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in 
    print(queue.pop(0))
IndexError: pop from empty list



0
Traceback (most recent call last):
  File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in 
    print(queue.pop(0))
IndexError: pop from empty list



7
Traceback (most recent call last):
  File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in 
    print(queue.pop(0))
IndexError: pop from empty list



8
Traceback (most recent call last):
  File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in 
    print(queue.pop(0))
IndexError: pop from empty list



9
Traceback (most recent call last):
  File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in 
    print(queue.pop(0))
IndexError: pop from empty list



0
Traceback (most recent call last):
  File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in 
    print(queue.pop(0))
IndexError: pop from empty list



7=6
Traceback (most recent call last):
  File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in 
    print(queue.pop(0))
IndexError: pop from empty list



7
Traceback (most recent call last):
  File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in 
    print(queue.pop(0))
IndexError: pop from empty list



8=9____10
Traceback (most recent call last):
  File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in 
    print(queue.pop(0))
IndexError: pop from empty list



7[]2
Traceback (most recent call last):
  File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in 
    print(queue.pop(0))
IndexError: pop from empty list



7[]2
Traceback (most recent call last):
  File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in 
    print(queue.pop(0))
IndexError: pop from empty list



7[]2
Traceback (most recent call last):
  File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in 
    print(queue.pop(0))
IndexError: pop from empty list



7
Traceback (most recent call last):
  File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in 
    print(queue.pop(0))
IndexError: pop from empty list



8
0

Full:  True

Elements dequeued from the queue
a
b
c

Empty:  True

Empty:  False
Full:  False



1
Traceback (most recent call last):
  File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in 
    print(queue.pop(0))
IndexError: pop from empty list



0
Traceback (most recent call last):
  File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in 
    print(queue.pop(0))
IndexError: pop from empty list



7=6

Output:   
 

Initial queue
deque(['a', 'b', 'c'])

Elements dequeued from the queue
a
b
c

Queue after removing elements
deque([])



Traceback (most recent call last):
  File "/home/b2fa8ce438c2a9f82d6c3e5da587490f.py", line 23, in 
    q.popleft()
IndexError: pop from an empty deque



Thực hiện bằng cách sử dụng hàng đợi.queue

Hàng đợi được tích hợp mô-đun Python được sử dụng để thực hiện hàng đợi. Hàng đợi.queue (MaxSize) khởi tạo một biến thành kích thước tối đa của tối đa. Tối đa hóa 0 0 0 có nghĩa là một hàng đợi vô hạn. Hàng đợi này tuân theo Quy tắc FIFO. & NBSP; Có nhiều chức năng khác nhau có sẵn trong mô -đun này: & nbsp; & nbsp;
There are various functions available in this module: 
 

  • Tối đa - Số lượng các mặt hàng được phép trong hàng đợi.– Number of items allowed in the queue.
  • trống () - trả về true nếu hàng đợi trống, sai nếu không. – Return True if the queue is empty, False otherwise.
  • full () - Trả về true nếu có các mục tối đa trong hàng đợi. Nếu hàng đợi được khởi tạo với MaxSize = 0 (mặc định), thì Full () sẽ không bao giờ trả về true. – Return True if there are maxsize items in the queue. If the queue was initialized with maxsize=0 (the default), then full() never returns True.
  • Nhận () - Xóa và trả lại một mục từ hàng đợi. Nếu hàng đợi trống, hãy đợi cho đến khi có một mặt hàng. – Remove and return an item from the queue. If queue is empty, wait until an item is available.
  • get_nowait () - Trả lại một mục nếu có sẵn ngay lập tức, nếu không hãy nâng QueueEmpty. – Return an item if one is immediately available, else raise QueueEmpty.
  • Đặt (mục) - Đặt một mục vào hàng đợi. Nếu hàng đợi đã đầy, hãy đợi cho đến khi có một khe miễn phí trước khi thêm mục. – Put an item into the queue. If the queue is full, wait until a free slot is available before adding the item.
  • put_nowait (mục) - Đặt một mục vào hàng đợi mà không chặn. Nếu không có khe miễn phí có sẵn ngay lập tức, hãy nâng cao hàng đợi. – Put an item into the queue without blocking. If no free slot is immediately available, raise QueueFull.
  • Qsize () - Trả lại số lượng mục trong hàng đợi. – Return the number of items in the queue.

Python3

0

Full:  True

Elements dequeued from the queue
a
b
c

Empty:  True

Empty:  False
Full:  False



5 queue
0

Full:  True

Elements dequeued from the queue
a
b
c

Empty:  True

Empty:  False
Full:  False



7 queue.append(6
0

Full:  True

Elements dequeued from the queue
a
b
c

Empty:  True

Empty:  False
Full:  False



9= queue.append(9= 'a'1
Traceback (most recent call last):
  File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in 
    print(queue.pop(0))
IndexError: pop from empty list



0
Traceback (most recent call last):
  File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in 
    print(queue.pop(0))
IndexError: pop from empty list



7'a'4

'a'5'a'

Traceback (most recent call last):
  File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in 
    print(queue.pop(0))
IndexError: pop from empty list



0

'a'5

Traceback (most recent call last):
  File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in 
    print(queue.pop(0))
IndexError: pop from empty list



2
Traceback (most recent call last):
  File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in 
    print(queue.pop(0))
IndexError: pop from empty list



0

'a'5

Traceback (most recent call last):
  File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in 
    print(queue.pop(0))
IndexError: pop from empty list



5
Traceback (most recent call last):
  File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in 
    print(queue.pop(0))
IndexError: pop from empty list



0
Traceback (most recent call last):
  File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in 
    print(queue.pop(0))
IndexError: pop from empty list



7
Traceback (most recent call last):
  File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in 
    print(queue.pop(0))
IndexError: pop from empty list



8
Traceback (most recent call last):
  File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in 
    print(queue.pop(0))
IndexError: pop from empty list



06
Traceback (most recent call last):
  File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in 
    print(queue.pop(0))
IndexError: pop from empty list



07
Traceback (most recent call last):
  File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in 
    print(queue.pop(0))
IndexError: pop from empty list



7
Traceback (most recent call last):
  File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in 
    print(queue.pop(0))
IndexError: pop from empty list



8=9
Traceback (most recent call last):
  File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in 
    print(queue.pop(0))
IndexError: pop from empty list



0
Traceback (most recent call last):
  File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in 
    print(queue.pop(0))
IndexError: pop from empty list



7
Traceback (most recent call last):
  File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in 
    print(queue.pop(0))
IndexError: pop from empty list



13
Traceback (most recent call last):
  File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in 
    print(queue.pop(0))
IndexError: pop from empty list



7
Traceback (most recent call last):
  File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in 
    print(queue.pop(0))
IndexError: pop from empty list



13
Traceback (most recent call last):
  File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in 
    print(queue.pop(0))
IndexError: pop from empty list



7
Traceback (most recent call last):
  File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in 
    print(queue.pop(0))
IndexError: pop from empty list



13
Traceback (most recent call last):
  File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in 
    print(queue.pop(0))
IndexError: pop from empty list



7
Traceback (most recent call last):
  File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in 
    print(queue.pop(0))
IndexError: pop from empty list



8
Traceback (most recent call last):
  File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in 
    print(queue.pop(0))
IndexError: pop from empty list



20
Traceback (most recent call last):
  File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in 
    print(queue.pop(0))
IndexError: pop from empty list



21

'a'5

Traceback (most recent call last):
  File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in 
    print(queue.pop(0))
IndexError: pop from empty list



23
Traceback (most recent call last):
  File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in 
    print(queue.pop(0))
IndexError: pop from empty list



0
Traceback (most recent call last):
  File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in 
    print(queue.pop(0))
IndexError: pop from empty list



7
Traceback (most recent call last):
  File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in 
    print(queue.pop(0))
IndexError: pop from empty list



8
Traceback (most recent call last):
  File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in 
    print(queue.pop(0))
IndexError: pop from empty list



20
Traceback (most recent call last):
  File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in 
    print(queue.pop(0))
IndexError: pop from empty list



28
Traceback (most recent call last):
  File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in 
    print(queue.pop(0))
IndexError: pop from empty list



7
Traceback (most recent call last):
  File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in 
    print(queue.pop(0))
IndexError: pop from empty list



8
Traceback (most recent call last):
  File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in 
    print(queue.pop(0))
IndexError: pop from empty list



31
Traceback (most recent call last):
  File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in 
    print(queue.pop(0))
IndexError: pop from empty list



32

Output:   
 

0

Full:  True

Elements dequeued from the queue
a
b
c

Empty:  True

Empty:  False
Full:  False




Làm thế nào để bạn tạo một hàng đợi đơn giản trong Python?

Trả lời: Để tạo một hàng đợi đơn giản trong Python, hãy làm theo các bước dưới đây:..

Tạo một danh sách trống ..

Bắt đầu nối thêm các yếu tố trong danh sách được tạo ở trên ..

Sử dụng ". Chức năng nối () để thêm các phần tử như được đưa ra dưới đây ..

Làm cách nào để tạo hàng đợi?

Tạo hàng đợi..

Từ thiết lập, nhập hàng đợi trong hộp tìm nhanh, sau đó chọn hàng đợi ..

Nhấp vào Mới ..

Nhập một nhãn và tên hàng đợi.....

Chọn ai để thông báo khi các bản ghi mới được thêm vào hàng đợi ..

Nếu org của bạn sử dụng các bộ phận, hãy chọn bộ phận mặc định của hàng đợi.....

Thêm đối tượng nào sẽ bao gồm trong hàng đợi ..

Thêm thành viên hàng đợi ..

Hàng đợi () làm gì trong Python?

Giống như Stack, Hàng đợi là một cấu trúc dữ liệu tuyến tính lưu trữ các mục theo cách đầu tiên theo cách đầu tiên (FIFO).Với một hàng đợi, mục được thêm gần đây nhất được loại bỏ trước tiên.Một ví dụ điển hình về hàng đợi là bất kỳ hàng đợi người tiêu dùng nào cho một tài nguyên nơi người tiêu dùng đến trước được phục vụ trước.stores items in First In First Out (FIFO) manner. With a queue the least recently added item is removed first. A good example of queue is any queue of consumers for a resource where the consumer that came first is served first.

Làm thế nào để bạn tạo một hàng đợi trong FIFO Python?

Trong Python, hàng đợi FIFO là một cấu trúc dữ liệu tuyến tính.Nó lưu trữ các đối tượng theo cách đầu tiên theo cách đầu tiên (FIFO).Tuy nhiên, bạn cũng có thể sử dụng một danh sách thông thường như một hàng đợi FIFO ...

empty()..

full()..

get()..

get_nowait()..

put()..

put_nowait()..

qsize()..