cs350: data structures queues - github...

36
CS350: Data Structures © James Moscola James Moscola Department of Physical Sciences York College of Pennsylvania CS350: Data Structures Queues James Moscola Department of Physical Sciences York College of Pennsylvania

Upload: others

Post on 17-Jul-2020

22 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS350: Data Structures Queues - GitHub Pagesycpcs.github.io/cs350-fall2014/lectures/Queues_lecture.pdf · CS350: Data Structures Queue Implementation Using an Array 21! /** * Return

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 Physical SciencesYork College of Pennsylvania

CS350: Data StructuresQueuesJames MoscolaDepartment of Physical SciencesYork College of Pennsylvania

Page 2: CS350: Data Structures Queues - GitHub Pagesycpcs.github.io/cs350-fall2014/lectures/Queues_lecture.pdf · CS350: Data Structures Queue Implementation Using an Array 21! /** * Return

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 - GitHub Pagesycpcs.github.io/cs350-fall2014/lectures/Queues_lecture.pdf · CS350: Data Structures Queue Implementation Using an Array 21! /** * Return

CS350: Data Structures

Queue Operations

• Stacks have two main operations - Enqueue - inserts an element into the back of the queue- Dequeue - removes a single element from the front of the queue

3

Page 4: CS350: Data Structures Queues - GitHub Pagesycpcs.github.io/cs350-fall2014/lectures/Queues_lecture.pdf · CS350: Data Structures Queue Implementation Using an Array 21! /** * Return

CS350: Data Structures

Queue Operations

4

Start with Empty Queue

FrontBack

Page 5: CS350: Data Structures Queues - GitHub Pagesycpcs.github.io/cs350-fall2014/lectures/Queues_lecture.pdf · CS350: Data Structures Queue Implementation Using an Array 21! /** * Return

CS350: Data Structures

Queue Operations

5

Enqueue Value: A

A

FrontBack

Page 6: CS350: Data Structures Queues - GitHub Pagesycpcs.github.io/cs350-fall2014/lectures/Queues_lecture.pdf · CS350: Data Structures Queue Implementation Using an Array 21! /** * Return

CS350: Data Structures

Queue Operations

6

A

FrontBack

B

Enqueue Value: B

Page 7: CS350: Data Structures Queues - GitHub Pagesycpcs.github.io/cs350-fall2014/lectures/Queues_lecture.pdf · CS350: Data Structures Queue Implementation Using an Array 21! /** * Return

CS350: Data Structures

Queue Operations

7

A

FrontBack

BC

Enqueue Value: C

Page 8: CS350: Data Structures Queues - GitHub Pagesycpcs.github.io/cs350-fall2014/lectures/Queues_lecture.pdf · CS350: Data Structures Queue Implementation Using an Array 21! /** * Return

CS350: Data Structures

FrontBack

Queue Operations

8

ABC

Dequeue the Front of the Queue:

Page 9: CS350: Data Structures Queues - GitHub Pagesycpcs.github.io/cs350-fall2014/lectures/Queues_lecture.pdf · CS350: Data Structures Queue Implementation Using an Array 21! /** * Return

CS350: Data Structures

Queue Interface

9

!public interface Queue<AnyType> {! public void enqueue( AnyType x ); public AnyType dequeue( );! public AnyType getFront( );! public boolean isEmpty( );! public void makeEmpty( );}

Page 10: CS350: Data Structures Queues - GitHub Pagesycpcs.github.io/cs350-fall2014/lectures/Queues_lecture.pdf · CS350: Data Structures Queue Implementation Using an Array 21! /** * Return

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 - GitHub Pagesycpcs.github.io/cs350-fall2014/lectures/Queues_lecture.pdf · CS350: Data Structures Queue Implementation Using an Array 21! /** * Return

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 - GitHub Pagesycpcs.github.io/cs350-fall2014/lectures/Queues_lecture.pdf · CS350: Data Structures Queue Implementation Using an Array 21! /** * Return

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 - GitHub Pagesycpcs.github.io/cs350-fall2014/lectures/Queues_lecture.pdf · CS350: Data Structures Queue Implementation Using an Array 21! /** * Return

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 - GitHub Pagesycpcs.github.io/cs350-fall2014/lectures/Queues_lecture.pdf · CS350: Data Structures Queue Implementation Using an Array 21! /** * Return

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 - GitHub Pagesycpcs.github.io/cs350-fall2014/lectures/Queues_lecture.pdf · CS350: Data Structures Queue Implementation Using an Array 21! /** * Return

CS350: Data Structures

A

Queue Implementation Using an Array

15

Dequeue the Front of the Queue:

-1

head

4 3 2 1 0

tail

BC A

Page 16: CS350: Data Structures Queues - GitHub Pagesycpcs.github.io/cs350-fall2014/lectures/Queues_lecture.pdf · CS350: Data Structures Queue Implementation Using an Array 21! /** * Return

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 - GitHub Pagesycpcs.github.io/cs350-fall2014/lectures/Queues_lecture.pdf · CS350: Data Structures Queue Implementation Using an Array 21! /** * Return

CS350: Data Structures

Queue Implementation Using an Array

17

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

Page 18: CS350: Data Structures Queues - GitHub Pagesycpcs.github.io/cs350-fall2014/lectures/Queues_lecture.pdf · CS350: Data Structures Queue Implementation Using an Array 21! /** * Return

CS350: Data Structures

Queue Implementation Using an Array

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 - GitHub Pagesycpcs.github.io/cs350-fall2014/lectures/Queues_lecture.pdf · CS350: Data Structures Queue Implementation Using an Array 21! /** * Return

CS350: Data Structures

Queue Implementation Using an Array

19

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

Page 20: CS350: Data Structures Queues - GitHub Pagesycpcs.github.io/cs350-fall2014/lectures/Queues_lecture.pdf · CS350: Data Structures Queue Implementation Using an Array 21! /** * Return

CS350: Data Structures

Queue Implementation Using an Array

20

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

Page 21: CS350: Data Structures Queues - GitHub Pagesycpcs.github.io/cs350-fall2014/lectures/Queues_lecture.pdf · CS350: Data Structures Queue Implementation Using an Array 21! /** * Return

CS350: Data Structures

Queue Implementation Using an Array

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[ front ]; front = increment( front ); return returnValue; }

Page 22: CS350: Data Structures Queues - GitHub Pagesycpcs.github.io/cs350-fall2014/lectures/Queues_lecture.pdf · CS350: Data Structures Queue Implementation Using an Array 21! /** * Return

CS350: Data Structures

Queue Implementation Using an Array

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 - GitHub Pagesycpcs.github.io/cs350-fall2014/lectures/Queues_lecture.pdf · CS350: Data Structures Queue Implementation Using an Array 21! /** * Return

CS350: Data Structures

Queue Implementation Using an Array

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++, front = increment( front ) ) newArray[ i ] = theArray[ front ];! theArray = newArray; front = 0; back = currentSize - 1; }

Page 24: CS350: Data Structures Queues - GitHub Pagesycpcs.github.io/cs350-fall2014/lectures/Queues_lecture.pdf · CS350: Data Structures Queue Implementation Using an Array 21! /** * Return

CS350: Data Structures

Queue Implementation Using a LinkedList

24

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

front

front points to the front of the linked list

null

backback points to the back of

the linked list

Page 25: CS350: Data Structures Queues - GitHub Pagesycpcs.github.io/cs350-fall2014/lectures/Queues_lecture.pdf · CS350: Data Structures Queue Implementation Using an Array 21! /** * Return

CS350: Data Structures

null

Queue Implementation Using a LinkedList

25

nullA

Enqueue Value: A

front

back

Page 26: CS350: Data Structures Queues - GitHub Pagesycpcs.github.io/cs350-fall2014/lectures/Queues_lecture.pdf · CS350: Data Structures Queue Implementation Using an Array 21! /** * Return

CS350: Data Structures

Queue Implementation Using a LinkedList

26

nullA

Enqueue Value: B

front

nullB

back

Page 27: CS350: Data Structures Queues - GitHub Pagesycpcs.github.io/cs350-fall2014/lectures/Queues_lecture.pdf · CS350: Data Structures Queue Implementation Using an Array 21! /** * Return

CS350: Data Structures

Queue Implementation Using a LinkedList

27

nullA

Enqueue Value: B

front

nullB

back

Page 28: CS350: Data Structures Queues - GitHub Pagesycpcs.github.io/cs350-fall2014/lectures/Queues_lecture.pdf · CS350: Data Structures Queue Implementation Using an Array 21! /** * Return

CS350: Data Structures

Queue Implementation Using a LinkedList

28

nullA

Enqueue Value: C

front

nullB

back

nullC

Page 29: CS350: Data Structures Queues - GitHub Pagesycpcs.github.io/cs350-fall2014/lectures/Queues_lecture.pdf · CS350: Data Structures Queue Implementation Using an Array 21! /** * Return

CS350: Data Structures

Queue Implementation Using a LinkedList

29

nullA

Enqueue Value: C

front

nullB

back

nullC

Page 30: CS350: Data Structures Queues - GitHub Pagesycpcs.github.io/cs350-fall2014/lectures/Queues_lecture.pdf · CS350: Data Structures Queue Implementation Using an Array 21! /** * Return

CS350: Data Structures

Queue Implementation Using a LinkedList

30

Dequeue the Front of the Queue:

front

nullB

back

nullC

Page 31: CS350: Data Structures Queues - GitHub Pagesycpcs.github.io/cs350-fall2014/lectures/Queues_lecture.pdf · CS350: Data Structures Queue Implementation Using an Array 21! /** * Return

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 - GitHub Pagesycpcs.github.io/cs350-fall2014/lectures/Queues_lecture.pdf · CS350: Data Structures Queue Implementation Using an Array 21! /** * Return

CS350: Data Structures

Queue Implementation Using a LinkedList

32

/** * Queue constructor */ public ListQueue( ) { front = back = null; }! private ListNode<AnyType> front; private ListNode<AnyType> back;

