1. Trang chủ
  2. » Giáo Dục - Đào Tạo

7 2 queues tủ tài liệu bách khoa

20 57 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 20
Dung lượng 0,91 MB

Nội dung

Trần Thị Thanh Nga ngattt@hcmuaf.edu.vn Khoa Công nghệ thông tin, ĐH Nông Lâm HCM Queues Queue  A queue is a container of objects (a linear collection) that are inserted and removed according to the first-in first-out (FIFO) principle  Example:  Queues of customers in a bank or in a grocery store and queues of cars waiting to pass through a tollbooth  Similarly, because a computer can send a print request faster than a printer can print, a queue of documents is often waiting to be printed at a printer Queues Queue  In the queue only operations are allowed:  Enqueue means to insert an item into the back of the queue,  Dequeue means removing the front item Queues Queue  The difference between stacks and queues is in removing  In a stack: remove the item the most recently added;  In a queue: remove the item the least recently added Queues The Queue Abstract Data Type  The queue supports two fundamental methods:  enqueue(o): Insert object o at the rear of the queue Input: Object; Output: none  dequeue(): Remove the object from the front of the queue and return it; an error occurs if the queue is empty Input: none; Output: Object Queues The Queue Abstract Data Type  These support methods should also be defined:  size(): Return the number of objects in the queue Input: none; Output: integer  isEmpty(): Return a boolean value that indicates whether the queue is empty Input: none; Output: boolean  front(): Return, but not remove, the front object in the queue; an error occurs if the queue is empty Input: none; Output: Object Queues Queu Interface interface QueueInterface { // Tests if the Queue is empty public boolean isEmpty(); // Removes and returns the front item public T dequeue() throws QueueException; //Returns the front item without its removal public T getFront() throws QueueException; //Inserts an item to the back public void enqueue(T e); //Removes all items from the Queue public void clear(); } Queues An Array-Based Queue  We need:  an array to store the queue elements,  the variables queueFront and queueRear to keep track of the first and last elements of the queue,  the variable maxQueueSize to specify the maximum size of the queue  How to use queueFront and queueRear to access the queue elements  How queueFront and queueRear indicate that the queue is empty or full? Queues An Array-Based Queue  To add an element to the queue, first we advance queueRear to the next array position and then add the element to the position that queueRear is pointing to  After adding elements to the queue: Queues An Array-Based Queue  To delete an element from the queue:  first we retrieve the element that queueFront is pointing to  and then advance queueFront to the next element of the queue Queues An Array-Based Queue  What happen when queueRear to point to the last array position, giving the impression that the queue is full However, the queue has only two or three elements and the front of the array is empty Queues An Array-Based Queue  Solution when the queue overflows to the rear, we can check the value of the index queueFront  If the value of queueFront indicates that there is room in the front of the array, then when queueRear gets to the last array position  slide all of the queue elements toward the first array position  This solution is good if the queue size is very small; Queues An Array-Based Queue  Another solution to this problem is to assume that the array is circular— that is, the first array position immediately follows the last array position Queues An Array-Based Queue  Because the array containing the queue is circular, we can use the following statement to advance queueRear (queueFront) to the next array position: queueRear = (queueRear + 1) % maxQueueSize;  If queueRear < maxQueueSize –  queueRear +

Ngày đăng: 09/11/2019, 07:16

TÀI LIỆU CÙNG NGƯỜI DÙNG

  • Đang cập nhật ...

TÀI LIỆU LIÊN QUAN

w