TK1924 Program Design & Problem Solving Session 2011/2012

Slides:



Advertisements
Similar presentations
Chapter 25 Lists, Stacks, Queues, and Priority Queues
Advertisements

TK1924 Program Design & Problem Solving Session 2011/2012
CS Data Structures I Chapter 6 Stacks I 2 Topics ADT Stack Stack Operations Using ADT Stack Line editor Bracket checking Special-Palindromes Implementation.
AP STUDY SESSION 2.
1
Copyright © 2003 Pearson Education, Inc. Slide 1.
Copyright © 2003 Pearson Education, Inc. Slide 1 Computer Systems Organization & Architecture Chapters 8-12 John D. Carpinelli.
Copyright © 2011, Elsevier Inc. All rights reserved. Chapter 6 Author: Julia Richards and R. Scott Hawley.
Author: Julia Richards and R. Scott Hawley
1 Hyades Command Routing Message flow and data translation.
Process a Customer Chapter 2. Process a Customer 2-2 Objectives Understand what defines a Customer Learn how to check for an existing Customer Learn how.
Custom Statutory Programs Chapter 3. Customary Statutory Programs and Titles 3-2 Objectives Add Local Statutory Programs Create Customer Application For.
Custom Services and Training Provider Details Chapter 4.
Programming Language Concepts
1 Click here to End Presentation Software: Installation and Updates Internet Download CD release NACIS Updates.
Break Time Remaining 10:00.
Turing Machines.
PP Test Review Sections 6-1 to 6-6
Chapter 17 Linked Lists.
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
Chapter 10: Applications of Arrays and the class vector
Chapter 24 Lists, Stacks, and Queues
11 Data Structures Foundations of Computer Science ã Cengage Learning.
Abstract Data Types and Algorithms
Data Structures Using C++
Chapter 1 Object Oriented Programming 1. OOP revolves around the concept of an objects. Objects are created using the class definition. Programming techniques.
EIS Bridge Tool and Staging Tables September 1, 2009 Instructor: Way Poteat Slide: 1.
Exarte Bezoek aan de Mediacampus Bachelor in de grafische en digitale media April 2014.
Sample Service Screenshots Enterprise Cloud Service 11.3.
Copyright © 2012, Elsevier Inc. All rights Reserved. 1 Chapter 7 Modeling Structure with Blocks.
1 RA III - Regional Training Seminar on CLIMAT&CLIMAT TEMP Reporting Buenos Aires, Argentina, 25 – 27 October 2006 Status of observing programmes in RA.
Basel-ICU-Journal Challenge18/20/ Basel-ICU-Journal Challenge8/20/2014.
1..
Adding Up In Chunks.
Copyright © 2013 by John Wiley & Sons. All rights reserved. HOW TO CREATE LINKED LISTS FROM SCRATCH CHAPTER Slides by Rick Giles 16 Only Linked List Part.
1 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt Synthetic.
1 hi at no doifpi me be go we of at be do go hi if me no of pi we Inorder Traversal Inorder traversal. n Visit the left subtree. n Visit the node. n Visit.
Types of selection structures
Essential Cell Biology
Exponents and Radicals
PSSA Preparation.
Essential Cell Biology
Immunobiology: The Immune System in Health & Disease Sixth Edition
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13 Pointers and Linked Lists.
Physics for Scientists & Engineers, 3rd Edition
Energy Generation in Mitochondria and Chlorplasts
1 Decidability continued…. 2 Theorem: For a recursively enumerable language it is undecidable to determine whether is finite Proof: We will reduce the.
Chapter 6 Stacks. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 6-2 Chapter Objectives Examine stack processing Define a stack abstract.
Chapter 18: Stacks and Queues
Chapter 18: Stacks and Queues
Chapter 17: Stacks and Queues
Data Structures Using C++ 2E Chapter 7 Stacks. Data Structures Using C++ 2E2 Objectives Learn about stacks Examine various stack operations Learn how.
Chapter 7 Stacks Dr. Youssef Harrath
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
Chapter 4 Stacks Stacks A stack is a linear data structure that can be accessed only at one of its ends for storing and retrieving. Its called.
DATA STRUCTURE & ALGORITHMS CHAPTER 3: STACKS. 2 Objectives In this chapter, you will: Learn about stacks Examine various stack operations Discover stack.
Chapter 18: Stacks and Queues
Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 19: Stacks and Queues.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues.
1 CS 132 Spring 2008 Chapter 7 Stacks Read p Problems 1-7.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 19: Stacks and Queues (part 2)
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues (part 2)
Data Structures Using C++1 Chapter 7 Stacks. Data Structures Using C++2 Chapter Objectives Learn about stacks Examine various stack operations Learn how.
Data Structures Using Java1 Chapter 6 Stacks. Data Structures Using Java2 Chapter Objectives Learn about stacks Examine various stack operations Learn.
Chapter 17: Stacks and Queues. Objectives In this chapter, you will: – Learn about stacks – Examine various stack operations – Learn how to implement.
Data Structures Using C++ 2E
Stacks Data structure Elements added, removed from one end only
Presentation transcript:

TK1924 Program Design & Problem Solving Session 2011/2012 L5: Stacks

Objectives In this chapter, you will: Learn about stacks Examine various stack operations Learn how to implement a stack as an array Discover stack applications Learn how to use a stack to remove recursion

Stacks Stack: list of homogenous elements Operations: Addition and deletion occur only at one end, called the top of the stack Example: in a cafeteria, the second tray can be removed only if first tray has been removed Last in first out (LIFO) data structure Operations: Push: to add an element onto the stack Pop: to remove an element from the stack

Stacks (cont’d.)

Stacks (cont’d.)

