1 מבוא למדעי המחשב הרצאה 21: Queue, Iterator & Iterable.

Slides:



Advertisements
Similar presentations
STACKS & QUEUES. Stacks Abstract data types An abstract data type (ADT) is an abstraction of a data structure An ADT specifies : –Data stored –Operations.
Advertisements

© 2004 Goodrich, Tamassia Queues1. © 2004 Goodrich, Tamassia Queues2 The Queue ADT (§4.3) The Queue ADT stores arbitrary objects Insertions and deletions.
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.
5/4/2015ITK 2751 Queue a FIFO (First In First Out) efbh front rear poll, remove offer peek, element capacity size.
Lab 1: 1. Download all my programs in your computer under the same folder. 2. The tree shown in the following figure represents an expression: (((( 3 +
Queues 4/14/2017 5:24 PM 5.2 Queues Queues Dr Zeinab Eid.
Elementary Data Structures CS 110: Data Structures and Algorithms First Semester,
1 Queues (5.2) CSE 2011 Winter May Announcements York Programming Contest Link also available from.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 The Stack ADT (§4.2) The Stack ADT stores arbitrary objects Insertions and deletions.
Computer Science 209 Software Development Iterators.
COMP 103 Linked Stack and Linked Queue.
Queues. What is a queue? First-in first-out data structure (FIFO) New objects are placed at rear Removal restricted to front Examples?
Queues.
1 Lecture 26 Abstract Data Types –III Overview  Creating and manipulating Linked List.  Linked List Traversal.  Linked List Traversal using Iterator.
Queues What is a queue? Queue Implementations: –As Array –As Circular Array –As Linked List Applications of Queues. Priority queues.
מחסנית (Stack): מבנה נתונים עליו מוגדרות הפעולות הבאות:
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.
1 Foundations of Software Design Fall 2002 Marti Hearst Lecture 13: Queues and Vectors.
תרגול 12: ומחסניתIteratorרשימה מקושרת, תור, 1. רשימה מקושרת (Linked List) רשימה מקושרת הינה קבוצה סדורה של אובייקטים, כאשר כל אובייקט ברשימה מכיל הצבעה.
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.
1 Lecture 26 Abstract Data Types – IV Overview  The List ADT  Implementing Stacks as Linked List  Linked List Implementation of Queues .  Preview:
ומחסניתIteratorרשימה מקושרת, תור,
© 2004 Goodrich, Tamassia Queues1. © 2004 Goodrich, Tamassia Queues2 The Queue ADT (§4.3) The Queue ADT stores arbitrary objects Insertions and deletions.
Queues What is a Queue? Queue Implementations: Queue As Array
CS 1031 Queues Definition of a Queue Examples of Queues Design of a Queue Class Different Implementations of the Queue Class.
Jan 12, 2012 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
CS 2430 Day 35. Agenda Introduction to linked lists Bag as linked list Stack as linked list.
COMP 103 Linked Lists. 2 RECAP-TODAY RECAP  Linked Structures: LinkedNode  Iterating and printing Linked Nodes  Inserting and removing Linked Nodes.
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’
09-1 Queues and List-Based ADT Implementations Problem Set: PS3 due Wednesday, March 7 Wellesley College CS230 Lecture 09 Monday, February 26 Handout #18.
Lecture7: Queue Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
בתרגול הקודם כללי הרשאות (Visibility modifiers) בהורשה – Public – Protected – private חוקי גישה לשדות ושיטות בהורשה ב -Java מחלקות אבסטרקטיות –המילה השמורה.
Chapter Objectives  Learn how to represent a waiting line (queue)  Become proficient using the methods in the Queue  Understand how to implement the.
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.
Chapter 8 Queue I CS Data Structures I COSC2006 April 24, 2017
2013-T2 Lecture 18 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and John.
תרגול 12: Iterator מחסנית תור 1. מחסנית (stack) מחסנית (stack) היא מבנה נתונים שמזכיר מחסנית של רובה : האיבר שנכנס ראשון למחסנית יוצא ממנה אחרון (LIFO.
CS 367 Introduction to Data Structures Lecture 5.
1 Stacks (Continued) and Queues Array Stack Implementation Linked Stack Implementation The java.util.Stack class Queue Abstract Data Type (ADT) Queue ADT.
Queue. Avoid confusion Britain Italy 6 Applications of Queues Direct applications –Waiting lists, bureaucracy –Access to shared resources (e.g.,
Queue. The Queue ADT Insertions and deletions follow the first-in first-out scheme Insertions are at the rear of the queue and removals are at the front.
Topic 13 Iterators. 9-2 Motivation We often want to access every item in a data structure or collection in turn We call this traversing or iterating over.
2015-T2 Lecture 19 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and John.
Chapter 4 ADTs Stack and Queue. 4-2 Formal ADT Specifications The Java interface construct lets us collect together method interfaces into a syntactic.
Object Oriented Programming in Java Habib Rostami Lecture 7.
1 Example: LinkedStack LinkedStack UML Class Diagram LinkedStack Class LinkedStack Attributes/Constructor LinkedStack Methods LinkedStack iterator method.
1 Queues (Continued) Queue ADT Linked queue implementation Array queue implementation Circular array queue implementation Deque Reading L&C , 9.3.
© 2004 Goodrich, Tamassia Queues. © 2004 Goodrich, Tamassia Stacks2 The Queue ADT The Queue ADT stores arbitrary objects Insertions and deletions follow.
Queues.
Software Development Iterators
Queues Queues Queues.
Queues What is a queue? Queue Implementations: As Array
Queues 11/9/2018 6:28 PM Queues 11/9/2018 6:28 PM Queues.
Queues 11/9/2018 6:32 PM 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.
Circular queue.
Queue.
Queues 11/22/2018 6:47 AM 5.2 Queues Queues Dr Zeinab Eid.
ArraySet Methods and ArrayIterator
הרצאה 21: Queue, Iterator & Iterable
בניית מחסנית סטטית Static Stack Implementation מורים מובילים תשע"ה
CSE 143 Lecture 27: Advanced List Implementation
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,
Queues 12/30/2018 9:24 PM Queues 12/30/2018 9:24 PM Queues.
Example: LinkedSet<T>
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Queues What is a Queue? Queue Implementations: As Array
Queues What is a queue? Queue Implementations: As Array
Presentation transcript:

1 מבוא למדעי המחשב הרצאה 21: Queue, Iterator & Iterable

2

3 תור – מבנה נתונים אבסטרקטי public interface Queue { public void enqueue(Object o); public Object dequeue(); public boolean isEmpty(); }

4 תור – שימושים בעולם התוכנה השימושים של תורים בעולם התוכנה מזכירים מאוד תורים במציאות : מקלדת שידור סרט באינטרנט (YouTube) שימוש ברשת לטובת מימוש של טלפון (VoIP) ועוד...

5 מימוש נאיבי לתור front = 0 numOfElements = 0

6 מימוש נאיבי לתור front = 0 enqueue (A) A numOfElements = 1

7 מימוש נאיבי לתור front = 0 enqueue (B) A B numOfElements = 2

8 מימוש נאיבי לתור front = 0 enqueue (C) A B numOfElements = 3 C

9 מימוש נאיבי לתור front = 1 dequeue () B numOfElements = 2 C

10 מימוש נאיבי לתור front = 1 enqueue (D) B numOfElements = 3 C D

11 מימוש נאיבי לתור front = 2 dequeue () numOfElements = 2 C D

12 תור מעגלי בעל קיבולת חסומה A B C n-1 1 front numOfElements = 3

13 תור מעגלי בעל קיבולת חסומה B C n-1 1 front numOfElements = 2 dequeue ()

14 תור מעגלי בעל קיבולת חסומה B C n-1 1 front numOfElements = 3 enqueue (D) D

15 תור מעגלי בעל קיבולת חסומה W X 0 1 n-1 front numOfElements = 4 n-2 U V

תור מעגלי בעל קיבולת חסומה public class CircularQueue implements Queue{ private Array arr; private int front, numOfElements, capacity; public CircularQueue(int capacity){ this.capacity = capacity; arr = new FixedSizeArray(capacity); front = 0; numOfElements = 0; }

תור מעגלי בעל קיבולת חסומה public Object dequeue(){ if (isEmpty()){ throw new EmptyQueueException(); } Object res = arr.get(front); arr.set(front, null); front = (front+1) % capacity; numOfElements = numOfElements-1; return res; }

תור מעגלי בעל קיבולת חסומה public void enqueue(Object o){ if (numOfElements == arr.size()){ throw new RuntimeException( "Queue is full!"); } arr.set((front + numOfElements) % capacity, o); numOfElements = numOfElements+1; } public boolean isEmpty(){ return numOfElements == 0; } } //class CircularQueue

19 יצירת סוג חדש של Exception class EmptyQueueException extends RuntimeException{ public EmptyQueueException(){ super(); } }//class EmptyQueueException

20 מימוש תור בעזרת מחסנית public class QueueAsStack implements Queue{ private Stack stack; public QueueAsStack () { stack = new StackAsArray(); } public boolean isEmpty() {// easy... return stack.isEmpty(); } public void enqueue(Object o) {//quit easy as well... stack.push(o); } }

21 מימוש תור בעזרת מחסנית public Object dequeue() { // hard work... if (stack.isEmpty()) throw new EmptyQueueException(); Stack auxStack = new StackAsArray(); while(!stack.isEmpty()) auxStack.push(stack.pop()); Object ret = auxStack.pop(); while(!auxStack.isEmpty()) stack.push(auxStack.pop()); return ret; } }//class QueueAsStack

22 ניתוח הפעולות ( לתור ולמחסנית ) פעולות יעילות הכנסת מספר קטן של פריטים הוצאת איבר ראשון פעולות לא יעילות הכנסת מספר גדול של פריטים ( תלוי במימוש המערך ) מציאת איבר בעל ערך מינימאלי מציאת איבר בעל מפתח מסוים

23 Iterator כיצד ניתן לאפשר בנאי מעתיק של מבנה הנתונים Set שלמדנו ? כיצד ניתן לבצע חיתוך או איחוד בין שתי קבוצות ? ישנו צורך בפונקציונאליות חשובה ברוב מבני הנתונים שעד כה התעלמנו ממנה – היכולת לעבור על כל האיברים.

24 Iterator public interface Iterator{ public boolean hasNext(); public Object next(); public void remove(); } public interface Iterable { public Iterator iterator(); }

25 נעדכן את ממשק הקבוצה public interface Set extends Iterable{ public void add(Object data); public void remove(Object data); public boolean contains(Object data); public int size(); }

26 נוסיף את השיטה הדרושה במימוש הקבוצה public class SetAsArray implements Set { private Array arr; private int size; //... public Iterator iterator() { return new ArrayIterator(arr, size); } }

27 נגדיר איטרטור עבור מערכים public class ArrayIterator implements Iterator { private Array arr; private int nextIx, size; public ArrayIterator(Array arr, int size) { this.arr = arr; this.size = size; nextIx = 0; }

28 נגדיר איטרטור עבור מערכים public boolean hasNext() { return nextIx < size; } public Object next() { if (!hasNext()) throw new NoSuchElementException(); nextIx = nextIx+1; return arr.get(nextIx-1); } public void remove() { throw new UnsupportedOperationException(); } } //class ArrayIterator

29 נוסיף בנאי מעתיק במימוש הקבוצה public class SetAsArray implements Set { private Array arr; int size; public SetAsArray(){ arr = new DynamicArray(); size = 0; } public SetAsArray(Set toCopy){ this(); if (toCopy == null) throw new NullPointerException("arguemnt to constructor is null"); Iterator iter = toCopy.iterator(); while (iter.hasNext()) add(iter.next()); }... }