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

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

20 58 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

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

The Queue Abstract Data Type  The queue supports two fundamental methods:  enqueueo: Insert object o at the rear of the queue Input: Object; Output: none  dequeue: Remove the obje

Trang 1

Trần Thị Thanh Nga

ngattt@hcmuaf.edu.vn

Khoa Công nghệ thông tin, ĐH Nông Lâm HCM

Trang 2

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

Trang 3

Queue

 In the queue only 2 operations are allowed:

Enqueue means to insert an item into the back of the queue,

Dequeue means removing the front item

Trang 4

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

Trang 5

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

Trang 6

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 do not remove, the front object

in the queue; an error occurs if the queue is empty

Input: none; Output: Object

Trang 7

Queu Interface

interface QueueInterface<T> {

// 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();

}

Trang 8

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 do queueFront and queueRear indicate that the

queue is empty or full?

Trang 9

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 2 elements to the queue:

Trang 10

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

Trang 11

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

Trang 12

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;

Trang 13

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

Trang 14

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 – 1

 queueRear + 1 <= maxQueueSize - 1,

 (queueRear + 1) % maxQueueSize = queueRear + 1

If queueRear == maxQueueSize – 1

queueRear + 1 == maxQueueSize,

(queueRear + 1) % maxQueueSize = 0

Trang 15

An Array-Based Queue

public class ArrayQueue<T> implements

QueueInterface<T> {

private static final int DEFAULT_CAPACITY = 10;

private int maxQueueSize ,

private T[] A ;

public ArrayQueue() {

A = (T[]) new Object[DEFAULT_CAPACITY];

Trang 16

An Array-Based Queue

public boolean isEmpty() {

return count == 0;

}

public boolean isFull() {

return count == maxQueueSize ;

}

public void clear() {

for ( int i = 0; i < maxQueueSize ; i ++)

A[i] = null ;

count = 0;

queueRear = -1;

Trang 17

public void enqueue(T value ) {

if (isFull())

doubleSize();

queueRear++;

A[queueRear % maxQueueSize] = value;

count++;

}

public T dequeue() {

T e = getFront();

A[queueFront % maxQueueSize] = null ;

queueFront++;

count ;

return e ;

}

Trang 18

Quick Review

A queue is a data structure in which the items are

added at one end and removed from the other end

A queue is a First In First Out (FIFO) data structure

The basic operations on a queue are as follows: Add

an item to the queue, remove an item from the queue,

retrieve the first and last element of the queue,

initialize the queue, check whether the queue is

empty, and check whether the queue is full

 A queue can be implemented as an array or a linked

Trang 19

Quick Review

 The middle elements of a queue should not be

accessed directly

If the queue is nonempty, the function front returns the front element of the queue and the function back

returns the last element in the queue

Queues are restricted versions of arrays and linked

lists

Trang 20

Question?

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