M180: Data Structures & Algorithms in Java Queues Arab Open University 1.

21
M180: Data Structures & Algorithms in Java Queues Arab Open University 1

description

3 The Queue ADT Auxiliary queue operations: –object front(): returns the element at the front without removing it. –integer size(): returns the number of elements stored. –boolean isEmpty(): indicates whether no elements are stored. –The length of a queue is the number of elements it contains.

Transcript of M180: Data Structures & Algorithms in Java Queues Arab Open University 1.

Page 1: M180: Data Structures & Algorithms in Java Queues Arab Open University 1.

1

M180: Data Structures & Algorithms in Java

Queues

Arab Open University

Page 2: M180: Data Structures & Algorithms in Java Queues Arab Open University 1.

2

The Queue ADT

• The Queue ADT stores arbitrary objects.

• Insertions and deletions follow the first-in first-out scheme.

• Insertions are at the rear of the queue and removals are at the front of the queue.

• Main queue operations:– enqueue(object): inserts an element at the end of the queue– object dequeue(): removes and returns the element at the

front of the queue

Page 3: M180: Data Structures & Algorithms in Java Queues Arab Open University 1.

3

The Queue ADT

• Auxiliary queue operations:

– object front(): returns the element at the front without removing it.

– integer size(): returns the number of elements stored.

– boolean isEmpty(): indicates whether no elements are stored.

– The length of a queue is the number of elements it contains.

Page 4: M180: Data Structures & Algorithms in Java Queues Arab Open University 1.

Applications of Queues

Scheduler for controlling access to shared resources ‒ maintains queues of printers’ jobs ‒ maintains queues of disk input/output requests

Simulation of real word situations ‒ use queues to simulate waiting queues in real scenarios

Telephone operators ‒ queuing system for handling calls to toll-free numbers.

Page 5: M180: Data Structures & Algorithms in Java Queues Arab Open University 1.

5

Queue ExampleOperation Output Q enqueue(5) – (5)enqueue(3) – (5, 3)dequeue() 5 (3)enqueue(7) – (3, 7)dequeue() 3 (7)front() 7 (7)dequeue() 7 ()dequeue() “error” ()isEmpty() true ()enqueue(9) – (9)enqueue(7) – (9, 7)size() 2 (9, 7)enqueue(3) – (9, 7, 3)enqueue(5) – (9, 7, 3, 5)dequeue() 9 (7, 3, 5)

Page 6: M180: Data Structures & Algorithms in Java Queues Arab Open University 1.

Specifying operations of a Queue

enqueue(newElem) // Inserts newElem at the back of the queue, if there is no violation of

createQueue// Create an empty queue

isEmpty( )// Determines if a queue is empty

getFront( ) // Returns, but does not remove, the head of the queue.

dequeue( ) // Retrieves and removes the head of the queue.

Page 7: M180: Data Structures & Algorithms in Java Queues Arab Open University 1.

7

Array implementation of queues• This is accomplished by inserting at one end (the rear) and

deleting from the other (the front)

• To insert: put new element in location 4, and set rear to 4• To delete: take element from location 0, and set front to 1

17 23 97 440 1 2 3 4 5 6 7

myQueue:

rear = 3front = 0

Page 8: M180: Data Structures & Algorithms in Java Queues Arab Open University 1.

8

Array implementation of queues

• Notice how the array contents “crawl” to the right as elements are inserted and deleted

17 23 97 44 333After insertion:

23 97 44 333After deletion:

rear = 4front = 1

17 23 97 44Initial queue:

rear = 3front = 0

Page 9: M180: Data Structures & Algorithms in Java Queues Arab Open University 1.

9

Circular arrays• We can treat the array holding the queue elements as

circular (joined at the ends)

44 55 11 22 330 1 2 3 4 5 6 7

myQueue:

rear = 1 front = 5• Elements were added to this queue in the order 11, 22,

33, 44, 55, and will be removed in the same order• Use: front = (front + 1) % myQueue.length;

and: rear = (rear + 1) % myQueue.length;

Page 10: M180: Data Structures & Algorithms in Java Queues Arab Open University 1.

10

Full and empty queues

• If the queue were to become completely full, it would look like this:

• If we were then to remove all eight elements, making the queue completely empty, it would look like this:

44 55 66 77 88 11 22 330 1 2 3 4 5 6 7

myQueue:

rear = 4 front = 5

0 1 2 3 4 5 6 7myQueue:

rear = 4 front = 5This is a problem!

