CS 206 Introduction to Computer Science II 10 / 15 / 2008 Instructor: Michael Eckmann.

Slides:



Advertisements
Similar presentations
Data Structures Through C
Advertisements

§3 The Stack ADT 1. ADT A stack is a Last-In-First-Out (LIFO) list, that is, an ordered list in which insertions and deletions are.
CS 206 Introduction to Computer Science II 02 / 27 / 2009 Instructor: Michael Eckmann.
Stacks & Their Applications COP Stacks  A stack is a data structure that stores information arranged like a stack.  We have seen stacks before.
C o n f i d e n t i a l Developed By Nitendra NextHome Subject Name: Data Structure Using C Title : Overview of Stack.
CS 206 Introduction to Computer Science II 03 / 04 / 2009 Instructor: Michael Eckmann.
 Balancing Symbols 3. Applications
CS 206 Introduction to Computer Science II 10 / 22 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 03 / 02 / 2009 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 12 / 06 / 2006 Instructor: Michael Eckmann.
Topic 15 Implementing and Using Stacks
CS 206 Introduction to Computer Science II 09 / 10 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 03 / 06 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 17 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 11 / 04 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 20 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 01 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 14 / 2009 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 12 / 11 / 2006 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 13 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 26 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 03 / 16 / 2009 Instructor: Michael Eckmann.
Stack: Linked List Implementation Push and pop at the head of the list New nodes should be inserted at the front of the list, so that they become the top.
TCSS 342, Winter 2005 Lecture Notes
CS 206 Introduction to Computer Science II 03 / 30 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 16 / 2009 Instructor: Michael Eckmann.
Topic 15 Implementing and Using Stacks
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Data Structures Stacks.
CS 106 Introduction to Computer Science I 12 / 13 / 2006 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 08 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 02 / 25 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 28 / 2009 Instructor: Michael Eckmann.
The Stack and Queue Types Lecture 10 Hartmut Kaiser
Objectives of these slides:
Implementing Stacks Ellen Walker CPSC 201 Data Structures Hiram College.
Week7 Stack Data Structures & Algorithms. Introduction to Stacks and Queues Widely used data structures Ordered List of element Easy to implement Easy.
CSC 205 Programming II Postfix Expressions. Recap: Stack Stack features Orderly linear structure Access from one side only – top item Stack operations.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
ISOM MIS 215 Module 3 – Stacks and Queues. ISOM Where are we? 2 Intro to Java, Course Java lang. basics Arrays Introduction NewbieProgrammersDevelopersProfessionalsDesigners.
Data Structures. The Stack: Definition A stack is an ordered collection of items into which new items may be inserted and from which items may be deleted.
Data Structures Week 4 Our First Data Structure The story so far  We understand the notion of an abstract data type.  Saw some fundamental operations.
COP3530 Data Structures600 Stack Stack is one the most useful ADTs. Like list, it is a collection of data items. Supports “LIFO” (Last In First Out) discipline.
Data Structures (part 2). Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that last ‘rush’
CS 206 Introduction to Computer Science II 02 / 23 / 2009 Instructor: Michael Eckmann.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues (part 2)
Basic Data Structures Stacks. A collection of objects Objects can be inserted into or removed from the collection at one end (top) First-in-last-out.
CHP-3 STACKS.
CS 206 Introduction to Computer Science II 10 / 10 / 2008 Instructor: Michael Eckmann.
Stacks A stack is a linear data structure that can be accessed only at one of its ends for storing and retrieving data LIFO (Last In First Out) structure.
3/3/20161 Stacks and Queues Introduction to Data Structures Ananda Gunawardena.
CC 215 DATA STRUCTURES MORE ABOUT STACK APPLICATIONS Dr. Manal Helal - Fall 2014 Lecture 6 AASTMT Engineering and Technology College 1.
Lecture - 6(Stacks) On Data structures. Prepared by, Jesmin Akhter, Lecturer, IIT,JU Lecture Outline What is a Stack? Array implementation of stacks Operations.
 In general, Queue is line of person waiting for their turn at some service counter like ticket window at cinema hall, at bus stand or at railway station.
1 Lecture 9: Stack and Queue. What is a Stack Stack of Books 2.
BCA II Data Structure Using C
Chapter 15 Lists Objectives
Stacks and Queues.
STACKS.
Building Java Programs
Stacks and Queues.
ITEC 2620M Introduction to Data Structures
Infix to Postfix Conversion
Stack A data structure in which elements are inserted and removed only at one end (called the top). Enforces Last-In-First-Out (LIFO) Uses of Stacks Evaluating.
Topic 15 Implementing and Using Stacks
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Queue Applications Lecture 31 Tue, Apr 11, 2006.
Stacks and Queues.
Presented by : Aman Gupta PGT CS KV No.1, Narimedu, Madurai
DATA STRUCTURES IN PYTHON
Stacks A stack is an ordered set of elements, for which only the last element placed into the stack is accessible. The stack data type is also known as.
Presentation transcript:

