Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Computer Science 112 Fundamentals of Programming II Array-Based Queues."— Presentation transcript:

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

2 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)

3 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!)

4 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)

5 Array Implementation II: Initial State Front of queue Rear of queue

6 Array Implementation II D Front of queue Rear of queue 00 Add an item

7 Array Implementation II DD Front of queue Rear of queue 01 Add an item

8 Array Implementation II DDD Front of queue Rear of queue 02 Add an item

9 Array Implementation II DDDD Front of queue Rear of queue 03 Add an item

10 Array Implementation II DDDDD Front of queue Rear of queue 04 Add an item

11 Array Implementation II DDDD Front of queue Rear of queue 14 Pop an item

12 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

13 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

14 Array Implementation III After 6 additions and 2 removals DD Front of queue Rear of queue 25 DD Before insertion

15 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

16 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

17 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

18 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

19 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

20 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

21 For Wednesday Modeling and Simulation


Download ppt "Computer Science 112 Fundamentals of Programming II Array-Based Queues."

Similar presentations


Ads by Google