Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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

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

2 Michael Eckmann - Skidmore College - CS 106 - Fall 2006 Today’s Topics Comments and/or Questions? Prog 2 grading –I sent an email with some commentary on some ways you could get points off –I still need to do the EC for some of you --- if you have nothing written in the Extra Credit: ________ it means I haven't checked it out yet. –If you have a 0 there it means I don't believe I got EC from you. Queues Stacks

3 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

4 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.

5 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.

6 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.

7 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?

8 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

9 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

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

11 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?

12 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 ?

13 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?

14 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.

15 Implementing a Queue What ideas do you have?


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

Similar presentations


Ads by Google