COMPUTER 2430 Object Oriented Programming and Data Structures I

Slides:



Advertisements
Similar presentations
Queues Printer queues Several jobs submitted to printer Jobs form a queue Jobs processed in same order as they were received.
Advertisements

Stacks, Queues, and Linked Lists
Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)
Queues1 Part-B2 Queues. Queues2 The Queue ADT (§4.3) The Queue ADT stores arbitrary objects Insertions and deletions follow the first-in first-out scheme.
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.
Queues CS 308 – Data Structures. What is a queue? It is an ordered group of homogeneous items of elements. Queues have two ends: –Elements are added at.
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.
What is a Queue? n Logical (or ADT) level: A queue is an ordered group of homogeneous items (elements), in which new elements are added at one end (the.
Queues CS 3358 – Data Structures. What is a queue? It is an ordered group of homogeneous items of elements. Queues have two ends: – Elements are added.
What is a Queue? A queue is a FIFO “first in, first out” structure.
Chapter 5 ADTs Stack and Queue. 2 Goals Describe a stack and its operations at a logical level Demonstrate the effect of stack operations using a particular.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
Tirgul 3 Subjects of this Tirgul: Linked Lists Doubly-Linked Lists Sparse Matrices Stack Queue.
Unit 11 1 Unit 11: Data Structures H We explore some simple techniques for organizing and managing information H This unit focuses on: Abstract Data Types.
1 C++ Plus Data Structures Nell Dale Queues ADTs Stack and Queue Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus Modified.
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation - FIFO As items are added they are chronologically ordered, items are removed.
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation - FIFO As items are added they are chronologically ordered, items are removed.
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation – FIFO As items are added they are chronologically ordered, items are removed.
CS 2430 Day 26. Announcements Exam #2: Wednesday, April 3 –Review in lab on Tuesday, April 2 –Sample problems sent via .
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
Lecture7: Queue Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
Lab 7 Queue ADT. OVERVIEW The queue is one example of a constrained linear data structure. The elements in a queue are ordered from least recently added.
Stacks And Queues Chapter 18.
CS 2430 Day 24. Announcements Quiz this Friday Program 5 posted on Monday Program 4 due date: Friday at 10pm Program 4 grace date: Wednesday at 10pm (don’t.
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.
Spring 2008 Mark Fontenot CSE Honors Principles of Computer Science I Note Set 15 1.
Chapter 4 ADTs Stack and Queue. 4-2 Formal ADT Specifications The Java interface construct lets us collect together method interfaces into a syntactic.
Queues CS 367 – Introduction to Data Structures. Queue A queue is a data structure that stores data in such a way that the last piece of data stored,
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 18: Stacks and Queues.
Queues By Jimmy M. Lu. Overview Definition Standard Java Queue Operations Implementation Queue at Work References.
Queues1 © 2014 Goodrich, Tamassia, Goldwasser Presentation for use with the textbook Data Structures and Algorithms in Java, 6 th edition, by M. T. Goodrich,
Queues.
Chapter 18: Stacks and Queues.
Stacks and Queues.
Queues Queues Queues.
Stack and Queue APURBO DATTA.
CSCE 3110 Data Structures & Algorithm Analysis
Queues What is a queue? Queue Implementations: As Array
COMPUTER 2430 Object Oriented Programming and Data Structures I
CMSC 341 Lecture 5 Stacks, Queues
Queues 11/9/2018 6:28 PM Queues 11/9/2018 6:28 PM Queues.
Chapter 19: Stacks and Queues.
Queues 11/16/2018 4:19 AM Queues 11/16/2018 4:19 AM Queues.
Circular queue.
Queues.
Chapter 5 ADTs Stack and Queue Fall 2013 Yanjun Li CS2200.
Queues 11/22/2018 6:47 AM 5.2 Queues Queues Dr Zeinab Eid.
COMPUTER 2430 Object Oriented Programming and Data Structures I
COMPUTER 2430 Object Oriented Programming and Data Structures I
CS 2430 Object Oriented Programming and Data Structures I
CSC 143 Queues [Chapter 7].
Queues 12/3/2018 Queues © 2014 Goodrich, Tamassia, Goldwasser Queues.
COMPUTER 2430 Object Oriented Programming and Data Structures I
Queues: Implemented using Arrays
Queues 3/9/15 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser,
COMPUTER 2430 Object Oriented Programming and Data Structures I
Queues 12/30/2018 9:24 PM Queues 12/30/2018 9:24 PM Queues.
Cs212: Data Structures Computer Science Department Lecture 7: Queues.
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.
COMPUTER 2430 Object Oriented Programming and Data Structures I
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
COMPUTER 2430 Object Oriented Programming and Data Structures I
Queues Jyh-Shing Roger Jang (張智星)
COMPUTER 2430 Object Oriented Programming and Data Structures I
Circular Queues: Implemented using Arrays
Queues.
Queues: Implemented using Linked Lists
Stacks, Queues, and Deques
Queues What is a queue? Queue Implementations: As Array
COMPUTER 2430 Object Oriented Programming and Data Structures I
Presentation transcript:

COMPUTER 2430 Object Oriented Programming and Data Structures I

Queue Container Normally fixed size (could grow if needed) FIFO: First In First Out Example Mongolian Grill Printing jobs Network routing . . .

Queue Implementation An array Elements are added at the end Elements are removed from the front

Queue Empty queue Add item1 Item1 Add item2 Item1 Item2 Add item3 Remove the first one Item2 Item3 Add item4 Item2 Item3 Item4

First In First Out (FIFO) In at one end and Out at the other end Queue First In First Out (FIFO) In at one end and Out at the other end Out In

Public Queue Methods add (at the end) remove (from the front) isEmpty isFull

Queue Implementation I Add is easy Remove has to move all remaining elements Remove is always from slot 0 count also indicates where next element goes

Queue Implementation II Add at the end and moves rear Remove at the front and moves pointer front Rear is not count any more! front points to where the first element is rear points to where next element goes

Queue Implementation II Very efficient Need to keep count Trouble when hit the end of array front points to where the first element is rear points to where next element goes

Queue Implementation III Make the array circular Need to keep count front points to where the first element is rear points to where next element goes

front rear count: 0 Add . . . front rear count: 7 Remove front rear count: 6 Add && Remove front rear count: 8 Add rear front count: 9

rear front count: 9 Add rear front count: 10 Remove rear front count: 9 Add && Remove rear front count: 6 Remove front rear count: 5

Full: count == items.length Empty: count == 0 Add && Remove front rear count: 8 Add . . . front rear count: 12 Add rear front count: 13 Add front rear count: 14 Full: count == items.length Empty: count == 0

Add Check isFull first or Throw Exception items[rear ++] = obj; front rear Add Check isFull first or Throw Exception front rear rear front rear front items[rear ++] = obj; if (rear == items.length) rear = 0; count ++; items[rear] = obj; rear = (rear + 1) % items.length; count ++;

Remove Check isEmpty first or Throw Exception obj = items[front ++]; rear front rear front front rear front rear obj = items[front ++]; if (front == items.length) front = 0; count --; obj = items[front]; front = (front + 1) % items.length; count --;

public class Queue { private Object[] items; private int front, rear, count; public Queue( int size ) items = new Object[size]; front = rear = count = 0; } public boolean isEmpty() return count == 0; public boolean isFull () return count == items.length; ... } // class Queue

public class Queue { private Object[] items; private int front, rear, count; ... // what if queue is full? public void add ( Object x ) items[rear] = x; rear = (rear + 1) % items.length; count ++; } // what if queue is empty? public Object remove() Object x = items[front]; front = (front + 1) % items.length; count --; return x; } // class Queue

// Generic Queue class public class Queue <E> { private E[] items; private int front, rear, count; // Java warning possible and it’s OK public Queue( int size ) items = (E[]) new Object[size]; front = rear = count = 0; } . . .

Due October 31, at 11 pm No Grace Date! Lab 8 Due October 31, at 11 pm No Grace Date!

Schedule Lab 7 (5points) Prog4 (20 points) Quiz 4 Test 2 Due 11 pm, November 2 Prog4 (20 points) Due 11 pm, November 9 Quiz 4 Monday, Nov 5 Test 2 Wednesday, Nov 14