# 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.

## Presentation on theme: "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."— 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 http://peerinstruction4cs.org. Permissions beyond the scope of this license may be available at http://peerinstruction4cs.org.Cynthia LeeCreative Commons Attribution-NonCommercial 4.0 International Licensehttp://peerinstruction4cs.org

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

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. 2 1 3 C. 1 3 2 D. 2 3 1 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 { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/13/4151550/slides/slide_13.jpg", "name": "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

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 { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/13/4151550/slides/slide_14.jpg", "name": "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

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 { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/13/4151550/slides/slide_17.jpg", "name": "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

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 { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/13/4151550/slides/slide_18.jpg", "name": "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

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

Download ppt "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."

Similar presentations