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;
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 list0
queue.append[
Traceback [most recent call last]: File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print[queue.pop[0]] IndexError: pop from empty list2
Traceback [most recent call last]: File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print[queue.pop[0]] IndexError: pop from empty list0
queue.append[
Traceback [most recent call last]: File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print[queue.pop[0]] IndexError: pop from empty list5
Traceback [most recent call last]: File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print[queue.pop[0]] IndexError: pop from empty list0
Traceback [most recent call last]: File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print[queue.pop[0]] IndexError: pop from empty list7
Traceback [most recent call last]: File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print[queue.pop[0]] IndexError: pop from empty list8
Traceback [most recent call last]: File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print[queue.pop[0]] IndexError: pop from empty list9
Traceback [most recent call last]: File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print[queue.pop[0]] IndexError: pop from empty list0
Traceback [most recent call last]: File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print[queue.pop[0]] IndexError: pop from empty list7
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 list7
Traceback [most recent call last]: File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print[queue.pop[0]] IndexError: pop from empty list8
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 list0
Traceback [most recent call last]: File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print[queue.pop[0]] IndexError: pop from empty list7
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 deque0
Traceback [most recent call last]: File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print[queue.pop[0]] IndexError: pop from empty list7
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 deque0
Traceback [most recent call last]: File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print[queue.pop[0]] IndexError: pop from empty list7
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 deque0
Traceback [most recent call last]: File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print[queue.pop[0]] IndexError: pop from empty list7
Traceback [most recent call last]: File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print[queue.pop[0]] IndexError: pop from empty list8
0 Full: True Elements dequeued from the queue a b c Empty: True Empty: False Full: False1
Traceback [most recent call last]: File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print[queue.pop[0]] IndexError: pop from empty list0
Traceback [most recent call last]: File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print[queue.pop[0]] IndexError: pop from empty list7
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: False5
0 Full: True Elements dequeued from the queue a b c Empty: True Empty: False Full: False6
0 Full: True Elements dequeued from the queue a b c Empty: True Empty: False Full: False7
0 Full: True Elements dequeued from the queue a b c Empty: True Empty: False Full: False8
0 Full: True Elements dequeued from the queue a b c Empty: True Empty: False Full: False9
=
queue
1queue
2'a'
Traceback [most recent call last]: File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print[queue.pop[0]] IndexError: pop from empty list0
queue
2
Traceback [most recent call last]: File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print[queue.pop[0]] IndexError: pop from empty list2
Traceback [most recent call last]: File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print[queue.pop[0]] IndexError: pop from empty list0
queue
2
Traceback [most recent call last]: File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print[queue.pop[0]] IndexError: pop from empty list5
Traceback [most recent call last]: File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print[queue.pop[0]] IndexError: pop from empty list0
Traceback [most recent call last]: File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print[queue.pop[0]] IndexError: pop from empty list7
Traceback [most recent call last]: File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print[queue.pop[0]] IndexError: pop from empty list8
Traceback [most recent call last]: File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print[queue.pop[0]] IndexError: pop from empty list9
Traceback [most recent call last]: File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print[queue.pop[0]] IndexError: pop from empty list0
Traceback [most recent call last]: File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print[queue.pop[0]] IndexError: pop from empty list7
=
6Traceback [most recent call last]: File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print[queue.pop[0]] IndexError: pop from empty list7
Traceback [most recent call last]: File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print[queue.pop[0]] IndexError: pop from empty list8
=
9____10Traceback [most recent call last]: File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print[queue.pop[0]] IndexError: pop from empty list7
[]
2Traceback [most recent call last]: File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print[queue.pop[0]] IndexError: pop from empty list7
[]
2Traceback [most recent call last]: File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print[queue.pop[0]] IndexError: pop from empty list7
[]
2Traceback [most recent call last]: File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print[queue.pop[0]] IndexError: pop from empty list7
Traceback [most recent call last]: File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print[queue.pop[0]] IndexError: pop from empty list8
0 Full: True Elements dequeued from the queue a b c Empty: True Empty: False Full: False1
Traceback [most recent call last]: File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print[queue.pop[0]] IndexError: pop from empty list0
Traceback [most recent call last]: File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print[queue.pop[0]] IndexError: pop from empty list7
=
6Output:
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: False5
queue
0 Full: True Elements dequeued from the queue a b c Empty: True Empty: False Full: False7
queue.append[
60 Full: True Elements dequeued from the queue a b c Empty: True Empty: False Full: False9
=
queue.append[
9=
'a'
1Traceback [most recent call last]: File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print[queue.pop[0]] IndexError: pop from empty list0
Traceback [most recent call last]: File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print[queue.pop[0]] IndexError: pop from empty list7
'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 list0
'a'
5
Traceback [most recent call last]: File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print[queue.pop[0]] IndexError: pop from empty list2
Traceback [most recent call last]: File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print[queue.pop[0]] IndexError: pop from empty list0
'a'
5
Traceback [most recent call last]: File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print[queue.pop[0]] IndexError: pop from empty list5
Traceback [most recent call last]: File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print[queue.pop[0]] IndexError: pop from empty list0
Traceback [most recent call last]: File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print[queue.pop[0]] IndexError: pop from empty list7
Traceback [most recent call last]: File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print[queue.pop[0]] IndexError: pop from empty list8
Traceback [most recent call last]: File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print[queue.pop[0]] IndexError: pop from empty list06
Traceback [most recent call last]: File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print[queue.pop[0]] IndexError: pop from empty list07
Traceback [most recent call last]: File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print[queue.pop[0]] IndexError: pop from empty list7
Traceback [most recent call last]: File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print[queue.pop[0]] IndexError: pop from empty list8
=
9Traceback [most recent call last]: File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print[queue.pop[0]] IndexError: pop from empty list0
Traceback [most recent call last]: File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print[queue.pop[0]] IndexError: pop from empty list7
Traceback [most recent call last]: File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print[queue.pop[0]] IndexError: pop from empty list13
Traceback [most recent call last]: File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print[queue.pop[0]] IndexError: pop from empty list7
Traceback [most recent call last]: File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print[queue.pop[0]] IndexError: pop from empty list13
Traceback [most recent call last]: File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print[queue.pop[0]] IndexError: pop from empty list7
Traceback [most recent call last]: File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print[queue.pop[0]] IndexError: pop from empty list13
Traceback [most recent call last]: File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print[queue.pop[0]] IndexError: pop from empty list7
Traceback [most recent call last]: File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print[queue.pop[0]] IndexError: pop from empty list8
Traceback [most recent call last]: File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print[queue.pop[0]] IndexError: pop from empty list20
Traceback [most recent call last]: File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print[queue.pop[0]] IndexError: pop from empty list21
'a'
5
Traceback [most recent call last]: File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print[queue.pop[0]] IndexError: pop from empty list23
Traceback [most recent call last]: File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print[queue.pop[0]] IndexError: pop from empty list0
Traceback [most recent call last]: File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print[queue.pop[0]] IndexError: pop from empty list7
Traceback [most recent call last]: File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print[queue.pop[0]] IndexError: pop from empty list8
Traceback [most recent call last]: File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print[queue.pop[0]] IndexError: pop from empty list20
Traceback [most recent call last]: File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print[queue.pop[0]] IndexError: pop from empty list28
Traceback [most recent call last]: File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print[queue.pop[0]] IndexError: pop from empty list7
Traceback [most recent call last]: File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print[queue.pop[0]] IndexError: pop from empty list8
Traceback [most recent call last]: File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print[queue.pop[0]] IndexError: pop from empty list31
Traceback [most recent call last]: File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print[queue.pop[0]] IndexError: pop from empty list32
Output:
0 Full: True Elements dequeued from the queue a b c Empty: True Empty: False Full: False