CS 206 Introduction to Computer Science II 10 / 15 / 2008 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS Fall 2008 Today’s Topics Questions? Divide & Conquer –finish solution to the maximum contiguous subsequence problem

Recursion 1. have at least one base case that is not recursive 2. recusive case(s) must progress towards the base case 3. trust that your recursive call does what it says it will do (without having to unravel all the recursion in your head.)‏ 4. try not to do redundant work. That is, in different recursive calls, don't recalculate the same info.

Divide and Conquer The divide and conquer technique is a way of converting a problem into smaller problems that can be solved individually and then combining the answers to these subproblems in some way to solve the larger problem DIVIDE = solve smaller problems recursively, except the base case(s)‏ CONQUER = compute the solution to the overall problem by using the solutions to the smaller problems solved in the DIVIDE part.

Divide and Conquer Divide the sequence in half. Solve each half individually then combine. If we divide the sequence in half and work on the left side, independently from the right side, what do we need to consider when combining?

Divide and Conquer Divide the sequence in half. Solve each half individually then combine. If we divide the sequence in half and work on the left side, independently from the right side, there are three cases to consider: –either the maximum contiguous subsequence lives wholly within the left side –or it lives wholly within the right side –or it lives partially in the left and partially in the right if this is the case, then we are guaranteed that the sequence contains the last element of the left side and the first element of the right side. Right?

Divide and Conquer Maximum sum of a contiguous subsequence of –seq[left.. right ] Conquer part: –compute the maxLeftBorderSum –compute the maxRightBorderSum –decide which is larger maxLeft or maxRight or maxLeftBorderSum + maxRightBorderSum

Divide and Conquer Maximum sum of a contiguous subsequence of –seq[left.. right ] Base case: –if (left == right)‏ return the larger of 0 or seq[left] Let's code this thing.

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.

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.

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.

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?

Queues and Stacks Recursive method calls A new call to the method causes it's local data and other state information (where in the method does it return to, etc.) to be pushed onto the stack The method calls finish in reverse order, so when a method call ends, it's local data & state is popped off the stack

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

Queues and Stacks Let's implement a stack with an array –what will be our instance variables? Let's implement a queue with an array –what will be our instance variables?

Queues and Stacks Applications of stacks –balancing symbols. e.g. curly braces (left { ) and (right })‏ compilers need to figure out if there are the same number of lefts as there are rights. a stack is handy for this how?

Queues and Stacks Applications of stacks –balancing symbols. e.g. curly braces (left { ) and (right })‏ compilers need to figure out if there are the same number of lefts as there are rights. a stack is handy for this –when see a left curly, push it on the stack –when see a right curly try to pop the stack --- there should be a left curly to pop, otherwise it's an error –when get to the end of the source code, if the stack is empty (and we haven't run into the error above) then the curly braces are balanced.

Queues and Stacks Applications of stacks –postfix expressions (reverse Polish notation)‏ infix (what we're used to) is like: 1.07* *8.75 postfix is like: 1.07, 8.75, *, 14.05, +, 1.07, 3.85, *, + infix result requires us to do the multiplies first and then store the intermediate values and then to the 2 adds. postfix operates in the following way: –when see a number, push it –when see an operator, perform it to the two top numbers on the stack and push the result final result in our example is:

Queues and Stacks Applications of stacks –converting from infix (with only +, *, (, ) ) to postfix expressions infix example (assume normal precedence): a + b*c + ( d*e + f ) * g postfix for our example could be: a b c * + d e * f + g * + –let's look at the algorithm to do this using a stack

Queues and Stacks converting infix to postfix –when an operand (a, b, c, etc.) is read write it to the output –if we see a ) then we pop the stack and write to output until a left paren is on top of the stack --- at which point we pop the left paren but do not output it. –when a + or * is read, examine the top of the stack – if top is of lower precedence than the one just read, then push the operator –when a + or * is read – if top is of the stack does not have lower precedence than the one just read, then pop the operator and write to output –when a ( is read push it –Let's try this algorithm with our example: infix: a + b*c + ( d*e + f ) * g postfix (expected output): a b c * + d e * f + g * +