Presented by : Aman Gupta PGT CS KV No.1, Narimedu, Madurai Stack Presented by : Aman Gupta PGT CS KV No.1, Narimedu, Madurai 8/27/2019
Topics to Cover . . . Introduction to some Data Structures Stack – An Introduction Stack – Operations Implementation of Stack Stack using Arrays Applications of Stack Conversion of Infix to Postfix polish notation Evaluation of Postfix expression Questions (Homework Assignment) 8/27/2019
Introduction of Some Data Structures Arrays Linked List Queue Stack 8/27/2019
Arrays 8/27/2019
LINKED LISTS – Connects Nodes 8/27/2019
Queue - FIFO A queue is like a line of people waiting for a bank teller. The queue has a front and a rear. $ $ Front Rear When you think of a computer science queue, you can imagine a line of people waiting for a teller in a bank. The line has a front (the next person to be served) and a rear (the last person to arrive. 8/27/2019
STACKS - LIFO In this Example, we had entered 1, 3 & 9 in the order but we have to retrieve 9 first . 8/27/2019
The Stack Operations Insertion - New data must enter the Stack at the TOP. It is usually called an Push operation. Don’t ask me why the C++ STL used the name push. It only confuses matters with a stack. In any case, when a new item enters a queue, it does so at the rear. 8/27/2019
The Stack Operations Deletion - When an item is taken from the Stack, it always comes from the TOP. It is usually called a Pop operation. When an item is removed from a queue, the removal occurs at the front. 8/27/2019
Implementation of Stack Static Stack – Using Arrays Dynamic Stack – Using Linked Lists 8/27/2019
Implementation using Array It is also Called as Static Stack 8/27/2019
Operations on Stack using Array PUSH Operation ALGORITHM : 1. If TOP = N, then : Print OVERFLOW and Return. 2. Set TOP := TOP + 1. 3. Set STACK[TOP] := ITEM. 4. Return. 8/27/2019
Operations on Stack using Array POP Operation ALGORITHM : 1. If TOP = 0, then : Print UNDERFLOW and Return. 2. Set ITEM:= STACK[TOP]. 3. Set TOP := TOP - 1. 4. Return. 8/27/2019
Review Questions 1. What do you mean by PUSH in terms of Stack? 2. What do you mean by UNDERFLOW & OVERFLOW. 3. What are the different ways to implement Stack? 4. What do you mean by dynamic Stack? 8/27/2019
Applications of Stack Infix to Postfix Conversion Evaluation of Postfix Expression 8/27/2019
Infix to Postfix Conversion Polish Notation The order in which any Operation is performed is determined by the position of Operator & Operands in the expression. Types of Polish notations : Infix Prefix Postfix A + B +AB AB+ 8/27/2019
Infix to Postfix Conversion Precedence of Operators The order in which any Operation is performed in the expression. Exponentiation ( ) Multiplication ( * ) & Division ( / ) Addition ( + ) & Subtraction( - ) 8/27/2019
Infix to Postfix Conversion Need of converting Infix to Postfix While using INFIX Compiler needs to remember the precedence of operators to solve the equation, which is tedious task but if we convert it into post fix notation then it arranges Operators according to their precedence. So, Compiler doesn’t needs to remember that so it saves time & Complexity. 8/27/2019
Infix to Postfix Conversion ALGORITHM 1. Push “(“ onto Stack and Add “)” to the end of Q. 2. Scan Q from left to right and repeat steps 3 to 6 for each element of Q until Stack is empty: 3. If an operand is encountered, add it to P. 4. If a Left parenthesis is encountered, push it on Stack. 5. If an Operator ¤ encountered, then : a) Repeatedly pop from Stack and add to P each operator which has the same precedence as or higher precedence than ¤. b) Add ¤ to the Stack. [ End of If Structure] 8/27/2019
Infix to Postfix Conversion ALGORITHM 6. If a Right parenthesis is encountered, then : a) Repeatedly pop from Stack and add to P each operator until a Left parenthesis is encountered. b) Remove the left Parenthesis. [ End of If Structure] [End of step 2 Loop] 7. Exit. 8/27/2019
Q : A + ( B * C – ( D / E F ) * G ) * H Example : Q : A + ( B * C – ( D / E F ) * G ) * H 8/27/2019
Q : A + ( B * C – ( D / E F ) * G ) * H 8/27/2019
Q : A + ( B * C – ( D / E F ) * G ) * H 8/27/2019
Evaluation of Postfix Expression 1. Add a right parenthesis “)“ at the end of P. 2. Scan p from left to right and repeat step 3 & 4 for each until the sentinel “)” is encountered. 3. If an operand is encountered, put it on Stack. 4. If an operator ¤ is encountered, then : a) Pop the two top elements of Stack, Say A(Top most) and B(Second Top). b) Evaluate A ¤ B. c) Push the result in Stack. [End of If structure] [End of step 2 Loop] 5. Set Value equal to top element of Stack. 6. Exit. 8/27/2019
EXAMPLE P : 5 6 2 + * 12 4 / - As You can see now P is empty i.e. there is no more element in P. So, Value at Stack[TOP] is your answer. In this case it is 37 8/27/2019
Homework Assignment 1. Convert following Infix expression to Postfix : X – y / (z + u) * V 100 + 20 – 50 / 15 * 4 + (8 + 7) 2. Evaluate Following Postfix Expressions: T, F, NOT, AND, F, T, OR, AND 10 20 + 25 15 - * 30 / 8/27/2019
THANX A LOT QUERIES ??? 8/27/2019
Static vs. Dynamic Structures Static Data Structures Fast access to elements Expensive to insert/remove element Have fixed maximum size Memory is reserved at the time of compilation. e.g. Arrays 8/27/2019
Static vs. Dynamic Structures Dynamic Data Structures Fast insertion/deletion of element Slower access to the element Have flexible size Memory allocation for the data structure takes place at the run time, only required amount of memory is allocated. e.g. Linked lists, Stacks, Queues, Trees etc. 8/27/2019
Static vs. Dynamic Structures A static data structure has a fixed size e.g. Arrays are static; once you define the number of elements it can hold, the number doesn’t change A dynamic data structure grows and shrinks at execution time as required by its contents A dynamic data structure is implemented using links 8/27/2019
Drawbacks of Static Data Structures Memory – We had to define memory in advance which will either waste Memory or create the space problem Time – For Insertion & Deletion 8/27/2019
Array Implementation A Stack can be implemented with an array, as shown here. Just like our stack implementation in the previous chapter, one way to implement a queue is to store the elements in an array. 8/27/2019