CS231 lecture notes, Fall 1999 Week 7, Wednesday GONNA BE DRACONIAN ABOUT THE ASSIGNMENT ON FRIDAY! Bring two copies of the homework: one to hand in, one to have in front of you while we discuss. Queueness --------- Like a stack, queue can contain any object. Order of removal is order of insertion (no priorities, no comparability requirement) stack is LIFO, queue is FIFO Applications of queues 1) buffer between fast CPU and slow devices Example: send something to the printer and then go do other things 2) buffer between fast network and slow Example: fireline with and without storage 3) simulation of real-world queues... Queueing theory Good results, like one-line versus many, on-ramp metering, feedback queueing in operating systems. Evil mind games like making us walk farther in airports, or lines at Disney World. Implementations --------------- Stack: Linked list... nice because adding to the beginning and removing from the beginning are easy Array... use a pointer to top of Stack (next empty one or last filled one)? Copy when full. Vector? Queue: Linked List: not as much fun, unless you keep a tail pointer! Array: Circular buffer! Implementation of stacks ------------------------ a) using arrays b) using lists (this time the lists are implemented internally) Most of this is almost identical to the array implementation of PriorityQueue: public class Stack { private int count; private int capacity; private int capacityIncrement; private Object[] itemArray; public Stack () { count = 0; capacity = 10; capacityIncrement = 10; itemArray = new Object[capacity]; } public boolean empty () { return (count == 0); } public void push (Object obj) { if (count == capacity) { capacity += capacityIncrement; capacityIncrement *= 2; Object[] tempArray = new Object[capacity]; for (int i=0; i