• 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

Implementation of Law Number 14 of 2008 concerning Transparency of Public Information (State Gazette of the Republic of Indonesia of 2010 Number 99, Supplement to State Gazette

The variables such as leaflet number and area, stomata size and density, flower length, keel width, flag width and peduncle length, and pollen diameter and

Act Number 13 Year 2003 Concerning Manpower 1 Article 139 It is stated that "The implementation of the strike staged by the workers / labourers of enterprises that serve the public

So it can be concluded that firm size, return on assets, operating profit margin, earnings per share and public ownership simultaneously significant effect on Income Smoothing.. It can

Сurriculum vitae PhD - Candidate of Technical Sciences, Associate Professor, Academician of the MAI RK Scientific interests: Strength calculations of structural elements and the

with or without the return of the mortgage certificate that has been issued, not returned, then it must be recorded in the Land Rights Mortgage Book.16 The Relevance of DSN Fatwa

PUBLIC ASSET CREATION SUMMARY Road Name/ Number Carriageway Width m Length m Tendered Cost $ Total: Road Reserve Area Dedicated m2: Piped Drainage Pipe Size mm Material Length m

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