Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Foundations of Software Design Fall 2002 Marti Hearst Lecture 13: Queues and Vectors.

Similar presentations


Presentation on theme: "1 Foundations of Software Design Fall 2002 Marti Hearst Lecture 13: Queues and Vectors."— Presentation transcript:

1 1 Foundations of Software Design Fall 2002 Marti Hearst Lecture 13: Queues and Vectors

2 2 Queues http://www.lib.uconn.edu/DoddCenter/ASC/Wilcox/Students.htm

3 3 Queue Container of objects that are inserted and removed according to the principle of –First-in-first-out –FIFO Objects can be inserted at any time, but only the least recently inserted can be removed at any time. Operations: –Enqueue: put item onto queue –Dequeue: remove item from queue

4 4 A Comparison How are queues similar to / different from stacks?

5 5 Queue Running Times What is the running time of each operation? Enqueue O(1) Dequeue O(1) isEmpty() O(1)

6 6

7 7 How are Queues Used? Queues are used extensively in –The OS For scheduling processes to receive resources –Computer networking For keeping storing and sending network packets

8 8 Use of Queues in the OS http://courses.cs.vt.edu/~csonline/OS/Lessons/Processes/index.html Processes waiting in a queue

9 9 Use of Queues in Distributed Systems Animation by Remzi Arpaci-Dusseau

10 10 Sequences, Lists, & Vectors There are many ways to implement sequences of items In order of abstractness: –Sequence > List > Vector Know about Vectors in Java –Can be more intuitive to program with –But can be less efficient –Implements the Enumeration interface

11 11 Java Vector API

12 12 Java Vector API

13 13 Java Vectors When you insert an element into a Java Vector, it moves all the elements behind the new one back one position –insertElementAt When you delete an element, is moves all the elements behind that one up one position –removeElementAt –removeElement

14 14 Vectors in Java How do they differ from arrays? –Can grow the length dynamically –Can insert items into any position –Have a different API (set of method calls) What are the running times of the operations? –boolean isEmpty() O(1) –Object firstElement() O(1) –boolean contains(Object elem) O(n) –Object elementAt(int index) O(1)

15 15 A Comparison How are queues similar to / different from vectors?

16 16 Let’s Write Code! Implement a Queue using Vectors What is the API? –void enQueue(Object o) –Object deQueue() –int size () –boolean isEmpty () –Object front ()

17 17

18 18


Download ppt "1 Foundations of Software Design Fall 2002 Marti Hearst Lecture 13: Queues and Vectors."

Similar presentations


Ads by Google