Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 106 Introduction to Computer Science I 12 / 11 / 2006 Instructor: Michael Eckmann.

Similar presentations


Presentation on theme: "CS 106 Introduction to Computer Science I 12 / 11 / 2006 Instructor: Michael Eckmann."— Presentation transcript:

1 CS 106 Introduction to Computer Science I 12 / 11 / 2006 Instructor: Michael Eckmann

2 Michael Eckmann - Skidmore College - CS 106 - Fall 2006 Today’s Topics Comments and/or Questions? Prog 2 and (most) labs will be given back on Wednesday (some will be done by tomorrow which you'll be able to pick up outside the office.) more with Linked lists –implement the Deck class as a linked list Queues Stacks

3 Linked lists Let's implement our deck of cards as a linked list of card nodes instead of as an array of cards. Note well: –A user of the Deck and Card class should not need to know how we decided to store the Deck of Cards. i.e. the implementation details should be hidden from the user of these classes. Deck was implemented as an array of Cards, now we're going to change the implementation to a linked list of Cards and last time we discussed the possibility of storing the Cards in an ArrayList.

4 Linked lists Let's implement our deck of cards as a linked list of card nodes instead of as an array of cards. Consider the shuffle method we had. –we generated 2 random #'s and swapped the cards at those positions. –we did the swap a bunch of times. –assuming we employ this same idea but with a linked list, what cases are there to consider when swapping (i.e. think about that email I sent out with the different cases for insert and delete, etc.) How about the cut method?

5 Linked lists Compare a linked list's behaviors to that of arrays and ArrayLists. – Which are dynamic in length? – How about ease of operations in terms of: speed of execution and programmer ease – Consider these operations: Add Insert Delete

6 Queues and Stacks A queue is a data structure that has the following characteristics – It is linear – Uses FIFO (first in, first out) processing Queue operations – Enqueue – add an item to the rear of the queue – Dequeue – remove an item from the front of the queue – Empty – returns true if the queue is empty What's significant about a queue is that there are no insert in anywhere or remove from anywhere in the queue. The only place to add something to the queue is the rear, and the only place to remove something from the queue is the front.

7 Queues and Stacks A stack is a data structure that has the following characteristics – It is linear – Uses LIFO (last in, first out) processing Stack operations – Push – add an item to the top of the stack – Pop – remove an item from the top of the stack – Empty – returns true if the stack is empty – Peek – retrieve information about the item on top of the stack without removing it The only allowable ways to put an item into and to get an item from the stack is via push and pop. There are no insert in anywhere or remove from anywhere in the stack. What if there was no peek? Is it redundant --- could a series of the existing operations achieve the same functionality.

8 Queues and Stacks Can anyone think of real world examples that are naturally modeled by queues? Can anyone think of a real world example that is naturally modeled by a stack? Let's see visual representations of a queue and a stack on the board.

9 Queues and Stacks Can anyone think of real world examples that are naturally modeled by queues? – Line of people at grocery store checkout – Line of airplanes waiting for takeoff Can anyone think of a real world example that is naturally modeled by a stack? – Plates at a salad bar A customer takes the top plate (pop) When new plates come out, they are “pushed” to the top of the stack. – Why is this example not a queue?

10 Queues and Stacks Recursive method calls A new call to the method causes it's local data to be popped onto the stack The method calls finish in reverse order, so when a method call ends, it's local data is popped off the stack

11 Queues and Stacks Could we implement a Queue with – a linked list – an array Could we implement a Stack with – a linked list – an array

12 Implementing a Stack Properties of a Stack –linear data structure –LIFO processing What operations should be available to Stacks? –push –pop –peek –empty?

13 Implementing a Stack If we were implementing the data on the Stack as an array of elements of some type, we would give a maximum length to our array. What else would we need to store to allow push, pop, peek and empty? to work?

14 Implementing a Stack If we were implementing the data on the Stack as an array of elements of some type, we would give a maximum length to our array. What else would we need to store to allow push, pop, peek and empty? to work? –top_of_stack we have two choices of what this will hold –either the index of the next place to push –or the index of the place to pop So, for an empty stack what value would top_of_stack have ?

15 Implementing a Stack Let's say we're representing a stack of Strings. public class Stack { private String the_stack[]; private int top_of_stack; public Stack(int max_size) { the_stack = new String[max_size]; } // anything else need to go in the constructor?

16 Implementing a Stack Let's say we're representing a stack of Strings. public class Stack { private String the_stack[]; private int top_of_stack; public Stack(int max_size) { the_stack = new String[max_size]; top_of_stack = -1; } // we can continue this in eclipse now.


Download ppt "CS 106 Introduction to Computer Science I 12 / 11 / 2006 Instructor: Michael Eckmann."

Similar presentations


Ads by Google