CSE 12 – Basic Data Structures Cynthia Bailey Lee Some slides and figures adapted from Paul Kube’s CSE 12 CS2 in Java Peer Instruction Materials by Cynthia.

Slides:



Advertisements
Similar presentations
Lists, Stacks, Queues Svetlin Nakov Telerik Corporation
Advertisements

§3 The Stack ADT 1. ADT A stack is a Last-In-First-Out (LIFO) list, that is, an ordered list in which insertions and deletions are.
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)
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.
Stacks, Queues, and Deques. 2 A stack is a last in, first out (LIFO) data structure Items are removed from a stack in the reverse order from the way they.
queues1 Queues Data structures that wait their turn.
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.
CS 206 Introduction to Computer Science II 03 / 04 / 2009 Instructor: Michael Eckmann.
Queues CS-212 Dick Steflik. Queues First In, First Out operation – FIFO As items are added they are chronologically ordered, items are removed in their.
Queues 4/14/2017 5:24 PM 5.2 Queues Queues Dr Zeinab Eid.
Queue Overview Queue ADT Basic operations of queue
COP3538 – Data Structures Using OOP Chapter 4 – Stacks and Queues.
CSC 212 – Data Structures. Using Stack Stack Limitations  Great for Pez dispensers, JVMs,& methods  All of these use most recent item added only 
CSE 12 – Basic Data Structures Cynthia Bailey Lee Some slides and figures adapted from Paul Kube’s CSE 12 CS2 in Java Peer Instruction Materials by Cynthia.
Queues. What is a queue? First-in first-out data structure (FIFO) New objects are placed at rear Removal restricted to front Examples?
1 Lecture 24 Abstract Data Types (ADT) –I Overview  What is an Abstract Data type?  What is Stack ADT?  Stack ADT Specifications  Array Implementation.
Queues What is a queue? Queue Implementations: –As Array –As Circular Array –As Linked List Applications of Queues. Priority queues.
CSC 212 Stacks & Queues. Announcement Daily quizzes accepted electronically only  Submit via one or other Dropbox  Cannot force you to compile & test.
Stacks, Queues, and Deques
Stacks, Queues, and Deques. A stack is a last in, first out (LIFO) data structure –Items are removed from a stack in the reverse order from the way they.
Queues What is a Queue? Queue Implementations: Queue As Array
Stacks, Queues, and Deques
CSE 373 Data Structures and Algorithms Lecture 2: Queues.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
CSE 12 – Basic Data Structures Cynthia Bailey Lee Some slides and figures adapted from Paul Kube’s CSE 12 CS2 in Java Peer Instruction Materials by Cynthia.
CSE 12 – Basic Data Structures Cynthia Bailey Lee Some slides and figures adapted from Paul Kube’s CSE 12 CS2 in Java Peer Instruction Materials by Cynthia.
CS 106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
Data Structures and Java CS 105. L7: Java Slide 2 Data structure Data structure defined: A systematic way of organizing and accessing data Examples Dictionary:
CSE 12 – Basic Data Structures Cynthia Bailey Lee Some slides and figures adapted from Paul Kube’s CSE 12 CS2 in Java Peer Instruction Materials by Cynthia.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
CSE 12 – Basic Data Structures Cynthia Bailey Lee Some slides and figures adapted from Paul Kube’s CSE 12 CS2 in Java Peer Instruction Materials by Cynthia.
1 Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
Lecture7: Queue Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
CSE 12 – Basic Data Structures Cynthia Bailey Lee Some slides and figures adapted from Paul Kube’s CSE 12 CS2 in Java Peer Instruction Materials by Cynthia.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
Stacks and Queues. 2 3 Runtime Efficiency efficiency: measure of computing resources used by code. can be relative to speed (time), memory (space), etc.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
CSE 12 – Basic Data Structures Cynthia Bailey Lee Some slides and figures adapted from Paul Kube’s CSE 12 CS2 in Java Peer Instruction Materials by Cynthia.
CSE 12 – Basic Data Structures Cynthia Bailey Lee Some slides and figures adapted from Paul Kube’s CSE 12 CS2 in Java Peer Instruction Materials by Cynthia.
Question of the Day  Three people check into a hotel for which they pay the manager $30. The manager finds out the rate is $25 and gives $5 to the bellboy.
Introduction Queue is a linear Data Structure in which the operations are performed based on FIFO (First In First Out) principle.
Question of the Day  How can you change the position of 1 toothpick and leave the giraffe in exactly the same form, but possibly mirror-imaged or oriented.
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.
Question of the Day  How can you change the position of 1 toothpick and leave the giraffe in exactly the same form, but possibly mirror-imaged or oriented.
CH 5 : STACKS, QUEUES, AND DEQUES ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA.
Chapter 4 ADTs Stack and Queue. 4-2 Formal ADT Specifications The Java interface construct lets us collect together method interfaces into a syntactic.
 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.
