Presentation is loading. Please wait.

Presentation is loading. Please wait.

Winter 2006CISC121 - Prof. McLeod1 Stuff Solution to midterm is posted. Marking has just started… Lab for this week is not posted (yet?). Final exam (full.

Similar presentations


Presentation on theme: "Winter 2006CISC121 - Prof. McLeod1 Stuff Solution to midterm is posted. Marking has just started… Lab for this week is not posted (yet?). Final exam (full."— Presentation transcript:

1 Winter 2006CISC121 - Prof. McLeod1 Stuff Solution to midterm is posted. Marking has just started… Lab for this week is not posted (yet?). Final exam (full of good news today!) is scheduled on April 27 (Thursday) at 2pm in Jock Hardy (Clergy). 3 hours long.

2 Winter 2006CISC121 - Prof. McLeod2 Last Time Replacing sparse tables with linked lists. Started Stacks and Queues

3 Winter 2006CISC121 - Prof. McLeod3 Today Finish looking at stacks Queues

4 Winter 2006CISC121 - Prof. McLeod4 Stacks – Cont. Another example are those plate dispensers in cafeterias. A stack follows the “Last In, First Out” or “LIFO” principle. A stack would have the following operations: –clear() – clear the stack. –isEmpty() – check to see if the stack is empty. –isFull() – check to see if the stack is full. –push(element) - put the element on top of the stack. –pop() – take the topmost element from the stack. –peek() – return the topmost element in the stack without removing it.

5 Winter 2006CISC121 - Prof. McLeod5 Stacks – Cont. See IntStack.java for a linked list implementation. See IntStackSmaller.java for an even smaller linked list implementation! Some features of IntStackSmaller: –An inner class for the node. –Only public methods are: clear () boolean isEmpty () push (int) int pop () int peek ()

6 Winter 2006CISC121 - Prof. McLeod6 Stacks – Cont. A stack can also be implemented with an ArrayList or even an array (But not as well, IMHO). Note that none of the stack operations require iteration through the linked list.

7 Winter 2006CISC121 - Prof. McLeod7 ArrayList Stack Implementation See ALStack.java (Note that we are assuming the user will check to see if the stack is empty before calling a “pop()” operation. What else could we do?) “Features”: –We need to keep track of position in the ArrayList. –We could use (but did not) the automatic “un-boxing” and boxing feature in Java 5.0 (huh?).

8 Winter 2006CISC121 - Prof. McLeod8 Array Stack Implementation See ArrayStack.java A bit clumsy? Of the three implementations, which is the best?

9 Winter 2006CISC121 - Prof. McLeod9 A Stack in Use How to add numbers that are too large to be stored in a long type variable? See LongAddition.java

10 Winter 2006CISC121 - Prof. McLeod10 The Stack Class in java.util java.util has a “ Stack ” class that implements the above methods using a Vector as the storage object. (A Vector is like an ArrayList…) Stack is a sub-class of Vector, and as such, inherits all the Vector methods.

11 Winter 2006CISC121 - Prof. McLeod11 The Stack Class – Cont. Unique Stack methods: boolean empty() // same as isEmpty Object peek() Object pop() Object push(element) int search(target) // returns position of target in stack, if not found –1 is returned. Stack() // constructor

12 Winter 2006CISC121 - Prof. McLeod12 The Stack Class – Cont. In Stack, “ push ” also returns a reference to the Object added to the stack, so both peek and push can change that topmost object on the stack. Since Stack “is a” Vector, methods like “ setElementAt ” and “ removeElementAt ” can be used on stack elements – but these methods would be illegal by our definition of what a stack is! Also, when Vector ’s are used to implement the stack, re-sizing of the Vector can greatly slow down the push method.

13 Winter 2006CISC121 - Prof. McLeod13 The Stack Class – Cont. For these reasons it is better to implement a stack as we have done above, using a private linked list (best) or a private array(next best) as a data object within the definition of the class. Implementing a stack using a linked list defined using an inner class for the node provides better information hiding than the Stack class.

14 Winter 2006CISC121 - Prof. McLeod14 Queues A queue is just a lineup, like at your favorite movie theatre. It would use the “FIFO” principle – “First In, First Out”. It would have the following operations: –clear() – clear the queue. –isEmpty() – check to see if the queue is empty. –isFull() – check to see if the queue is full. –enqueue(element) - put the element at the end of the queue. –dequeue() – take the first element from the queue. –firstEl() – return the first element in the queue without removing it.

15 Winter 2006CISC121 - Prof. McLeod15 Aside: “isFull()” Our implementations of stacks and queues do not have an “ isFull() ” method, because they do not need one. If you did need such a method to model a stack or queue that is limited in size, how would you do it? Suppose the maximum size is provided when the stack or queue is created (in the constructor).

16 Winter 2006CISC121 - Prof. McLeod16 Queues - Cont. Does a linked list implementation of a queue require both head and tail links? If a singly linked list is used would you enqueue to the head or the tail? Why? YUP! - enqueue to tail! - easier to remove head node than to remove tail node in singly linked list for dequeue operation

17 Winter 2006CISC121 - Prof. McLeod17 Queues – Cont. A queue can easily be implemented using a singly linked list with a head and tail. (See IntQueue.java, for example) A queue, in code, is often used as part of a model of a real-world process. In the “real-world” queues are everywhere – Airport runways, McDonalds, banks, assembly lines, etc.

18 Winter 2006CISC121 - Prof. McLeod18 Queues – Cont. How would you implement a queue with an ArrayList or an array? Not so easy, right? A “circular array” can be used, with variables that point to the first and last occupied positions in the array. If “last” moves to the end of the array, it can be wrapped back to the beginning. Linked lists really are the best way to implement a queue.

19 Winter 2006CISC121 - Prof. McLeod19 Queues – Priority Queue A Priority Queue is just a queue where the elements are also given a priority. In this case, an element with a higher priority can be removed before the element that is “first” in the the queue, when the first element is of a lower priority. (Like how an ambulance gets through traffic…)

20 Winter 2006CISC121 - Prof. McLeod20 Back to Stacks - Activation Frames Stacks are an integral part of computer architecture. For example, Java byte code is run by the “Java Virtual Machine”, or “JVM”. The JVM is stack – based. Each thread (or process…) in the JVM has its own private run-time stack in memory (our programs are “single threaded”). Each run-time stack contains “activation frames” – one for each method that has been activated. Only one activation frame (or method) can be active at a time for each thread. An activation frame is created, or “pushed”, every time a method is invoked. When the method is completed, the frame is popped off the stack.

21 Winter 2006CISC121 - Prof. McLeod21 Activation Frames An activation frame (or “stack frame” or “activation record”) contains (among other things): –All local variables. –A link to the frame of the calling method, so that control can be returned to line of code in the caller immediately after the method call. –Information for catching exceptions. –An “operand stack”, which is another stack that is used by the JVM as a source of arguments and a repository of results. An understanding of how this particular stack works will help to explain how recursive methods work…

22 Winter 2006CISC121 - Prof. McLeod22 Demonstration Run “RunTimeStackDemo.java” in debug mode, stepping though method calls and observe thread stack display.

23 Winter 2006CISC121 - Prof. McLeod23 Summary - Stacks & Queues We will use the thread stack to help explain how recursion works - later... Stacks & Queues are often used to model real- world processes. Providing an easy-to-use stack or queue object makes the modeling effort easier. Now we have to move on to something completely different!


Download ppt "Winter 2006CISC121 - Prof. McLeod1 Stuff Solution to midterm is posted. Marking has just started… Lab for this week is not posted (yet?). Final exam (full."

Similar presentations


Ads by Google