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 = -1 self._items = Array(ArrayQueue.DEFAULT_CAPACITY)
Array Implementation I DDDD 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!)
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 = -1 self._front = -1 self._items = Array(ArrayQueue.DEFAULT_CAPACITY)
Array Implementation II: Initial State Front of queue Rear of queue
Array Implementation II D Front of queue Rear of queue 00 Add an item
Array Implementation II DD Front of queue Rear of queue 01 Add an item
Array Implementation II DDD Front of queue Rear of queue 02 Add an item
Array Implementation II DDDD Front of queue Rear of queue 03 Add an item
Array Implementation II DDDDD Front of queue Rear of queue 04 Add an item
Array Implementation II DDDD Front of queue Rear of queue 14 Pop an item
Array Implementation II After 5 additions and 2 removals DD Front of queue Rear of queue 24 D Cells 0 and 1 are unavailable and wasted Adjustments must be made when rear or front reaches capacity
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
Array Implementation III After 6 additions and 2 removals DD Front of queue Rear of queue 25 DD Before insertion
Array Implementation III Reset the rear pointer to 0 to wrap around the array DD Front of queue Rear of queue 20 D After insertion D D
from arrays import Array from 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__ front rear items
from arrays import Array from 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 0 front rear 4 DDDDD items
The Array Implementation: add 1 front rear 9 DDDDDDDDD items from arrays import Array from 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
from arrays import Array from 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 1 front rear 0 DDDDDDDDDD items
The Array Implementation: pop 2 front rear 0 DDDDDDDDD items from arrays import Array from 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
For Wednesday Modeling and Simulation