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

36
CS350: Data Structures © James Moscola James Moscola Department of Engineering & Computer Science York College of Pennsylvania CS350: Data Structures Queues

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

Page 1: CS350: Data Structures Queues · 2017-12-09 · CS350: Data Structures Queues • Queues are another very common data structure that can be used for a variety of data storage purposes

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

Page 2: CS350: Data Structures Queues · 2017-12-09 · CS350: Data Structures Queues • Queues are another very common data structure that can be used for a variety of data storage purposes

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

Page 3: CS350: Data Structures Queues · 2017-12-09 · CS350: Data Structures Queues • Queues are another very common data structure that can be used for a variety of data storage purposes

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

Page 4: CS350: Data Structures Queues · 2017-12-09 · CS350: Data Structures Queues • Queues are another very common data structure that can be used for a variety of data storage purposes

CS350: Data Structures

Queue Operations

4

Start with Empty Queue

FrontBack

Page 5: CS350: Data Structures Queues · 2017-12-09 · CS350: Data Structures Queues • Queues are another very common data structure that can be used for a variety of data storage purposes

CS350: Data Structures

Queue Operations

5

Enqueue Value: A

A

FrontBack

Page 6: CS350: Data Structures Queues · 2017-12-09 · CS350: Data Structures Queues • Queues are another very common data structure that can be used for a variety of data storage purposes

CS350: Data Structures

Queue Operations

6

A

FrontBack

B

Enqueue Value: B

Page 7: CS350: Data Structures Queues · 2017-12-09 · CS350: Data Structures Queues • Queues are another very common data structure that can be used for a variety of data storage purposes

CS350: Data Structures

Queue Operations

7

A

FrontBack

BC

Enqueue Value: C

Page 8: CS350: Data Structures Queues · 2017-12-09 · CS350: Data Structures Queues • Queues are another very common data structure that can be used for a variety of data storage purposes

CS350: Data Structures

FrontBack

Queue Operations

8

ABC

Dequeue the Front of the Queue:

Page 9: CS350: Data Structures Queues · 2017-12-09 · CS350: Data Structures Queues • Queues are another very common data structure that can be used for a variety of data storage purposes

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

Page 10: CS350: Data Structures Queues · 2017-12-09 · CS350: Data Structures Queues • Queues are another very common data structure that can be used for a variety of data storage purposes

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

Page 11: CS350: Data Structures Queues · 2017-12-09 · CS350: Data Structures Queues • Queues are another very common data structure that can be used for a variety of data storage purposes

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

Page 12: CS350: Data Structures Queues · 2017-12-09 · CS350: Data Structures Queues • Queues are another very common data structure that can be used for a variety of data storage purposes

CS350: Data Structures

Queue Implementation Using an Array

12

Enqueue Value: A

A

-1

head

4 3 2 1 0

tail

Page 13: CS350: Data Structures Queues · 2017-12-09 · CS350: Data Structures Queues • Queues are another very common data structure that can be used for a variety of data storage purposes

CS350: Data Structures

Queue Implementation Using an Array

13

Enqueue Value: B

A

-1

head

4 3 2 1 0

tail

B

Page 14: CS350: Data Structures Queues · 2017-12-09 · CS350: Data Structures Queues • Queues are another very common data structure that can be used for a variety of data storage purposes

CS350: Data Structures

Queue Implementation Using an Array

14

Enqueue Value: C

A

-1

head

4 3 2 1 0

tail

BC

Page 15: CS350: Data Structures Queues · 2017-12-09 · CS350: Data Structures Queues • Queues are another very common data structure that can be used for a variety of data storage purposes

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

Page 16: CS350: Data Structures Queues · 2017-12-09 · CS350: Data Structures Queues • Queues are another very common data structure that can be used for a variety of data storage purposes

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

Page 17: CS350: Data Structures Queues · 2017-12-09 · CS350: Data Structures Queues • Queues are another very common data structure that can be used for a variety of data storage purposes

CS350: Data Structures

Queue Implementation Using an Array (Java)

17

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

Page 18: CS350: Data Structures Queues · 2017-12-09 · CS350: Data Structures Queues • Queues are another very common data structure that can be used for a variety of data storage purposes

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; }