Queues By Jimmy M. Lu. Overview Definition Standard Java Queue Operations Implementation Queue at Work References.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 240 Abstract Data Types Queues Dale Roberts, Lecturer
Queues Queues Queues.
Topic 16 Queues Adapted from Mike Scott’s materials.
CMSC 341 Lecture 5 Stacks, Queues
Queues.
Stacks and Queues.
Queues 11/22/2018 6:47 AM 5.2 Queues Queues Dr Zeinab Eid.
COMPUTER 2430 Object Oriented Programming and Data Structures I
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,
Stacks and Queues CLRS, Section 10.1.
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Queues Jyh-Shing Roger Jang (張智星)
Stacks and Queues.
CSE 12 – Basic Data Structures
CE 221 Data Structures and Algorithms
Stacks, Queues, and Deques
Stacks and Queues.
Data Structures & Programming
Presentation transcript:

CSE 12 – Basic Data Structures Cynthia Bailey Lee Some slides and figures adapted from Paul Kube’s CSE 12 CS2 in Java Peer Instruction Materials by Cynthia Lee is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License. Based on a work at Permissions beyond the scope of this license may be available at LeeCreative Commons Attribution-NonCommercial 4.0 International Licensehttp://peerinstruction4cs.org

Today’s Topics 1. Queues 2. Midterm review 2

Reading quiz!

Reading Quiz! A queue is also called a...? A. LIFO B. FILO C. FIFO D. LILO

Reading Quiz! When an item is removed from a queue, it is...? A. The one that has been in the queue for the longest time B. The one that has been in the queue for the shortest time C. The smallest one in the queue D. The largest one in the queue E. Other/none/more

Reading Quiz! When using enqueue() to place the following items into a queue: enqueue(32) enqueue(65) enqueue(0) enqueue(23) enqueue(-1) the output when dequeueing from the queue is: A. -1, 23, 0, 65, 32 B. -1, 0, 23, 32, 65 C. 65, 32, 23, 0, -1 D. 32, 65, 0, 23, -1 E. Other/none/more

Queues

Which of the following are behaviors inherent in queues, which are decisions we'll make in specifying the behavior of our queue, which are decisions we'll make when implementing our queue. 1. When you dequeue, you get out the oldest element in the queue 2. When you insert, the element is inserted at the beginning of the linked list. 3. When you dequeue an empty queue, it throws an exception. InherentSpecifyingImplementing A.12 3 B C D E. None of these is correct Queue Design