Stack Operations In the abstract class stackADT: initializeStack isEmptyStack isFullStack push top pop

Implementation of Stacks as Arrays First element can go in first array position, the second in the second position, etc. The top of the stack is the index of the last element added to the stack Stack elements are stored in an array Stack element is accessed only through top To keep track of the top position, use a variable called stackTop

Implementation of Stacks as Arrays (cont'd.) Because stack is homogeneous You can use an array to implement a stack Can dynamically allocate array Enables user to specify size of the array The class stackType implements the functions of the abstract class stackADT

UML Class Diagram of class stackType

Implementation of Stacks as Arrays (cont'd.) C++ arrays begin with the index 0 Must distinguish between: The value of stackTop The array position indicated by stackTop If stackTop is 0, the stack is empty If stackTop is nonzero, the stack is not empty The top element is given by stackTop - 1

Implementation of Stacks as Arrays (cont'd.)

Initialize Stack

Empty Stack If stackTop is 0, the stack is empty

Full Stack The stack is full if stackTop is equal to maxStackSize

Push Store the newItem in the array component indicated by stackTop Increment stackTop Must avoid an overflow

Push (cont'd.)

Return the Top Element

Pop Simply decrement stackTop by 1 Must check for underflow condition

Pop (cont’d.)

Pop (cont’d.)

Copy Stack

Constructor

Destructor

Stack Header File myStack.h Place definitions of class and functions (stack operations) together in a file

Programming Example: Highest GPA Input: program reads an input file with each student’s GPA and name 3.5 Bill 3.6 John 2.7 Lisa 3.9 Kathy 3.4 Jason 3.9 David 3.4 Jack Output: the highest GPA and all the names associated with the highest GPA

Programming Example: Problem Analysis and Algorithm Design Read the first GPA and name of the student This is the highest GPA so far Read the second GPA and student name Compare this GPA with highest GPA so far New GPA is greater than highest GPA so far Update highest GPA, initialize stack, add to stack New GPA is equal to the highest GPA so far Add name to stack New GPA is smaller than the highest GPA Discard

Programming Example: Problem Analysis and Algorithm Design (cont’d.) 3.5 Bill 3.6 John 2.7 Lisa 3.9 Kathy 3.4 Jason 3.9 David 3.4 Jack 3.9 [0] [1] [2] [3] : [98] [99] Kathy David highestGPA 1 100 list stackTop maxStackSize

Application of Stacks: Postfix Expressions Calculator Infix notation: usual notation for writing arithmetic expressions The operator is written between the operands Example: a + b The operators have precedence Parentheses can be used to override precedence

Application of Stacks: Postfix Expressions Calculator (cont'd.) Prefix (Polish) notation: the operators are written before the operands Introduced by the Polish mathematician Jan Lukasiewicz Early 1920s The parentheses can be omitted Example: + a b

Application of Stacks: Postfix Expressions Calculator (cont'd.) Reverse Polish notation: the operators follow the operands (postfix operators) Proposed by the Australian philosopher and early computer scientist Charles L. Hamblin Late 1950's Advantage: the operators appear in the order required for computation Example: a + b * c In a postfix expression: a b c * +

Application of Stacks: Postfix Expressions Calculator (cont'd.)

Application of Stacks: Postfix Expressions Calculator (cont'd.) Postfix notation has important applications in computer science Many compilers first translate arithmetic expressions into postfix notation and then translate this expression into machine code Evaluation algorithm: Scan expression from left to right When an operator is found, back up to get the operands, perform the operation, and continue

Application of Stacks: Postfix Expressions Calculator (cont'd.) Example: 6 3 + 2 * =

Application of Stacks: Postfix Expressions Calculator (cont'd.) Symbols can be numbers or anything else: +, -, *, and / are operators Pop stack twice and evaluate expression If stack has less than two elements  error If symbol is =, the expression ends Pop and print answer from stack If stack has more than one element  error If symbol is anything else Expression contains an illegal operator

Application of Stacks: Postfix Expressions Calculator (cont'd.) Examples: 7 6 + 3 ; 6 - = ; is an illegal operator 14 + 2 3 * = Does not have enough operands for + 14 2 3 + = Error: stack will have two elements when we encounter equal (=) sign

Application of Stacks: Postfix Expressions Calculator (cont'd.) We assume that the postfix expressions are in the following form: #6 #3 + #2 * = If symbol scanned is #, next input is a number If the symbol scanned is not #, then it is: An operator (may be illegal) or An equal sign (end of expression) We assume expressions contain only +, -, *, and / operators

Main Algorithm Pseudocode: We will write four functions: evaluateExpression, evaluateOpr, discardExp, and printResult

Function evaluateExpression

Function evaluateOpr

Function evaluateOpr (cont’d.)

Function discardExp This function is called whenever an error is discovered in the expression

Function printResult If the postfix expression contains no errors, the function printResult prints the result Otherwise, it outputs an appropriate message The result of the expression is in the stack and the output is sent to a file

Function printResult (cont’d.)

Nonrecursive Algorithm to Print a Linked List Backward To print the list backward, first we need to get to the last node of the list Problem: how do we get back to previous node? Links go in only one direction Solution: save a pointer to each of the nodes with info 5, 10, and 15 Use a stack (LIFO)

Nonrecursive Algorithm to Print a Linked List Backward

Nonrecursive Algorithm to Print a Linked List Backward Let us now execute the following statements: Output: 20 15 10 5

Summary Stack: items are added/deleted from one end Last In First Out (LIFO) data structure Operations: push, pop, initialize, destroy, check for empty/full stack Can be implemented as array or linked list Middle elements should not be accessed Postfix notation: operators are written after the operands (no parentheses needed)