Page 33: CS350: Data Structures Queues - GitHub Pagesycpcs.github.io/cs350-fall2014/lectures/Queues_lecture.pdf · CS350: Data Structures Queue Implementation Using an Array 21! /** * Return

CS350: Data Structures

Queue Implementation Using a LinkedList

33

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

Page 34: CS350: Data Structures Queues - GitHub Pagesycpcs.github.io/cs350-fall2014/lectures/Queues_lecture.pdf · CS350: Data Structures Queue Implementation Using an Array 21! /** * Return

CS350: Data Structures

Queue Implementation Using a LinkedList

34

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

Page 35: CS350: Data Structures Queues - GitHub Pagesycpcs.github.io/cs350-fall2014/lectures/Queues_lecture.pdf · CS350: Data Structures Queue Implementation Using an Array 21! /** * Return

CS350: Data Structures

Queue Implementation Using a LinkedList

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 back = front = new ListNode<AnyType>( x ); else // Regular case back = back.next = new ListNode<AnyType>( x ); }

Page 36: CS350: Data Structures Queues - GitHub Pagesycpcs.github.io/cs350-fall2014/lectures/Queues_lecture.pdf · CS350: Data Structures Queue Implementation Using an Array 21! /** * Return

CS350: Data Structures

Queue Implementation Using a LinkedList

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 = front.element; front = front.next; return returnValue; }