Revised based on textbook author’s notes.

Slides:



Advertisements
Similar presentations
Stacks & Their Applications COP Stacks  A stack is a data structure that stores information arranged like a stack.  We have seen stacks before.
Advertisements

Stacks Chapter 11.
Prefix, Postfix, Infix Notation
Computer Science 112 Fundamentals of Programming II Applications of Stacks.
COMPSCI 105 S Principles of Computer Science 13 Stacks.
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.
Lecture 12 – ADTs and Stacks.  Modularity  Divide the program into smaller parts  Advantages  Keeps the complexity managable  Isolates errors (parts.
Stacks Chapter 5. Chapter Objectives  To learn about the stack data type and how to use its four methods: push, pop, peek, and empty  To understand.
Topic 15 Implementing and Using Stacks
Stacks Chapter 5. Chapter 5: Stacks2 Chapter Objectives To learn about the stack data type and how to use its four methods: push, pop, peek, and empty.
Stacks.
Stacks Chapter 5. Chapter 5: Stacks2 Chapter Objectives To learn about the stack data type and how to use its four methods: push, pop, peek, and empty.
Infix, Postfix, Prefix.
Fall 2007CS 2251 Stacks Chapter 5. Fall 2007CS 2252 Chapter Objectives To learn about the stack data type and how to use its four methods: push, pop,
Fall 2007CS 2251 Stacks Chapter 5. Fall 2007CS 2252 Chapter Objectives To learn about the stack data type and how to use its four methods: push, pop,
Implementing and Using Stacks
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.
More About Stacks: Stack Applications Dan Nguyen CS 146, Spring 2004 Professor Sin-Min Lee.
Stack  A stack is a linear data structure or abstract data type for collection of items, with the restriction that items can be added one at a time and.
Exam 1 –Monday June 25 th –open Book / Open Notes –No Electronic Devices (calculators, laptops, etc) –Room Number: W –Time: 5:30pm to 8:00pm.
Data Structures Lecture : Stacks (Infix, Postfix and Prefix Expressions) Azhar Maqsood NUST Institute of Information Technology (NIIT)
Fundamentals of Python: From First Programs Through Data Structures Chapter 14 Linear Collections: Stacks.
Comp 245 Data Structures Stacks. What is a Stack? A LIFO (last in, first out) structure Access (storage or retrieval) may only take place at the TOP NO.
1 Stacks – Chapter 3 A stack is a data structure in which all insertions and deletions of entries are made at one end, called the top of the stack. Alternatively,
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.
Computer Science Department Data Structure & Algorithms Problem Solving with Stack.
SAK 3117 Data Structures Chapter 3: STACKS. Objective To introduce: Stack concepts Stack operations Stack applications CONTENT 3.1 Introduction 3.2 Stack.
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.
CSE 373: Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks; Eclipse.
CHAPTER 3 STACK CSEB324 DATA STRUCTURES & ALGORITHM.
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.
Data Structures & Algorithms
COMPSCI 105 SS 2015 Principles of Computer Science 09 ADT & Stacks.
Stacks An Abstract Data Type. Restricted Access Unlike arrays, stacks only allow the top most item to be accessed at any time The interface of a stack.
Copyright © Curt Hill Stacks An Useful Abstract Data Type.
CHP-3 STACKS.
Prefix, Postfix, Infix Notation. Infix Notation  To add A, B, we write A+B  To multiply A, B, we write A*B  The operators ('+' and '*') go in between.
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.
CC 215 DATA STRUCTURES MORE ABOUT STACK APPLICATIONS Dr. Manal Helal - Fall 2014 Lecture 6 AASTMT Engineering and Technology College 1.
Applications of Stack Maitrayee Mukerji. Stacks Last In First Out (LIFO List) ◦ FILO? Insertions and Deletions from the same end called the Top Push(),
Chapter 3 Lists, Stacks, Queues. Abstract Data Types A set of items – Just items, not data types, nothing related to programming code A set of operations.
Stacks Stack Abstract Data Type (ADT) Stack ADT Interface
Stacks Access is allowed only at one point of the structure, normally termed the top of the stack access to the most recently added item only Operations.
CS Data Structures Chapter 6 Stacks Mehmet H Gunes
Stacks Chapter 5.
CSE 373: Data Structures and Algorithms
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Stack application: postponing data usage
Algorithms and Data Structures
Visit for more Learning Resources
Building Java Programs Chapter 14
COMPUTER 2430 Object Oriented Programming and Data Structures I
Stacks and Queues.
Stacks Chapter 5 Adapted from Pearson Education, Inc.
More About Stacks: Stack Applications
COMPUTER 2430 Object Oriented Programming and Data Structures I
Cs212: Data Structures Computer Science Department Lecture 6: Stacks.
Stacks.
Topic 15 Implementing and Using Stacks
Stacks Chapter 5.
Stack.
More About Stacks: Stack Applications
Data Structures and Algorithms 2/2561
Chapter 7 (continued) © 2011 Pearson Addison-Wesley. All rights reserved.
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:

Revised based on textbook author’s notes. Stacks Revised based on textbook author’s notes.

Stacks A restricted access container that stores a linear collection. Very common for solving problems in computer science. Provides a last-in first-out (LIFO) protocol.

The Stack New items are added and existing items are removed from the top of the stack.

Why stacks are useful? Many computer science problems (and real life problems) are solved using stacks. We’ll touch a few here. Example: determine if a string is a palindrome Here is a list of palindrome http://www.palindromelist.net/ A Toyota’s a Toyota. Civic Go dog. Tell a ballet.

We can solve the palindrome problem in recursion

We can also solve the problem using stacks Given a string s, put the sting into a stack, then take the one on stack out in order (LIFO). If the two are the same, it is a palindrome! civic Original string In-stack Output godog hello olleh

The Stack ADT A stack stores a linear collection of items with access limited to a last-in first-out order. Adding and removing items is restricted to the top of the stack. Stack() is_empty() len() pop() peek() push( item )

Stack Example reverse.py # Extracts a collection of integer values from the user # and prints them in reverse order. PROMPT = "Enter an int value (<0 to end): " my_stack = Stack() # Extract the values and push them onto a stack. value = int(input( PROMPT )) while value >= 0 : my_stack.push( value ) # Pop the values from the stack and print each. while not my_stack.is_empty() : value = my_stack.pop() print( value )

Stack Implementation Several common ways to implement a stack: Python list easiest to implement Linked list better choice when a large number of push and pop operations are performed.

Linked list implementation

Stack Applications Many applications encountered in computer science requires the use of a stack. Balanced delimiters Postfix expressions

Balanced Delimiters Many applications use delimiters to group strings of text or simple data into subparts. mathematical expressions programming languages HTML markup

Source Code Example Consider the following C++ source code: int sumList( int theList[], int size ) { int sum = 0; int i = 0; while( i < size ) { sum += theList[ i ]; i += 1; } return sum;

Source Code Example The delimiters must be paired and balanced. We can design and implement an algorithm to: scan a C++ source file, and determine if the delimiters are properly paired.

Valid C++ Source? from lliststack import Stack def isValidSource( srcfile ): s = Stack() for line in srcfile : for token in line : if token in "{[(" : s.push( token ) elif token in "}])" : if s.isEmpty() : return False else : left = s.pop() if (token == "}" and left != "{") or \ (token == "]" and left != "[") or \ (token == ")" and left != "(") : return s.isEmpty()

Mathematical Expressions We work with mathematical expressions on a regular basis. Easy to determine the order of evaluation. Easy to calculate. But the task is more difficult in computer programs. A program cannot visualize the expression to determine the order of evaluation. Must examine one token at a time.

Types of Expressions Three different notations can be used: infix: A + B * C prefix: + A * B C postfix: A B C * +

Infix to Postfix Infix expressions can be easily converted by hand to postfix notation. 1. Fully parenthesize the expression. 2. For each set of (), move operator to the end of the closing parenthesis. A * B + C / D ((A * B) + (C / D)) ((A B *) (C D /) +)

Infix to Postfix (cont) The expression at the end of step 2: 3. Remove all of the parentheses. Which results in the postfix version. ((A B *) (C D /) +) A B * C D / +

Evaluating Postfix Expressions We can evaluate a valid postfix expression using a stack structure. For each token: If the current token is an operand, push its value onto the stack. If the current token is an operator: pop the top two operands off the stack. perform the operation (top value is RHS operand). push the result of the operation back on the stack. The final result will be the last value on the stack.

Postfix Evaluation Examples To illustrate the use of the algorithm, assume the existence of an empty stack, and the following variable assignments Evaluate the valid expression: A = 8 C = 3 B = 2 D = 4 A B C + * D /

Postfix Example #1 Token Alg Step Stack Description ABC+*D/ 1 8 push value of A 8 2 push value of B 8 2 3 push value of C 2(a) pop top two values: y = 3, x = 2 2(b) compute z = x + y or z = 2 + 3 2(c) 8 5 push result (5) of the addition pop top two values: y = 5, x = 8 compute z = x * y or z = 8 * 5 40 push result (40) of the multiplication 40 4 push value of D pop top two values: y = 4, x = 40 compute z = x / y or z = 40 / 4 10 push result (10) of the division

Postfix Example #2 What happens if the expression is invalid? A B * C D + Token Alg Step Stack Description AB*CD+ 1 8 push value of A 8 2 push value of B 2(a) pop top two values: y = 2, x = 8 2(b) compute z = x * y or z = 8 * 2 2(c) 16 push result (16) of the multiplication 16 3 push value of C 16 3 4 push value of D pop top two values: y = 4, x = 3 compute z = x + y or z = 3 + 4 16 7 push result (7) of the addition Error xxxxxx Too many values left on the stack.

Postfix Example #3 What happens if there are too many operators for the given number of operands? A B * + C / Token Alg Step Stack Description AB*+C/ 1 8 push value of A 8 2 push value of B 2(a) pop top two values: y = 2, x = 8 2(b) compute z = x * y or z = 8 * 2 2(c) 16 push result (16) of the multiplication pop top two values: y = 16, x = ? Error xxxxxx Only one value on stack, two needed.