+ All Categories
Home > Documents > 1 Foundations of Software Design Fall 2002 Marti Hearst Lecture 13: Queues and Vectors.

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

Date post: 21-Dec-2015
Category:
View: 217 times
Download: 3 times
Share this document with a friend
Popular Tags:
18
1 Foundations of Software Design Fall 2002 Marti Hearst Lecture 13: Queues and Vectors
Transcript

1

Foundations of Software DesignFall 2002Marti Hearst

Lecture 13: Queues and Vectors 

 

2

Queues

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

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

A Comparison

• How are queues similar to / different from stacks?

5

Queue Running Times

• What is the running time of each operation?• Enqueue

O(1)

• DequeueO(1)

• isEmpty()O(1)

6

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

Use of Queues in the OS

http://courses.cs.vt.edu/~csonline/OS/Lessons/Processes/index.html

Processes waitingin a queue

9

Use of Queues in Distributed Systems

Animation byRemzi Arpaci-Dusseau

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

Java Vector API

12

Java Vector API

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

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

A Comparison

• How are queues similar to / different from vectors?

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

18


Recommended