STACK IMPLEMENTATION Adam M.B..

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 Chapter 11.
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)
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.
Department of Technical Education Andhra Pradesh
CS 206 Introduction to Computer Science II 03 / 06 / 2009 Instructor: Michael Eckmann.
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.
Reverse Polish Notation (RPN) & Stacks CSC 1401: Introduction to Programming with Java Week 14 – Lecture 2 Wanda M. Kunkle.
CS 206 Introduction to Computer Science II 10 / 17 / 2008 Instructor: Michael Eckmann.
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 03 / 16 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 28 / 2009 Instructor: Michael Eckmann.
Evaluation of Expressions
The Stack and Queue Types Lecture 10 Hartmut Kaiser
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.
Data Structures Lecture : Stacks (Infix, Postfix and Prefix Expressions) Azhar Maqsood NUST Institute of Information Technology (NIIT)
Stack Data Structure By : Imam M Shofi. What is stack? A stack is a limited version of an array. A stack is a limited version of an array. New elements,
Data Structures: CSCI 362 – Stack Implementation Data Structures: CSCI 362 – Stack Implementation lecture notes adapted from Data Structures with C++ using.
Stack Applications.
1 Stacks Chapter 4 2 Introduction Consider a program to model a switching yard –Has main line and siding –Cars may be shunted, removed at any time.
CSC 205 Programming II Postfix Expressions. Recap: Stack Stack features Orderly linear structure Access from one side only – top item Stack operations.
© University of Auckland Trees CS 220 Data Structures & Algorithms Dr. Ian Watson.
Computer Science Department Data Structure & Algorithms Problem Solving with Stack.
Due: 2007/11/12. Problem 1 Rewrite function Push and Pop (Program 3.10 and 3.12) using an additional variable lastOp as discussed on Page 146. The queue.
Linear Data Structures LIFO – Polish notation Context Saving.
EC-211 DATA STRUCTURES LECTURE 8. STACK APPLICATIONS Infix, Prefix, and Postfix Expressions Example – Infix: A+B – Prefix: +AB – Postfix: AB+
Prefix, Postfix and Infix. Infix notation  A-B/(C+D)  evaluate C+D (call the result X),  then B/X (call the result Y),  and finally A-Y.  The order.
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.
CPU performs the bulk of data processing operations CPU performs the bulk of data processing operations The CPU is made up of three parts. They are Control.
DATA STRUCTURES Application of Stack – Infix to Postfix conversion a Joshua Presentation.
Applications of Stack Maitrayee Mukerji. Stacks Last In First Out (LIFO List) ◦ FILO? Insertions and Deletions from the same end called the Top Push(),
Lecture - 6(Stacks) On Data structures. Prepared by, Jesmin Akhter, Lecturer, IIT,JU Lecture Outline What is a Stack? Array implementation of stacks Operations.
Algorithms and Programming Sorting
Review Use of Stack Introduction Stack in our life Stack Operations
Queue Adam M.B..
STACK Adam M.B..
Circular Linked List Adam M.B..
Double Linked List Adam M.B..
Linked List (2) Adam M.B..
Infix to postfix conversion
Data Structures and Algorithms
Heap Sort Adam M.B..
Stack as an ADT.
Program to search an element of array using linear search.
Data Structures and Algorithms
Copyright ©2012 by Pearson Education, Inc. All rights reserved
STACKS.
Stacks Chapter 4.
Stack application: postponing data usage
Algorithms and Data Structures
STACK CHAPTER 03 Developed By :- Misha Ann Alexander Data Structures.
Visit for more Learning Resources
PART II STACK APPLICATIONS
Stacks Chapter 5 Adapted from Pearson Education, Inc.
Stacks, Queues, and Deques
Lecture No.07 Data Structures Dr. Sohail Aslam
UNIT-I Topics to be covere d 1.Introduction to data structures.
Chapter 8 Central Processing Unit
Queue Applications Lecture 31 Mon, Apr 9, 2007.
Infix to Postfix Conversion
(Part 2) Infix, Prefix & Postfix
Queue Applications Lecture 31 Tue, Apr 11, 2006.
17CS1102 DATA STRUCTURES © 2016 KL University – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS.
Presented by : Aman Gupta PGT CS KV No.1, Narimedu, Madurai
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:

STACK IMPLEMENTATION Adam M.B.

NUMERIC AND POLISH NOTATION  NUMERIC AND POLISH NOTATION

Numeric Notation Infix Operator between operands Prefix Operator before operands Postfix Operator after operands

Polish Notation Known as prefix notation (Jan Lukasiewicz). Example: INFIX PREFIX A+B +AB A+B-C -+ABC (A+B)*(C-D) *+AB-CD

