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

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

Computer Science 112 Fundamentals of Programming II List Iterators.
Computer Science 112 Fundamentals of Programming II Queues and Priority Queues.
Queues. Queue Definition Ordered list with property: All insertions take place at one end (tail) All insertions take place at one end (tail) All deletions.
Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)
COSC 1P03 Data Structures and Abstraction 9.1 The Queue Whenever you are asked if you can do a job, tell 'em, "Certainly, I can!" Then get busy and find.
IKI 10100I: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100I: Data.
Data Structure (Part I) Stacks and Queues. Introduction to Stack An stack is a ordered list in which insertion and deletions are made at one end. –The.
Introduction to C Programming CE Lecture 12 Circular Queue and Priority Queue Data Structures.
CS 206 Introduction to Computer Science II 03 / 04 / 2009 Instructor: Michael Eckmann.
CHAPTER 7 Queues.
Fundamentals of Python: From First Programs Through Data Structures
MSc.It :- Ali Abdul Karem Habib Kufa University / mathematics & Science Of Computer.
Data Structure Dr. Mohamed Khafagy.
Today’s Agenda  Stacks  Queues  Priority Queues CS2336: Computer Science II.
COMPSCI 105 SS 2015 Principles of Computer Science Queues.
 Abstract Data Type Abstract Data Type  What is the difference? What is the difference?  Stacks Stacks  Stack operations Stack operations  Parsing.
Queue RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
CS 206 Introduction to Computer Science II 10 / 22 / 2008 Instructor: Michael Eckmann.
Lecture 12 – ADTs and Stacks.  Modularity  Divide the program into smaller parts  Advantages  Keeps the complexity managable  Isolates errors (parts.
Computer Science 112 Fundamentals of Programming II Lists.
Fundamentals of Programming II Inheritance and Abstract Classes
Queue, Deque, and Priority Queue Implementations Chapter 14.
Data Structures: Lists i206 Fall 2010 John Chuang Some slides adapted from Glenn Brookshear, Brian Hayes, or Marti Hearst.
CS 206 Introduction to Computer Science II 10 / 20 / 2008 Instructor: Michael Eckmann.
Queue using an array. .head.tail Pointers head and tail always point to the first empty slot before or after elements in the list. Thus, initially they.
CS 206 Introduction to Computer Science II 10 / 26 / 2009 Instructor: Michael Eckmann.
Circular queue. Array-based Queue Use an array of size N in a circular fashion Three variables keep track of the front, rear, and size f index of the.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
CHP-4 QUEUE.
Queue 1 Operations on Queues A Dequeue Operation An Enqueue Operation Array Implementation Link list Implementation Examples.
© 2006 Pearson Addison-Wesley. All rights reserved8 A-1 Chapter 8 Queues.
Chapter 16 Stacks & Queues. Objective In this chapter we will learn:  Stacks  Queues  Different implementations (arrays and linked list) of both 
Queues Tonga Institute of Higher Education. Definitions Queue - A data structure that stores a number of items. The first item entered into a queue is.
Computer Science 112 Fundamentals of Programming II Interfaces and Implementations.
Computer Science 112 Fundamentals of Programming II Binary Search Trees.
Stacks And Queues Chapter 18.
Queue 09/10/081. Queue (Linear Queue) It is a linear data structure consisting of list of items. In queue, data elements are added at one end, called.
Computer Science 112 Fundamentals of Programming II Implementation Strategies for Unordered Collections.
Data Structures Chapter 6. Data Structure A data structure is a representation of data and the operations allowed on that data. Examples: 1.Array 2.Record.
CHP-4 QUEUE. 1.INTRODUCTION  A queue is a linear list in which elements can be added at one end and elements can be removed only at other end.  That.
Chapter 8 Queues. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 The Abstract Data Type Queue A queue –New items enter at the back, or rear, of.
Advanced Higher Computing Science Stacks Queues and linked lists.
Stack and Queues Part 2. Priority Queues Priority Queues (cont’) A priority queue is a more specialized data structure than a stack or a queue. However,
Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
Queue, Deque, and Priority Queue Implementations Chapter 23.
Give Eg:? Queues. Introduction DEFINITION: A Queue is an ordered collection of element in which insertions are made at one end and deletions are made.
Stacks and Queues CMSC 201. Stacks and Queues Sometimes, when we use a data-structure in a very specific way, we have a special name for it. This is to.
1 Midterm 1 on Friday February 12 Closed book, closed notes No computer can be used 50 minutes 4 questions Write a function Write program fragment Explain.
Computer Science 112 Fundamentals of Programming II Iterators.
1 Data Structures CSCI 132, Spring 2014 Lecture 7 Queues.
 In general, Queue is line of person waiting for their turn at some service counter like ticket window at cinema hall, at bus stand or at railway station.
1 Lecture 9: Stack and Queue. What is a Stack Stack of Books 2.
STACKS & QUEUES for CLASS XII ( C++).
Fundamentals of Programming II Linked Lists
Review Array Array Elements Accessing array elements
Data Structures Using C, 2e
COSC160: Data Structures: Lists and Queues
Fundamentals of Programming II Working with Arrays
Fundamentals of Programming II Binary Search Trees
Fundamentals of Programming II Interfaces and Implementations
Queues Queues Queues.
Stack and Queue APURBO DATTA.
Computer Science 112 Fundamentals of Programming II
DATA STRUCTURE QUEUE.
Linear Data Structures Chapter 3
Revised based on textbook author’s notes.
Stack A data structure in which elements are inserted and removed only at one end (called the top). Enforces Last-In-First-Out (LIFO) Uses of Stacks Evaluating.
Circular Queues: Implemented using Arrays
CSCS-200 Data Structure and Algorithms
Presentation transcript:

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