Download presentation
Presentation is loading. Please wait.
Published byЛюбовь Кириленко Modified over 5 years ago
1
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
2
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
3
Introduction of Some Data Structures
Arrays Linked List Queue Stack 8/27/2019
4
Arrays 8/27/2019
5
LINKED LISTS – Connects Nodes
8/27/2019
6
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
7
STACKS - LIFO In this Example, we had entered 1, 3 & 9 in the order but we have to retrieve 9 first . 8/27/2019
8
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
9
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
10
Implementation of Stack
Static Stack – Using Arrays Dynamic Stack – Using Linked Lists 8/27/2019
11
Implementation using Array
It is also Called as Static Stack 8/27/2019
12
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
13
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
14
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
15
Applications of Stack Infix to Postfix Conversion
Evaluation of Postfix Expression 8/27/2019
16
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
17
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
18
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
19
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
20
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
21
Q : A + ( B * C – ( D / E F ) * G ) * H
Example : Q : A + ( B * C – ( D / E F ) * G ) * H 8/27/2019
22
Q : A + ( B * C – ( D / E F ) * G ) * H
8/27/2019
23
Q : A + ( B * C – ( D / E F ) * G ) * H
8/27/2019
24
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
25
EXAMPLE P : * / - 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
26
Homework Assignment 1. Convert following Infix expression to Postfix :
X – y / (z + u) * V – 50 / 15 * 4 + (8 + 7) 2. Evaluate Following Postfix Expressions: T, F, NOT, AND, F, T, OR, AND * 30 / 8/27/2019
27
THANX A LOT QUERIES ??? 8/27/2019
28
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
29
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
30
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
31
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
32
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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.