CS350: Data Structures Queues · 2017-12-09 · CS350: Data Structures Queues • Queues are...

Post on 17-Jul-2020

16 views 0 download

Transcript of CS350: Data Structures Queues · 2017-12-09 · CS350: Data Structures Queues • Queues are...

CS350: Data Structures © James Moscola

College Catalog2009–2011

!"#$%&'())*+,-.)/.&01234546708.9:;*&<:(#.="#>&1015?26511??@A9/**/")*&<B!&C(>&1015?2D50633

05?3352775?30?EEEF+C:F(A;

!""#$%%&'$#()*$&+$,-$%.$"

'GHI<GJHK&L<MNK'GONJHK&P@JJHGMFIF&'<IJ@QH&'@OK

!<GR%&'@'HGPOJ&N<F&012

YO

RK

CO

LLEGE O

F PENN

SY

LVA

NIA

CO

LLEGE C

ATA

LOG

2009–2011

!""#$%&'()*+,--.../ 012$1$"..."34#3$4.56

College Catalog2009–2011

!"#$%&'())*+,-.)/.&01234546708.9:;*&<:(#.="#>&1015?26511??@A9/**/")*&<B!&C(>&1015?2D50633

05?3352775?30?EEEF+C:F(A;

!""#$%%&'$#()*$&+$,-$%.$"

'GHI<GJHK&L<MNK'GONJHK&P@JJHGMFIF&'<IJ@QH&'@OK

!<GR%&'@'HGPOJ&N<F&012

YO

RK

CO

LLEGE O

F PENN

SY

LVA

NIA

CO

LLEGE C

ATA

LOG

2009–2011

!""#$%&'()*+,--.../ 012$1$"..."34#3$4.56

College Catalog2009–2011

!"#$%&'())*+,-.)/.&01234546708.9:;*&<:(#.="#>&1015?26511??@A9/**/")*&<B!&C(>&1015?2D50633

05?3352775?30?EEEF+C:F(A;

!""#$%%&'$#()*$&+$,-$%.$"

'GHI<GJHK&L<MNK'GONJHK&P@JJHGMFIF&'<IJ@QH&'@OK

!<GR%&'@'HGPOJ&N<F&012

YO

RK

CO

LLEGE O

F PENN

SY

LVA

NIA

CO

LLEGE C

ATALO

G 2009–2011

!""#$%&'()*+,--.../ 012$1$"..."34#3$4.56

James MoscolaDepartment of Engineering & Computer ScienceYork College of Pennsylvania

CS350: Data StructuresQueues

CS350: Data Structures

Queues

• Queues are another very common data structure that can be used for a variety of data storage purposes

• Similar to a line of people waiting for a ride at an amusement park - People enter the line/queue at the rear- People wait behind others that entered the line/queue before them- People exit from the front of the line/queue to get on the ride- People in the middle of the line/queue cannot get out without first

advancing to the front of the line- There is no cutting

• May also be referred to as a FIFO (First-In-First-Out)

2

CS350: Data Structures

Queue Operations

• Queues have two main operations

- Enqueue - inserts an element into the back of the queue

- Dequeue - removes a single element from the front (i.e. the head) of the queue

3

CS350: Data Structures

Queue Operations

4

Start with Empty Queue

FrontBack

CS350: Data Structures

Queue Operations

5

Enqueue Value: A

A

FrontBack

CS350: Data Structures

Queue Operations

6

A

FrontBack

B

Enqueue Value: B

CS350: Data Structures

Queue Operations

7

A

FrontBack

BC

Enqueue Value: C

CS350: Data Structures

FrontBack

Queue Operations

8

ABC

Dequeue the Front of the Queue:

CS350: Data Structures

Queue Interface (Java)

9

public interface Queue<AnyType> {

public void enqueue( AnyType x ); public AnyType dequeue( );

public AnyType getHead( );

public boolean isEmpty( );

public void makeEmpty( );}

CS350: Data Structures

Queue Implementations

• Queues can be implemented in multiple ways

• Two popular methods for implementing queues include - (1) Arrays- (2) Linked Lists

• Both of these implementation approaches allow for constant time operations - O(1)

10

CS350: Data Structures

-1

Queue Implementation Using an Array

11

Start with Empty Queue (i.e. an array)

tail pointer is initialized to -1

head

second row is array index

4 3 2 1 0

tail

CS350: Data Structures

Queue Implementation Using an Array

12

Enqueue Value: A

A

-1

head

4 3 2 1 0

tail

CS350: Data Structures

Queue Implementation Using an Array

13

Enqueue Value: B

A

-1

head

4 3 2 1 0

tail

B

CS350: Data Structures

Queue Implementation Using an Array

14

Enqueue Value: C

A

-1

head

4 3 2 1 0

tail

BC

CS350: Data Structures

A

Queue Implementation Using an Array

15

Dequeue the Head of the Queue:

-1

head

4 3 2 1 0

tail

BC A

CS350: Data Structures

Queue Implementation Using an Array

• Considerations when using an array implementation - Enqueue, and Dequeue operations run in constant time ... in most

cases- What happens when your array is full and you want to Enqueue

another element?• Array must be increased in size which takes time an more memory• Time to copy and create new array is O(N)• Time to copy array is amortized over the lifetime of the array• May not be suitable for all types of systems (e.g. RTOS)

16

CS350: Data Structures

Queue Implementation Using an Array (Java)

17

/** * Queue constructor */ public ArrayQueue( ) { theArray = (AnyType []) new Object[ DEFAULT_CAPACITY ]; makeEmpty( ); }

CS350: Data Structures

Queue Implementation Using an Array (Java)

18

/** * Test if the queue is logically empty. * @return true if empty, false otherwise. */ public boolean isEmpty( ) { return currentSize == 0; }

CS350: Data Structures

Queue Implementation Using an Array (Java)

19

/** * Make the queue logically empty. */ public void makeEmpty( ) { currentSize = 0; head = 0; tail = -1; }

CS350: Data Structures

Queue Implementation Using an Array (Java)

20

/** * Insert a new item into the queue. * @param x the item to insert. */ public void enqueue( AnyType x ) { if( currentSize == theArray.length ) doubleQueue( ); tail = increment( tail ); theArray[ tail ] = x; currentSize++; }

CS350: Data Structures

Queue Implementation Using an Array (Java)

21

/** * Return and remove the least recently inserted item * from the queue. * @return the least recently inserted item in the queue. * @throws UnderflowException if the queue is empty. */ public AnyType dequeue( ) { if( isEmpty( ) ) throw new UnderflowException( "ArrayQueue dequeue" ); currentSize--;

AnyType returnValue = theArray[ head ]; head = increment( head ); return returnValue; }

CS350: Data Structures

Queue Implementation Using an Array (Java)

22

/** * Internal method to increment with wraparound. * @param x any index in theArray's range. * @return x+1, or 0 if x is at the end of theArray. */ private int increment( int x ) { if( ++x == theArray.length ) x = 0; return x; }

CS350: Data Structures

Queue Implementation Using an Array (Java)

23

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

newArray = (AnyType []) new Object[ theArray.length * 2 ];

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

theArray = newArray; head = 0; tail = currentSize - 1; }

CS350: Data Structures

Queue Implementation Using a LinkedList

24

Start with Empty Queue (i.e. a null LinkedList)

head

head points to the front of the linked list

NULL

tailtail points to the back of

the linked list

CS350: Data Structures

null

Queue Implementation Using a LinkedList

25

NULLA

Enqueue Value: A

head

tail

CS350: Data Structures

Queue Implementation Using a LinkedList

26

nullA

Enqueue Value: B

head

NULLB

tail

CS350: Data Structures

Queue Implementation Using a LinkedList

27

nullA

Enqueue Value: B

head

NULLB

tail

CS350: Data Structures

Queue Implementation Using a LinkedList

28

nullA

Enqueue Value: C

head

nullB

tail

NULLC

CS350: Data Structures

Queue Implementation Using a LinkedList

29

nullA

Enqueue Value: C

head

nullB

tail

NULLC

CS350: Data Structures

Queue Implementation Using a LinkedList

30

Dequeue the Front of the Queue:

head

nullB

tail

NULLC

CS350: Data Structures

Queue Implementation Using a LinkedList

• Considerations when using an queue implementation - Enqueue and Dequeue operations run in constant time ... still- Each element inserted into the queue requires a pointer to the data

and a second pointer to the next node in the LinkedList

31

CS350: Data Structures

Queue Implementation Using a LinkedList (Java)

32

/** * Queue constructor */ public ListQueue( ) { head = tail = null; }

private ListNode<AnyType> head; private ListNode<AnyType> tail;

CS350: Data Structures

Queue Implementation Using a LinkedList (Java)

33

/** * Test if the queue is logically empty. * @return true if empty, false otherwise. */ public boolean isEmpty( ) { return head == null; }

CS350: Data Structures

Queue Implementation Using a LinkedList (Java)

34

/** * Make the queue logically empty. */ public void makeEmpty( ) { head = null; tail = null; }

CS350: Data Structures

Queue Implementation Using a LinkedList (Java)

35

/** * Insert a new item into the queue. * @param x the item to insert. */ public void enqueue( AnyType x ) { if( isEmpty( ) ) // Make queue of one element tail = head = new ListNode<AnyType>( x ); else // Regular case tail = tail.next = new ListNode<AnyType>( x ); }

CS350: Data Structures

Queue Implementation Using a LinkedList (Java)

36

/** * Return and remove the least recently inserted item * from the queue. * @return the least recently inserted item in the queue. * @throws UnderflowException if the queue is empty. */ public AnyType dequeue( ) { if( isEmpty( ) ) throw new UnderflowException( "ListQueue dequeue" );

AnyType returnValue = head.element; head = head.next; return returnValue; }