Page 11: M180: Data Structures & Algorithms in Java Queues Arab Open University 1.

11

Full and empty queues: solutions

• Solution #1: Keep an additional variable

• Solution #2: (Slightly more efficient) Keep a gap between elements: consider the queue full when it has n-1 elements

44 55 66 77 88 11 22 330 1 2 3 4 5 6 7

myQueue:

rear = 4 front = 5count = 8

44 55 66 77 11 22 330 1 2 3 4 5 6 7

myQueue:

rear = 3 front = 5

Page 12: M180: Data Structures & Algorithms in Java Queues Arab Open University 1.

12

Queue Interface in Java

• Java interface corresponding to our Queue ADT.

• No corresponding built-in Java class

public interface Queue {

public int size();

public boolean isEmpty();

public Object front();public void enqueue(Object o);

public Object dequeue();}

Page 13: M180: Data Structures & Algorithms in Java Queues Arab Open University 1.

13

Interfaces

• Can contain only constants (final variables) and abstract method (no implementation).

• An interface is basically a kind of class—it contains methods and variables.

• Therefore, it is the responsibility of the class that implements an interface to supply the code for methods.

Page 14: M180: Data Structures & Algorithms in Java Queues Arab Open University 1.

14

Interfaces Definition

• Syntax:

• Example:

interface InterfaceName {// Constant/Final Variable Declaration// Methods Declaration – only method

body}

interface Speaker {public void speak( );

}

Page 15: M180: Data Structures & Algorithms in Java Queues Arab Open University 1.

15

Implementing Interfaces

• Interfaces are used like super-classes who properties are inherited by classes. This is achieved by creating a class that implements the given interface as follows:

class ClassName implements InterfaceName [, InterfaceName2, …]{

// Body of Class}

Page 16: M180: Data Structures & Algorithms in Java Queues Arab Open University 1.

16

Implementing Interfaces Exampleclass Politician implements Speaker {

public void speak(){System.out.println(“Talk politics”);

}}

class Priest implements Speaker {public void speak(){

System.out.println(“Religious Talks”);}

}

class Lecturer implements Speaker {public void speak(){

System.out.println(“Talks Object Oriented Design and Programming!”);

}}

Page 17: M180: Data Structures & Algorithms in Java Queues Arab Open University 1.

17

Queue Interfacepublic interface Queue{   void  enqueue( Object x ); // Insert a new item into the queue.       Object getFront( ); // Get the least recently inserted item in the queue        Object dequeue( ); // Return and remove the least recently inserted item

   boolean isEmpty( ); // return true if empty, false otherwise

   void makeEmpty( ); // Make the queue logically empty. }

Page 18: M180: Data Structures & Algorithms in Java Queues Arab Open University 1.

18

Array-Queue Implementationprivate int[ ] theArray;    private int        currentSize;    private int        front;    private int        back;

private static final int DEFAULT_CAPACITY = 10;    public class ArrayQueue implements Queue{    public ArrayQueue( )     {        theArray = new int[ DEFAULT_CAPACITY ];        makeEmpty( );    }

   public boolean isEmpty( )    {        return currentSize == 0;    }

Page 19: M180: Data Structures & Algorithms in Java Queues Arab Open University 1.

19

Queue Implementationpublic void makeEmpty( )    {        currentSize = 0;        front = 0;        back = -1;    }   

    public int dequeue( )    {        if( !isEmpty( ) )             currentSize--;

        int returnValue = theArray[ front ];        front = increment( front );        return returnValue;    } 

Page 20: M180: Data Structures & Algorithms in Java Queues Arab Open University 1.

Queue Implementationpublic int getFront( )    {        if( !isEmpty( ) )            return theArray[ front ];    }

public void enqueue( Object x )    {        if( currentSize == theArray.length )            doubleQueue( );        back = increment( back );        theArray[ back ] = x;        currentSize++;    }

    private int increment( int x ) //Internal method to expand theArray.    {        if( ++x == theArray.length )            x = 0;        return x;    }

Page 21: M180: Data Structures & Algorithms in Java Queues Arab Open University 1.

21

Queue Implementation

    private void doubleQueue( ) //Internal method to expand theArray.    {         int  [ ] newArray;

        newArray = new  int[ theArray.length * 2 ];

            // Copy elements that are logically in the queue        for( int i = 0; i < currentSize; i++, front = increment( front ) )            newArray[ i ] = theArray[ front ];

        theArray = newArray;        front = 0;        back = currentSize - 1;    }}