21
Computer Science 112 Fundamentals of Programming II Array-Based Queues

Computer Science 112 Fundamentals of Programming II Array-Based Queues

Embed Size (px)

Citation preview

Page 1: Computer Science 112 Fundamentals of Programming II Array-Based Queues

Computer Science 112

Fundamentals of Programming IIArray-Based Queues

Page 2: Computer Science 112 Fundamentals of Programming II Array-Based Queues

Array Implementation I

• Maintain a rear pointer to the index of the most recently added item

• The index of the front of the queue is always 0

self._rear = -1self._items = Array(ArrayQueue.DEFAULT_CAPACITY)

Page 3: Computer Science 112 Fundamentals of Programming II Array-Based Queues

Array Implementation I

D D D D

0 1 2 3 4 5

Front of queue Rear of queue 3

• Rear is always logical size - 1

• Resize the array when we run out of room

• Shift to the left when we pop (yikes, a linear operation!)

Page 4: Computer Science 112 Fundamentals of Programming II Array-Based Queues

Array Implementation II

• Keep separate pointers to front and rear

• Don’t shift items during a pop, but let the front pointer move to the right

self._rear = -1self._front = -1self._items = Array(ArrayQueue.DEFAULT_CAPACITY)

Page 5: Computer Science 112 Fundamentals of Programming II Array-Based Queues

Array Implementation II: Initial State

0 1 2 3 4 5

Front of queue Rear of queue-1 -1

Page 6: Computer Science 112 Fundamentals of Programming II Array-Based Queues

Array Implementation II

D

0 1 2 3 4 5

Front of queue Rear of queue0 0

Add an item

Page 7: Computer Science 112 Fundamentals of Programming II Array-Based Queues

Array Implementation II

D D

0 1 2 3 4 5

Front of queue Rear of queue0 1

Add an item

Page 8: Computer Science 112 Fundamentals of Programming II Array-Based Queues

Array Implementation II

D D D

0 1 2 3 4 5

Front of queue Rear of queue0 2

Add an item

Page 9: Computer Science 112 Fundamentals of Programming II Array-Based Queues

Array Implementation II

D D D D

0 1 2 3 4 5

Front of queue Rear of queue0 3

Add an item

Page 10: Computer Science 112 Fundamentals of Programming II Array-Based Queues

Array Implementation II

D D D D D

0 1 2 3 4 5

Front of queue Rear of queue0 4

Add an item

Page 11: Computer Science 112 Fundamentals of Programming II Array-Based Queues

Array Implementation II

D D D D

0 1 2 3 4 5

Front of queue Rear of queue1 4

Pop an item

Page 12: Computer Science 112 Fundamentals of Programming II Array-Based Queues

Array Implementation II

• After 5 additions and 2 removals

D D

0 1 2 3 4 5

Front of queue Rear of queue2 4

D

• Cells 0 and 1 are unavailable and wasted

• Adjustments must be made when rear or front reaches capacity

Page 13: Computer Science 112 Fundamentals of Programming II Array-Based Queues

Array Implementation III

• Same as previous version, but manages a circular array

• When either front or rear reaches the last cell, the pointer is reset to 0 to wrap around the array on the next removal or addition

Page 14: Computer Science 112 Fundamentals of Programming II Array-Based Queues

Array Implementation III

• After 6 additions and 2 removals

D D

0 1 2 3 4 5

Front of queue Rear of queue2 5

D DBeforeinsertion

Page 15: Computer Science 112 Fundamentals of Programming II Array-Based Queues

Array Implementation III

• Reset the rear pointer to 0 to wrap around the array

D D

0 1 2 3 4 5

Front of queue Rear of queue2 0

DAfterinsertion D D

Page 16: Computer Science 112 Fundamentals of Programming II Array-Based Queues

from arrays import Arrayfrom abstractqueue import AbstractQueue

class ArrayQueue(AbstractQueue):

DEFAULT_CAPACITY = 10

def __init__(self, sourceCollection = None) self._front = self._rear = -1 self._items = Array(ArrayQueue.DEFAULT_CAPACITY) AbstractQueue.__init__(self, sourceCollection)

The Array Implementation: __init__

-1frontrear -1 0 1 2 3 4 5 6 7 8 9

items

Page 17: Computer Science 112 Fundamentals of Programming II Array-Based Queues

from arrays import Arrayfrom abstractqueue import AbstractQueue

class ArrayQueue(AbstractQueue):

. . .

def add(self, item): # Resize array if full if self.isEmpty(): self._front = self._rear = 0 elif self._rear == len(self._items) - 1: self._rear = 0 else: self._rear += 1 self._items[self._rear] = item self._size += 1

The Array Implementation: add

0frontrear 4

D D D D D0 1 2 3 4 5 6 7 8 9

items

Page 18: Computer Science 112 Fundamentals of Programming II Array-Based Queues

The Array Implementation: add

1frontrear 9

D D D D D D D D D0 1 2 3 4 5 6 7 8 9

items

from arrays import Arrayfrom abstractqueue import AbstractQueue

class ArrayQueue(AbstractQueue):

. . .

def add(self, item): # Resize array if full if self.isEmpty(): self._front = self._rear = 0 elif self._rear == len(self._items) - 1: self._rear = 0 else: self._rear += 1 self._items[self._rear] = item self._size += 1

Page 19: Computer Science 112 Fundamentals of Programming II Array-Based Queues

from arrays import Arrayfrom abstractqueue import AbstractQueue

class ArrayQueue(AbstractQueue):

. . .

def pop(self): # Check precondition here data = self._items[self._front] self._size -= 1 if self.isEmpty(): self._front = self._rear = -1 elif self._front == len(self._items) - 1: self._front = 0 else: self._front += 1 # Resize array here if necessary return data

The Array Implementation: pop

1frontrear 0

D D D D D D D D D D0 1 2 3 4 5 6 7 8 9

items

Page 20: Computer Science 112 Fundamentals of Programming II Array-Based Queues

The Array Implementation: pop

2frontrear 0

D D D D D D D D D0 1 2 3 4 5 6 7 8 9

items

from arrays import Arrayfrom abstractqueue import AbstractQueue

class ArrayQueue(AbstractQueue):

. . .

def pop(self): # Check precondition here data = self._items[self._front] self._size -= 1 if self.isEmpty(): self._front = self._rear = -1 elif self._front == len(self._items) - 1: self._front = 0 else: self._front += 1 # Resize array here if necessary return data

Page 21: Computer Science 112 Fundamentals of Programming II Array-Based Queues

For Wednesday

Modeling and Simulation