Date post: | 21-Dec-2015 |
Category: |
Documents |
View: | 217 times |
Download: | 3 times |
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
5
Queue Running Times
• What is the running time of each operation?• Enqueue
O(1)
• DequeueO(1)
• isEmpty()O(1)
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
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
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)
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 ()