CSC153, Class 48: Linear Structures Overview: * Deletion in linked lists * Overview of linear structures * Stacks * Queues * Implementation notes Notes: * Reminder: CSC151 has cool project presentations Monday at 2:15 * Read more of Bailey * Sorry, exams not graded yet * Food: Dried fruit, chocolate Reminder: Linked lists implement lists a lot like Scheme does, with "Nodes" that contain a value and a successor ---------------------------------------- Lists are comparatively complex collections We often want simpler collections with only a few operations: * add something to the collection * get something from the collection, removing it after getting it * check if the collection is empty * check if the collection is full Someone decided that we should define an ADT called the "Linear Structure" that supports these operations What policy for get? * "Random" * "Easiest for programmer" * "Most recently added" LIFO, "Stack" * "Least recently added", FIFO "Queue" * "Most important", "Priority Queue" * "Least important", "Priority Queue" Other possible methods that don't overly complicate the structures? * peek, like get but no removal * size Note: Once we've defined Linear we can write public interface Queue extends Linear { } // interface Queue Why bother? * So that people can say "I want a queue" when they want a queue and not "I want a linear structure" and hope to get a queue. To a traditional computer scientist, the primary Queue methods are enqueue and dequeue "When you write queue ten thousand times, it starts to get freaky." To a traditional computer scientist, the primary Stack methods are push and pop. How can you implement queues, stacks, and other linear structures? * Use Nodes (without previous) * Use an array * Use a list public class BriansQueue implements Queue { // Fields Node first; Node last; public Object get() throws EmptyStructureExcepction { if (this.first == null) throw new EmptyStructureException; Object tmp = this.first.value; this.first = this.first.next; return tmp; } public void add(Object value) { this.last.next = new Node(value); this.last = this.last.next; } // add(Object) } // class BriansQueue FOR MONDAY, FIGURE OUT WHAT'S WRONG WITH EACH 1. Think about empty queues ENJOY WALTZ. BEHAVE RESPONSIBLY.