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
Templates in C++ Template function in C++ makes it easier to reuse classes and functions. A template can be viewed as a variable that can be instantiated.
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.
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.
A queue is an ADT which allows data values to be accessed only one at a time and only the first inserted. The rule imposed on a queue is: First In First.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 18: Stacks And Queues.
1 A full binary tree A full binary tree is a binary tree in which all the leaves are on the same level and every non leaf node has two children. SHAPE.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
Chapter 5 Queues Modified. Chapter Scope Queue processing Comparing queue implementations 5 - 2Java Software Structures, 4th Edition, Lewis/Chase.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Queues. What is a queue? First-in first-out data structure (FIFO) New objects are placed at rear Removal restricted to front Examples?
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.
Queues.
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.
CSE 373 Data Structures and Algorithms Lecture 2: Queues.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
Data Structures (part 2). Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that last ‘rush’
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.
CSE 373: Data Structures and Algorithms Lecture 2: Queues.
Chapter 4 ADTs Stack and Queue. 4-2 Formal ADT Specifications The Java interface construct lets us collect together method interfaces into a syntactic.
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.
© 2004 Goodrich, Tamassia Queues. © 2004 Goodrich, Tamassia Stacks2 The Queue ADT The Queue ADT stores arbitrary objects Insertions and deletions follow.
Stacks and Queues. 2 Abstract Data Types (ADTs) abstract data type (ADT): A specification of a collection of data and the operations that can be performed.
Linked Data Structures
18 Chapter Stacks and Queues
Chapter 18: Stacks and Queues.
CSE 373: Data Structures and Algorithms
Stacks and Queues.
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:18 AM Queues 11/16/2018 4:18 AM Queues.
Queues 11/16/2018 4:19 AM Queues 11/16/2018 4:19 AM Queues.
Instructor: Mr.Vahidipour
Queues.
Topic 16 Queues "FISH queue: n.
COMPUTER 2430 Object Oriented Programming and Data Structures I
CS 2430 Object Oriented Programming and Data Structures I
CSC 143 Queues [Chapter 7].
COMPUTER 2430 Object Oriented Programming and Data Structures I
Queues: Implemented using Arrays
ADT list.
COMPUTER 2430 Object Oriented Programming and Data Structures I
Queues 12/30/2018 9:24 PM Queues 12/30/2018 9:24 PM Queues.
COMPUTER 2430 Object Oriented Programming and Data Structures I
slides created by Alyssa Harding
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
Stacks and Queues 1.
slides created by Marty Stepp
CSE 373 Data Structures Lecture 6
COMPUTER 2430 Object Oriented Programming and Data Structures I
CS 2430 Object Oriented Programming and Data Structures I
Circular Queues: Implemented using Arrays
Queues.
Queues Definition of a Queue Examples of Queues
CSE 373 Data Structures Lecture 6
CSCS-200 Data Structure and Algorithms
Lecture 16 Stacks and Queues CSE /26/2018.
Queues Dr. Anwar Ghani Lecture 06: Queue Data Structures
Queues The content for these slides was originally created by Gerard Harrison. Ported to C# by Mike Panitz.
COMPUTER 2430 Object Oriented Programming and Data Structures I
Data Structures & Programming
Presentation transcript:

COMPUTER 2430 Object Oriented Programming and Data Structures I

Queue and Stack Queue: First In First Out (FIFO) Stack: Last in First Out (LIFO) In Out

Queue Implementation // Circular array int front, rear, count; front points to where the first element is rear points to where to add next element (not the last element!)

public class Queue { private Object[] items; private int front, rear, count; public Queue( int size ) public boolean isEmpty() public boolean isFull () public void add ( Object x ) throws Exception public Object remove() throws Exception } // class Queue

items = new Object[N]; front = rear = count = 0; Queue Class items = new Object[N]; front = rear = count = 0; front rear 4 (N-1) 1 3 2

Queue Class Add A Add B front front A A 4 (N-1) 1 B 4 (N-1) 1 3 2 rear A A 4 (N-1) 1 B 4 (N-1) 1 3 2 rear 3 2 rear

Queue Class Add C, D Add E front front rear A A 4 (N-1) 1 E B 4 (N-1) rear A A 4 (N-1) 1 E B 4 (N-1) 1 B D C D C 3 2 rear 3 2

Queue Class After a while Remove twice rear rear 4 (N-1) 1 4 (N-1) 1 rear rear F 4 (N-1) 1 4 (N-1) 1 D front front 3 2 3 2

public class Queue <E> { private E[] items; private int front, rear, count; public Queue( int size ) public boolean isEmpty() public boolean isFull () public void add ( E x ) // rear = (rear + 1) % items.length; public E remove() // front = (front + 1) % items.length; }

Queue Method numItems As Implementer // Returns the number of items in the queue public int numItems() { return count; }

User Method numItems // Do not modify the queue public int numItems(Queue q, int qSize) { Queue tq = new Queue(qSize); int num = 0; while ( !q.isEmpty() ) tq.add( q.remove() ); num ++; } while ( !tq.isEmpty() ) q.add( tq.remove() ); return num; Is the 2nd loop necessary?

User Method numItems public int numItems(Queue q, int qSize) ( Queue tq = new Queue(qSize); int num = 0; while ( !q.isEmpty() ) { tq.add( q.remove() ); num ++; } q = tq; // Can we do this? return num; NO! Pass by Value!

User Method numItems public int numItems(Queue q, int qSize) ( Queue tq = new Queue(qSize); int num = 0; while ( !q.isEmpty() ) { tq.add( q.remove() ); num ++; } while ( !tq.isEmpty() ) q.add( tq.remove() ); return num; The 2nd while loop is necessary! Can you do it without tq?

User Method numItems // If array is allowed public int numItems(Queue q, int qSize) ( // Queue tq = new Queue(qSize); Object [] temp = new Object[qSize]; int num = 0; while ( !q.isEmpty() ) { temp[num ++] = q.remove(); } for (int index = 0; index < num; index ++) q.add( temp[index] ); return num;

User Method numItems // If arrays not allowed! public int numItems(Queue q, int qSize) ( Queue tq = new Queue(qSize); int num = 0; while ( !q.isEmpty() ) { tq.add( q.remove() ); num ++; } while ( !tq.isEmpty() ) q.add( tq.remove() ); return num;

Special Queue Operations Remove the second element of the queue Remove the last element of the queue Switch two elements of the queue User vs Implementer

Quizz 4 Monday, November 5 10 points Queue and Exception

Lab 8

Lab 7 Prog 4