Consider the following implementation of a Queue that extends LinkedList and inherits all of its methods. public class Queue extends LinkedList { private int size=0; public int size(){ return size; } public void enqueue(E e){ add(e); size+=1; } public E dequeue() { size-=1; return removeFirst(); } What does this code output? Queue q= new Queue (); q.enqueue(10); q.enqueue(20); q.clearAll(); System.out.println(q.size()); A: 0 B: 2 C: throws a runtime exception (probably NoSuchMethodException) D: throws a checked exception; this block should have a try-catch block E: it won't even compile!

Consider the following implementation of a Queue that extends LinkedList and inherits all of its methods. public class Queue extends LinkedList { private int size=0; public int size(){ return size; } public void enqueue(E e){ add(e); size+=1; } public E dequeue() { size-=1; return removeFirst(); } What does this code output? Queue q= new Queue (); q.enqueue(10); q.enqueue(20); q.clearAll(); System.out.println(q.size()); A: 0 B: 2 C: throws a runtime exception (probably NoSuchMethodException) D: throws a checked exception; this block should have a try-catch block E: it won't even compile!

Queue should not extend LinkedList A Queue IS NOT a LinkedList! There are things you can do to a LinkedList, such as call clearAll() to remove all elements, that you should not be able to do with a Queue. You should encapsulate both the LinkedList and the size variable as instance variables inside your Queue class.

Consider the following approaches to determine whether the size() method in LinkedList is O(1) A: Insert one element and time how long the size method takes; compare this to how long the size method takes if you had inserted 100 elements. O(1) means they should be about equal. B: Insert a million elements and time how long the size method takes; compare this to how long the size method takes if you had inserted 10 million elements. O(1) means they should be about equal. C: Insert 10 billion elements and time how long the size method takes; compare this to how long the size method takes if you had inserted 100 billion elements. O(1) means they should be about equal. D: All 3 will work E: At least 1 of A,B,C won't work (consider your reasoning as to why when we go over each answer)

public E dequeue(){ // potential issue if empty, for now, assume not empty E e = array[front]; return e; } Select the correct code to insert from below: A front+ +; B rear = rear-1; C for(int i= 0; i<rear; i++) { array[i] = array[i+1]; } rear = rear -1; D None of these are correct

public void enqueue( E element){ // potential issue if full, for now, assume room front++; } Select the correct code to insert from below: A array[0] = e; B array[front] = e; C for(int i= 0; i<front; i++) { array[i+1] = array[i]; } array[front] = e; D None of these are correct

public E dequeue(){ // potential issue if empty, // for now, assume not empty size--; E e = array[front]; return e; } Select the correct code to insert from below: A front+ +; if(front == array.length) front = 0; B rear = rear-1; if(rear <0) rear = array.length-1; C for(int i= 0; i<rear; i++) { array[i] = array[i+1]; } rear = rear -1; if(rear < 0) rear = array.length-1; D None of these are correct

public void enqueue(E e){ // potential issue if full, // for now, assume not full size++; } Select the correct code to insert from below: A rear+ +; if(rear == array.length) rear = 0; array[rear] = e; B rear++ array[rear] =e; C for(int i= front; i<rear; i++) { array[i] = array[i+1]; } array[rear] = e; front--; D None of these are correct

Suppose our array is full and you try to enqueue, your coworker suggests just throwing an exception because it would be easy to implement? A. Sounds like a good idea. B. No, you need to resize and copy, fixing front and rear pointers. C. I have a better idea.

Suppose we have a Queue implemented with a circular array. The capacity is 10 and the size is 5. Which are not legal values for front and rear ? frontrear A05 B59 C72 D94 EAll are legal values

Suppose we have a Queue implemented with a circular array. The capacity is 10 and the size is 5. Which are not legal values for front and rear ? frontrear A05 B59 C72 D94 EAll are legal values

A if (front==rear) return 0; return rear-front; B if (rear>front) return rear-front; return front-rear; C if (front==0 && front==rear) return 0; if (front>rear) return front-rear; return rear-front; D if (front>rear) return front-rear; if (front==rear) return 0; return rear-front; E if (front==rear) return 0; if (front>rear) return front-rear; if (rear>front) return rear-front; Which of these could be the body of the size() method for a Queue that uses a circular array implementation?

A if (front==rear) return 0; return rear-front; B if (rear>front) return rear-front; return front-rear; C if (front==0 && front==rear) return 0; if (front>rear) return front-rear; return rear-front; D if (front>rear) return front-rear; if (front==rear) return 0; return rear-front; E if (front==rear) return 0; if (front>rear) return front-rear; if (rear>front) return rear-front; Which of these could be the body of the size() method for a Queue that uses a circular array implementation?

Midterm Practice

True/false section