Presentation is loading. Please wait.

Presentation is loading. Please wait.

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


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:

1 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

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

3 Reading quiz!

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

5 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

6 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

7 Queues

8 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

9 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!

10 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!

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

12 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)

13 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

14 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

15

16

17 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

18 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

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

20 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

21 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

22 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?

23 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?

24 Midterm Practice

25 True/false section

26


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


Ads by Google