Page 19: CS350: Data Structures Queues · 2017-12-09 · CS350: Data Structures Queues • Queues are another very common data structure that can be used for a variety of data storage purposes

CS350: Data Structures

Queue Implementation Using an Array (Java)

19

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

Page 20: CS350: Data Structures Queues · 2017-12-09 · CS350: Data Structures Queues • Queues are another very common data structure that can be used for a variety of data storage purposes

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++; }

Page 21: CS350: Data Structures Queues · 2017-12-09 · CS350: Data Structures Queues • Queues are another very common data structure that can be used for a variety of data storage purposes

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; }

Page 22: CS350: Data Structures Queues · 2017-12-09 · CS350: Data Structures Queues • Queues are another very common data structure that can be used for a variety of data storage purposes

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; }

Page 23: CS350: Data Structures Queues · 2017-12-09 · CS350: Data Structures Queues • Queues are another very common data structure that can be used for a variety of data storage purposes

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; }

Page 24: CS350: Data Structures Queues · 2017-12-09 · CS350: Data Structures Queues • Queues are another very common data structure that can be used for a variety of data storage purposes

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

Page 25: CS350: Data Structures Queues · 2017-12-09 · CS350: Data Structures Queues • Queues are another very common data structure that can be used for a variety of data storage purposes

CS350: Data Structures

null

Queue Implementation Using a LinkedList

25

NULLA

Enqueue Value: A

head

tail

Page 26: CS350: Data Structures Queues · 2017-12-09 · CS350: Data Structures Queues • Queues are another very common data structure that can be used for a variety of data storage purposes

CS350: Data Structures

Queue Implementation Using a LinkedList

26

nullA

Enqueue Value: B

head

NULLB

tail

Page 27: CS350: Data Structures Queues · 2017-12-09 · CS350: Data Structures Queues • Queues are another very common data structure that can be used for a variety of data storage purposes

CS350: Data Structures

Queue Implementation Using a LinkedList

27

nullA

Enqueue Value: B

head

NULLB

tail

Page 28: CS350: Data Structures Queues · 2017-12-09 · CS350: Data Structures Queues • Queues are another very common data structure that can be used for a variety of data storage purposes

CS350: Data Structures

Queue Implementation Using a LinkedList

28

nullA

Enqueue Value: C

head

nullB

tail

NULLC

Page 29: CS350: Data Structures Queues · 2017-12-09 · CS350: Data Structures Queues • Queues are another very common data structure that can be used for a variety of data storage purposes

CS350: Data Structures

Queue Implementation Using a LinkedList

29

nullA

Enqueue Value: C

head

nullB

tail

NULLC

Page 30: CS350: Data Structures Queues · 2017-12-09 · CS350: Data Structures Queues • Queues are another very common data structure that can be used for a variety of data storage purposes

CS350: Data Structures

Queue Implementation Using a LinkedList

30

Dequeue the Front of the Queue:

head

nullB

tail

NULLC

Page 31: CS350: Data Structures Queues · 2017-12-09 · CS350: Data Structures Queues • Queues are another very common data structure that can be used for a variety of data storage purposes

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

Page 32: CS350: Data Structures Queues · 2017-12-09 · CS350: Data Structures Queues • Queues are another very common data structure that can be used for a variety of data storage purposes

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;

Page 33: CS350: Data Structures Queues · 2017-12-09 · CS350: Data Structures Queues • Queues are another very common data structure that can be used for a variety of data storage purposes

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; }

Page 34: CS350: Data Structures Queues · 2017-12-09 · CS350: Data Structures Queues • Queues are another very common data structure that can be used for a variety of data storage purposes

CS350: Data Structures

Queue Implementation Using a LinkedList (Java)

34

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

Page 35: CS350: Data Structures Queues · 2017-12-09 · CS350: Data Structures Queues • Queues are another very common data structure that can be used for a variety of data storage purposes

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 ); }

Page 36: CS350: Data Structures Queues · 2017-12-09 · CS350: Data Structures Queues • Queues are another very common data structure that can be used for a variety of data storage purposes

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; }