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.

Slides:



Advertisements
Similar presentations
INFIX, PREFIX, & POSTFIX EXPRESSIONS. Infix Notation We usually write algebraic expressions like this: a + b This is called infix notation, because the.
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.
Stacks & Their Applications COP Stacks  A stack is a data structure that stores information arranged like a stack.  We have seen stacks before.
Stacks - 3 Nour El-Kadri CSI Evaluating arithmetic expressions Stack-based algorithms are used for syntactical analysis (parsing). For example.
Arithmetic Expressions Infix form –operand operator operand 2+3 or a+b –Need precedence rules –May use parentheses 4*(3+5) or a*(b+c)
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 5 Sept 15 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
Lecture 12 – ADTs and Stacks.  Modularity  Divide the program into smaller parts  Advantages  Keeps the complexity managable  Isolates errors (parts.
Topic 15 Implementing and Using Stacks
CS 206 Introduction to Computer Science II 03 / 06 / 2009 Instructor: Michael Eckmann.
Stacks & Queues Infix Calculator CSC 172 SPRING 2002 LECTURE 5.
Infix to postfix conversion Process the tokens from a vector infixVect of tokens (strings) of an infix expression one by one When the token is an operand.
CS 206 Introduction to Computer Science II 10 / 17 / 2008 Instructor: Michael Eckmann.
Lecture 6 Feb 12 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
Reverse Polish Expressions Some general observations about what they are and how they relate to infix expressions. These 9 slides provide details about.
CS 206 Introduction to Computer Science II 10 / 15 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 03 / 16 / 2009 Instructor: Michael Eckmann.
1 CSCD 326 Data Structures I Infix Expressions. 2 Infix Expressions Binary operators appear between operands: W - X / Y - Z Order of evaluation is determined.
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 206 Introduction to Computer Science II 10 / 28 / 2009 Instructor: Michael Eckmann.
The Stack and Queue Types Lecture 10 Hartmut Kaiser
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.
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.
Stack Applications.
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.
Linear Data Structures LIFO – Polish notation Context Saving.
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.
CE 221 Data Structures and Algorithms Chapter 3: Lists, Stacks, and Queues - II Text: Read Weiss, §3.6 1Izmir University of Economics.
Copyright © Curt Hill Stacks An Useful Abstract Data Type.
CHP-3 STACKS.
Stacks & Queues CSC 172 SPRING 2002 LECTURE 4 Agenda  Stack  Definition  Implementation  Analysis  Queue  Definition  Implementation  Analysis.
1 Data Structures and Algorithms Stack. 2 The Stack ADT Introduction to the Stack data structure Designing a Stack class using dynamic arrays Linked Stacks.
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(),
CE 221 Data Structures and Algorithms Chapter 3: Lists, Stacks, and Queues - II Text: Read Weiss, §3.6 1Izmir University of Economics.
1 Data Structures and Algorithms Stack. 2 The Stack ADT Introduction to the Stack data structure Designing a Stack class using dynamic arrays Linked Stacks.
BCA II Data Structure Using C
Review Use of Stack Introduction Stack in our life Stack Operations
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.
Revised based on textbook author’s notes.
COMPSCI 107 Computer Science Fundamentals
Infix to postfix conversion
CS 201 Data Structures and Algorithms
MEMORY REPRESENTATION OF STACKS
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Stacks Chapter 4.
Stack application: postponing data usage
PART II STACK APPLICATIONS
Stacks Chapter 5 Adapted from Pearson Education, Inc.
Stacks, Queues, and Deques
More About Stacks: Stack Applications
Queue Applications Lecture 31 Mon, Apr 9, 2007.
Infix to Postfix Conversion
Topic 15 Implementing and Using Stacks
Stack.
Queue Applications Lecture 31 Tue, Apr 11, 2006.
More About Stacks: Stack Applications
17CS1102 DATA STRUCTURES © 2016 KL University – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS.
Chapter 7 (continued) © 2011 Pearson Addison-Wesley. All rights reserved.
LINEAR DATA STRUCTURES
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:

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

Stacks as ADTs Data – A collection of elements of the same type Operations – clear() – isEmpty() – push(el) – pop() – topEl()

Stacks as ADT’s (cont’d) Implementation – With a doubly linked list, code in book Time Complexity of push and pop operations?

Stacks as ADTs (cont’d)

Applications of Stacks parenthesis matching Performing “postfix” calculations Performing “infix” to “postfix” conversions Function calls (and specially recursion) rely upon stacks

Symbol Balancing A useful tool for checking your code is to see if the (), {}, and [] symbols balance properly For example, the sequence …[…(…)…]… is legal but the sequence …[…(…]…)… is not The presence of one misplaced symbol can result in hundreds of worthless compiler diagnostic errors

Symbol Balancing (cont’d) The algorithm is simple and efficient: – Create an empty stack – Read characters until end of file (EOF) – If the character is an opening symbol (, [, or {, push it onto the stack – If it is a closing symbol ), ], or }, then if the stack is empty report an error. Otherwise pop the stack – If the symbol popped is not the corresponding opening symbol, report an error – At EOF, if the stack is not empty report an error Example: Apply this algorithm to the statement: (([])), (([)]) s=t[5]+u/(v*(w+y));

When will an error reported for Symbol Matching

Postfix Evaluation What is “postfix”? – It is the most efficient method for representing arithmetic expressions – With “infix” (the method you are used to) a + b operators between operands: – With “postfix” : a b + operators after operands – There is never any need to use ()’s with postfix notation. There is never any ambiguity

Postfix Example This example is in infix: a + b * c + (d * e + f) * g In postfix this is: a b c * + d e * f + g * +

Postfix Evaluation Algorithm The algorithm is simple and efficient O(n): – Read in input – If input is an operand, push on stack – If input is an operator, pop top two operands off stack, perform operation, and place result on stack Example: 1+2*3 (infix) 123*+ (postfix) a b c * +

Infix to Postfix Conversion Only need a stack to write an algorithm to convert an infix expression to a postfix expression – It can handle the presence of ()’s – It is efficient: O(n)

Infix -> Postfix Conversion Example This example is in infix: a + b * c + (d * e + f) * g In postfix this is: a b c * + d e * f + g * +

Conversion Algorithm The algorithm is reasonably simple: – Read infix expression as input – If input is operand, output the operand – If input is an operator +, -, *, /, then pop and output all operators of >= precedence. Push operator – If input is (, then push – If input is ), then pop and output all operators until see a ( on the stack. Pop the ( without output – If no more input then pop and output all operators on stack

Function Calls In almost all programming languages, calling a function involves the use of a stack – When there is a function call, all of the important information (e.g., values of variables, etc…) is stored on a stack, and control is transferred to the new function – This is especially important during recursion

Example