Postfix Notation (Suffix) Known as Reverse Polish Notation (RPN). Example: INFIX POSTFIX A+B AB+ A+B-C AB+C- (A+B)*(C-D) AB+CD-*

 INFIX TO POSTFIX

Processes Supposed Algorithm Q = Infix notation P = Postfix Notation 1 stack for temporary variable. Algorithm Push “(“ into stack and add “)” to sentinel of Q.

Processes Scan Q from left to right then repeat step c until f for each Q elements until Q is empty. If scan result is operand then add it to P. If scan result is “(“ then push to stack. If scan result is “)” then pop element of stack and add to P until found “(“. “(“ is not included in P.

Processes If scan result is operator then: If top element of stack is operator that have higher or equal than the scanned operator then pop the operator in stack to P. For the contrary, push the scanned operator to stack.

Example 1 E = A + B Q : P : A + B ) AB+ ( 1. A ( A 2. + ( + A 3. B ( + Num Symbol Stack P Expression ( 1. A ( A 2. + ( + A 3. B ( + A B 4. ) A B +

Example 2 E = A + (B – C) / D Q : P : A + (B – C) / D ) ABC-D/+ Num Symbol Stack P Expression ( 1. A ( A 2. + (+ A 3. ( (+( A 4. B (+( AB 5. - (+(- AB 6. C (+(- ABC 7. ) (+ ABC- 8. / (+/ ABC- 9. D (+/ ABC-D 10. ) ABC-D/+

Example 3 In Q, there are 20 elements: Q : A + ( B * C - ( D / E ^ F ) * G ) * H Q : A + ( B * C - ( D / E ^ F ) * G ) * H In Q, there are 20 elements: Q : ) A + ( B * C - D / E ^ F ) G H 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

Example 3 Num Symbol Stack P Expression ( 1 A 2 + (+ 3 (+( 4 B AB 5 * (+(* 6 C ABC 7 - (+(- ABC* 8 (+(-( 9 D ABC*D 10 / (+(-(/

Example 3 Jadi P : ABC*DEF^/G*-H*+ Num Symbol Stack P Expression 11 E (+(-(/ ABC*DE 12 ^ (+(-(/^ 13 F ABC*DEF 14 ) (+(- ABC*DEF^/ 15 * (+(-* 16 G ABC*DEF^/G 17 (+ ABC*DEF^/G*- 18 (+* 19 H ABC*DEF^/G*-H 20 ABC*DEF^/G*-H*+ Jadi P : ABC*DEF^/G*-H*+

Manual Way Using [ ] and its format [operand1,operand2,operator] and still concern about operator priority.

Example Q = A + B – C = [AB+] – C P = AB+C- b. Q = A + (B – C) / D P = ABC-D/+

Exercise E = A + BD – F GH K b. E = A + BDH – F G - K Convert infix notation into postfix notation with algoritma way and manual way

 COUNTING IN POSTFIX

Processes Add “)” to sentinel of P. Scan P from left to right. Repeat step C until d for each elements in P until sentinel is found. If scan result is operand then push it to stack.

Processes If scan result is operator (for example opr1) then: Pop from stack then save it into var1 Pop again from stack then save it into var2 Count by format var2 opr1 var1 then save it into hitung variable. Push value of hitung variable into stack. If scan result is “)” then pop element of stack and save it into Value variable.

Example P : P : 2,6,3,-,1,/,+ ABC-D/+ Ex. A=2, B=6, C=3, D=1 ,) Value = 5 Num Symbol Stack Counting Process 1. 2 2 2. 6 2,6 3. 3 2,6,3 4. - 2,3 Var1=3,Var2=6,Hitung=Var2 Opr1 Var1 = 6 - 3 = 3 5. 1 2,3,1 6. / 2,3 Var1=1,Var2=3,Hitung=Var2 Opr1 Var1 = 3 / 1 = 3 7. + 5 Var1=3,Var2=2,Hitung=Var2 Opr1 Var1 = 2 + 3 = 5 8. )

Manual Way Using [ ] by searching first operator from left then count with two left operands using format [operand1 operator operand2] .

Example P : 2,6,3,-,1,/,+ : 2,[6-3],1,/,+ : 2,3,1,/,+ : 2,[3/1],+ : 2,3,+ : [2+3] : 5

Exercise Do the task 6.31 (page 210); 6.32, 6.35-6.37 (page 211) at Data Structures book, seymour. Make a program to convert infix notation into postfix operator and counting in postfix notation.

GRACIAS THANK YOU Copyright © Adam Mukharil Bachtiar 2012 Contact Person: Adam Mukharil Bachtiar Informatics Engineering UNIKOM Jalan Dipati Ukur Nomor. 112-114 Bandung 40132 Email: adfbipotter@gmail.com Blog: http://adfbipotter.wordpress.com Copyright © Adam Mukharil Bachtiar 2012