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.

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

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.
Prefix, Postfix, Infix Notation
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)
COSC 2006 Chapter 7 Stacks III
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.
Arithmetic Expressions
Topic 15 Implementing and Using Stacks
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.
Stacks & Queues Infix Calculator CSC 172 SPRING 2004 LECTURE 13.
Infix, Postfix, Prefix.
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.
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.
30-Jun-15 Stacks. What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything.
Topic 15 Implementing and Using Stacks
Class 4: Queues. cis 335 Fall 2001 Barry Cohen What is a queue? n A stack is an ordered sequence of items. n As in lists and stacks, each node contains.
Main Index Contents 11 Main Index Contents Stacks Further Stack Examples Further Stack Examples Pushing/Popping a Stack Pushing/Popping a Stack Class StackClass.
Class 4: Stacks. cis 335 Fall 2001 Barry Cohen What is a stack? n A stack is an ordered sequence of items, of which only the last (‘top’) item can be.
CS 206 Introduction to Computer Science II 10 / 28 / 2009 Instructor: Michael Eckmann.
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)
Evaluation of Expressions Instructor : Prof. Jyh-Shing Roger Jang Designer : Shao-Huan Wang The ideas are reference to the textbook “Fundamentals of Data.
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.
CSC 205 Programming II Postfix Expressions. Recap: Stack Stack features Orderly linear structure Access from one side only – top item Stack operations.
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.
 STACK STACK  BASIC STACK OPERATIONS BASIC STACK OPERATIONS  PUSH ALGORITHM PUSH ALGORITHM  POP ALGORITHM POP ALGORITHM  EVALUATING A POSTFIX EXPRESSION.
EC-211 DATA STRUCTURES LECTURE 8. STACK APPLICATIONS Infix, Prefix, and Postfix Expressions Example – Infix: A+B – Prefix: +AB – Postfix: AB+
CHP-3 STACKS.
Reverse Polish Notation Written by J.J. Shepherd.
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(),
CSC 172 DATA STRUCTURES. A TALE OF TWO STRUCTURES.
BCA II Data Structure Using C
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
CSC 172 DATA STRUCTURES.
Stack application: postponing data usage
STACK CHAPTER 03 Developed By :- Misha Ann Alexander Data Structures.
Stacks – Calculator Application
Visit for more Learning Resources
Stacks – Calculator Application
Stacks – Calculator Application
PART II STACK APPLICATIONS
COMPUTER 2430 Object Oriented Programming and Data Structures I
Infix to Postfix Conversion
COMPUTER 2430 Object Oriented Programming and Data Structures I
Stacks – Calculator Application
Stack Applications Lecture 29 Thu, Apr 5, /23/2019
Queue Applications Lecture 31 Mon, Apr 9, 2007.
Infix to Postfix Conversion
Topic 15 Implementing and Using Stacks
(Part 2) Infix, Prefix & Postfix
Queue Applications Lecture 31 Tue, Apr 11, 2006.
Stack Applications Lecture 29 Thu, Apr 1, /29/2019 Stacks.
17CS1102 DATA STRUCTURES © 2016 KL University – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS.
Stacks.
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.
© 2016 Pearson Education, Ltd. All rights reserved.
Presentation transcript:

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

Stack Applications: Reversing a Word RAIL  LIAR

Empty Stack Start from an empty stack Insert the word RAIL into the stack

R A I L R

A R

I A R

L I A R

The Word is now in the stack. Now empty the Stack. L I A R

L L I A R

L I I A R

L I A A R

L I A R R

Stack Operations push : push a new item on the top of the stack pop : if the stack is not empty, remove the top item of the stack; not defined if the stack is empty top : if the stack is not empty, read the value of the top item of the stack; not defined if the stack is empty

Practice What’s remained in the stack after the following operations, assuming that at the beginning the stack is empty? push(3); push(5); pop(); top(); push(9);

Evaluate Postfix Expressions Operator comes after the operands PostfixInfix *(5 + 4) * *(5 + 4) * (9 - 6)

Evaluate Postfix: Keep Intermediate Results E.g. the result of (5 + 4) while we calculate (9 - 6) stacks do this! Assumptions: –binary operators only –we can split the expression string into tokens (pieces)

Evaluate Postfix: the Algorithm while there are more tokens in the input string: –if next token is a number, push it onto the stack –if next token is an operand pop two elements off the stack perform the operation stack the result pop final element as answer

Evaluate * 5 is a number, place it on the stack 5

Evaluate * 4 is a number, place it on the stack 4 5

Evaluate * + is an operand Pop two numbers Apply Operand = 9

Evaluate * Put the result back onto the stack = 9

Evaluate * 9 is a number, place it on the stack 9 9

Evaluate * 6 is a number, place it on the stack 6 9 9

Evaluate * - is an operand Pop two numbers Apply Operand = 3

Evaluate * - is an operand Put the result back onto the stack = 3 3

Evaluate * 9 3 * is an operand  Pop two numbers  Apply Operand 3 9 * = 27

Evaluate * 27 * is an operand  Pop two numbers  Apply Operand 3 9 * = 27

Evaluate * No more tokens in the string Pop the final answer * = (5 + 4) * (9 - 6) = 27