• Tidak ada hasil yang ditemukan

The Queue ADT

N/A
N/A
Protected

Academic year: 2025

Membagikan "The Queue ADT"

Copied!
8
0
0

Teks penuh

(1)

The Queue ADT

Queue: a dynamic data structure where elements are entered at one end and removed from the other end, and processed via a FIFO (first-in / first-out).

The end from which addition to the queue occurs is called rear of queue, the process of addition is called enqueue. The item that is added onto the queue at the rear must move up to the front before it can be removed.

The end from which deleting from a queue occurs is called front of queue, the process of deleting is called dequeue.

Array implementation of a Queue ADT

Defining the queue ADT as JAVA class

public class Queue {

final int MAX_QUEUE_SIZE = 150; // The physical size of the queue int data[]; // Declaration of the queue

protected int front, rear; // The front and rear elements in the queue protected int size = 0; // Logical size of queue

// ….. Here we should add the implementation of all methods }

Queue Operations

1. Create Operation

- Function: To create a new queue.

- Precondition: The queue is in an unpredictable state

- Postcondition: The queue is initialized to empty.

- Implementation:

(2)

public Queue ( ) {

data = new int [MAX_QUEUE_SIZE];

front = 0;

rear = -1;

size = 0;

}

OR

Queue<Integer> que = new LinkedList<Integer>();

2. Empty Operation

- Function: To check if the queue is empty or not.

- Precondition: The queue is initialized.

- Postcondition: Returns true if queue is empty, false otherwise.

- Implementation:

public boolean isEmpty() {

return ( size == 0 );

}

OR

return que.isEmpty();

3. Full Operation

- Function: To check if the queue is full or not - Precondition: The queue is initialized.

- Postcondition: Returns true if queue is full, false otherwise.

- Implementation:

public boolean isFull() {

return (rear == MAX_QUEUE_SIZE-1); // Check of fullness }

OR

return que.isFull();

(3)

4. Enqueue Operation (Add)

- Function: To add a new item to the rear of the queue - Preconditions: ▪ The queue is initialized.

▪ There is memory available to store the new item in the queue.

▪ The parameter is an object of type E.

- Postconditions: - Item is added to the rear of the queue.

- The size of the queue is incremented by 1.

- Implementation:

public void enqueue(int item) {

if ( !isFull() ) {

++rear;

data[rear] = item;

++size;

} else

System.out.println("The Queue is full.. The value has NOT been inserted");

}

OR

que.add(item);

5. Dequeue Operation (Delete)

- Function: To delete the item at the front of the queue - Preconditions: ▪ The queue is initialized.

▪ The queue is not empty.

- Postconditions: - The front value of the queue is removed (and returned).

- The size of the queue is decremented by 1.

- Implementation:

public int dequeue() {

if ( ! isEmpty() ) {

int deletedItem = data[ front ];

++front;

--size;

return deletedItem;

}

(4)

else

return -999;

} OR

return que.remove();

6. atFront Operation (Retrieve)

- Function: To return the value at the front of the queue.

- Preconditions: ▪ The queue is initialized.

▪ The queue is not empty.

- Postcondition: Returns the value at the front of the queue without removing it.

- Implementation:

public int atFront() {

if ( ! isEmpty() ) {

int returnedItem = data[ front ];

return returnedItem;

} else

return -999;

} OR

return que.peek();

7. Length Operation

- Function: To return the number of elements in the queue.

- Precondition: The queue is initialized.

- Postcondition: The number of elements in the queue is returned - Implementation:

public int length() {

return size;

} OR

return que.size();

(5)

LinkedList Implementation of a Queue ADT Defining the queue ADT as JAVA class

public class Queue {

private long size;

private Node front;

private Node rear;

// …….. Here we should add the implementation of all methods……..

public static void main(String args[]) {

// …….. Main Program………

} // Main Program End

private class Node {

int data;

Node next;

public Node (int value) {

data = value;

next = null;

}

} // Node Class End } // Queue Class End

Queue Operations

1. Creation operation:

public Queue () {

front = null;

rear = null;

size =0;

}

2. isEmpty operation:

public boolean isEmpty() {

return size == 0;

}

(6)

3. atFront operation:

public int atFront() {

if (! isEmpty()) {

int returnedItem = front.data;

return returnedItem;

} else

return -999;

}

4. Length operation:

public long length ( ) {

return size;

}

5. Enqueue operation:

public void enqueue(int item ) {

If (isEmpty()) {

front = new Node(item);

rear = front;

}

else {

rear.next = new Node(item);

rear = rear.next;

} ++size;

}

6. Dequeue operation:

public int dequeue() {

if (! isEmpty()) {

int deletedItem = front.data;

Node garbage;

garbage = front;

front = front.next;

garbage.next = null;

System.gc();

--size;

return deletedItem;

(7)

} else

return -999;

}

Circular Implementation of a Queue

- Instead of ++front; use front = ( front +1 ) % MAX_QUEUE_SIZE;

- Instead of ++rear; use rear = ( rear +1 ) % MAX_QUEUE_SIZE;

-

If( size == 1 ) 

one-entry queue -

If( size == 0 ) 

empty queue

-

If( size ==MAX_QUEUE_SIZE ) 

full queue - If Front > rear  Size = (N – front + rear mod N) + 1 - If Front > rear  Size = (N – (front + rear) mod N) - 1

Priority Queue

- Priority queue: is a queue in which the entries on the queue are

ranked into groups according to priority. Such a queue requires a

rear pointer for each different possible priority value.

(8)

- Example:

Jobs enter at rear of one of three queues

- Empty condition for priority queue is: rear( n-1 ) = rear n

Referensi

Dokumen terkait

Histioteuthis bonnellii : size of mature specimens examined (ML: mantle length; TW: total weight), number of spermatophores (Sp), number of spermatangia (Sg), spermatophores size in

Below is the definition of the Pen class: public class Pen{ private String colour; private boolean hasEraser; private int tipType; public PenString colour, int tipType, boolean

There were 8 variables used to predict the number of outpatient visits as the participants about a medical check-up, referral, extension of BPJS service, queue length, health treatment

The implementation of archive management in the administration section of public elementary schools in Pammana Subdistrict, Wajo Regency, South Sulawesi was also found to be a number

DISCUSSION.—Similarity in the size and shape of the disc granules, regularity and sequence of the arm spines, number of tentacle scales, length-to-breadth ratio of the oral shields, and

This rule is used to satisfy the QoS requirements of WiMAX Algorithm 1:- Setting up the priority Input: Expiry Time, Waiting Time, Packet Size, Queue Length, Type of Service, Mobility

Bujian Dusun Program in South Bengkulu Regency: Study of the Implementation of Regent Regulation Number 39 of 2020 concerning Implementation of Public Services Through the Injiak

Int Data Type In Python, the int data type is a numeric type used to represent whole integer number as opposition with float number.. Creating a int is done by using the whole integer