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.

Slides:



Advertisements
Similar presentations
Data Structures Through C
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 Chapter 11.
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)
Stacks Example: Stack of plates in cafeteria.
CS 206 Introduction to Computer Science II 03 / 04 / 2009 Instructor: Michael Eckmann.
C o n f i d e n t i a l Developed By Nitendra NextHome Subject Name: Data Structure Using C Unit : Overview of Queues.
Topic 15 Implementing and Using Stacks
Department of Technical Education Andhra Pradesh
Infix, Postfix, Prefix.
Chapter 6 Stacks. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 6-2 Chapter Objectives Examine stack processing Define a stack abstract.
Topic 15 Implementing and Using Stacks
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.
Objectives of these slides:
Data Structures Using C++ 2E Chapter 7 Stacks. Data Structures Using C++ 2E2 Objectives Learn about stacks Examine various stack operations Learn how.
Data Structures Lecture : Stacks (Infix, Postfix and Prefix Expressions) Azhar Maqsood NUST Institute of Information Technology (NIIT)
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.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
Chapter 4 Stacks Stacks A stack is a linear data structure that can be accessed only at one of its ends for storing and retrieving. Its called.
Computer Science Department Data Structure & Algorithms Problem Solving with Stack.
SAK 3117 Data Structures Chapter 3: STACKS. Objective To introduce: Stack concepts Stack operations Stack applications CONTENT 3.1 Introduction 3.2 Stack.
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.
DATA STRUCTURE & ALGORITHMS CHAPTER 3: STACKS. 2 Objectives In this chapter, you will: Learn about stacks Examine various stack operations Discover stack.
CHAPTER 61 STACK, QUEUES, RECURSION. Introduction when one wants to restrict insertion and deletion so that they can take place only at the beginning.
Stacks 1. Stack  What is a stack? An ordered list where insertions and deletions occur at one end called the top. Also known as last-in-first-out (LIFO)
 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+
Stack Any Other Data Structure Array Linked List
CHAPTER 3 STACK CSEB324 DATA STRUCTURES & ALGORITHM.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 19: Stacks and Queues (part 2)
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues (part 2)
Data Structures & Algorithms
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.
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.
Prof. I. J. Chung Data Structure #5 Professor I. J. Chung.
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(),
Lecture - 6(Stacks) On Data structures. Prepared by, Jesmin Akhter, Lecturer, IIT,JU Lecture Outline What is a Stack? Array implementation of stacks Operations.
CE 221 Data Structures and Algorithms Chapter 3: Lists, Stacks, and Queues - II Text: Read Weiss, §3.6 1Izmir University of Economics.
Data Structures & Algorithm CS-102
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
 Chapter 7 introduces the stack data type.  Several example applications of stacks are given in that chapter.  This presentation shows another use called.
Review Use of Stack Introduction Stack in our life Stack Operations
Introduction Of Stack.
Infix to postfix conversion
Objectives In this lesson, you will learn to: Define stacks
Stacks.
Copyright ©2012 by Pearson Education, Inc. All rights reserved
STACKS.
Stacks Chapter 4.
Data Structures – Week #3
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.
STACK, QUEUES, RECURSION
UNIT-I Topics to be covere d 1.Introduction to data structures.
(Part 2) Infix, Prefix & Postfix
Stack.
Presented by : Aman Gupta PGT CS KV No.1, Narimedu, Madurai
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:

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

Unit-3 Overview of Stack Developed By Nitendra Stack: A Stack is defined formally as a list (a linear data structure) in which all insertion and deletion are made at one end and that end is called the top of the stack. Here, the last item inserted will be on top of stack. Since deletion is done from the same end, Last item Inserted is the first item to be deleted out from the stack and so, stack is also called Last In First Out (LIFO) data structure. There are three basic operations:  PUSH ( Insert an item into the stack )  POP ( Delete an item from the stack )  LIST ( List the stack elements ) Overview of Stack NextHome

Unit-3 Overview of Stack Developed By Nitendra From the definition of stack it is clear that it is a collection of similar type of items and naturally we can use an array (An array is a collection of similar data types) to hold the items of stack. Since array is used, its size is fixed and that size is called STACK_SIZE. At the time of push operation, the stack full condition must be checked. If stack is full called “Stack Overflow” at this condition no element can be inserted into the stack. Otherwise push the element into the stack. At the time of pop operation, the stack empty condition must be checked. If stack is empty called “Stack Underflow” at this condition no element can be deleted from the stack. Otherwise pop the element from the stack. Operation NextHome

Unit-3 Overview of Stack Developed By Nitendra Example (PUSH Operation) : So, let us assume that 5 items 30, 20, 25, 10 and 40 are to be placed on the stack. The items can be inserted one by one as shown in following figure. After inserting 40 the stack is full. In this situation it is not possible to insert any new item. This situation is called stack overflow. NextHome

Unit-3 Overview of Stack Developed By Nitendra Example (POP Operation) : When an item is to be deleted, it should be deleted from the top as shown in following figure. The items deleted in order are 40, 10, 25, 20 and 30. Finally, when all items are deleted, top points to bottom of stack. When the stack is empty, it is not possible to delete any item and this situation is called stack underflow. NextHome

Unit-3 Overview of Stack Developed By Nitendra Example (LIST Operation) : Assume that the stack contains three elements as shown below: The item 30 is at the top of the stack and item 10 is at the bottom of the stack. Usually, the contents of the stack are displayed from the bottom of the stack till the top of the stack is reached. So, first item to be displayed is 10, next item to be displayed is 20 and final item to be displayed is 30 NextHome

Unit-3 Overview of Stack Developed By Nitendra TOP is a pointer which moves when PUSH and POP operations are occurred. Initially, the pointer TOP =-1, implies no value is present in the stack, that is stack empty. When PUSH operation occurred then TOP = TOP + 1 and when POP operation occurred then TOP = TOP - 1 when TOP = -1, implies underflow TOP = STACK_SIZE -1, implies Overflow Item is the value to be inserted or deleted General Algorithm for Stack Operation NextHome

Unit-3 Overview of Stack Developed By Nitendra PUSH(stack,TOP,item) Step I : If TOP=STACK_SIZE - 1, then print “Stack Overflow” and Return end if Step II: set TOP  TOP+1 Step III: set stack[TOP]  item Step IV: Return Algorithm for PUSH Operation NextHome

Unit-3 Overview of Stack Developed By Nitendra POP(stack,TOP,item) Step I : If TOP=-1, then print “Stack Underflow” and Return end if Step II: set item  stack[TOP] Step III: TOP  TOP -1 Step IV: Return Algorithm for POP Operation NextHome

Unit-3 Overview of Stack Developed By Nitendra A stack is very useful in situations when data have to be stored and then retrieved in the reverse order. Some applications of stack are listed below:  Function Calls: Stacks play in nested function calls. When the main program calls a function named F(), a stack frame for F() gets pushed on top of the stack frame for main. If F() calls another function G(), a new stack frame for G() is pushed on top of the frame for F(). When G() finishes its processing and returns, its frame gets popped off the stack, restoring F() to the top of the stack. In case of recursion a system stack is maintained to keep the track of function calls. Applications of Stack NextHome

Unit-3 Overview of Stack Developed By Nitendra As another example, consider adding very large numbers. Suppose we wanted to add 353,120,457,764,910,452,008,700 and 234,765,000,129,654,080,277. First of all note that it would be difficult to represent the numbers as integer variables or other, as they cannot hold such large values. The problem can be solved by treating the numbers as strings of numerals, store them on two stacks, and then perform addition by popping numbers from the stacks. Large Number Arithmetic NextHome

Unit-3 Overview of Stack Developed By Nitendra Stacks are useful in evaluation of arithmetic expressions. The process of writing the operators on an expression either before operands or after operands or in between them is called Polish Notation, in the honour of its discover, the Polish mathematician Jan Luksiewiez. The fundamental property is that the order in which the operations are to be performed is completely determined by the positions of the operators and operands is the expression. Accordingly, one never needs parenthesis when writing expression in Polish notation. There are 3 types of notations for expressions. The standard form is known as the infix form. The other two are postfix and prefix forms. Evaluation of Arithmetic Expressions NextHome

Unit-3 Overview of Stack Developed By Nitendra Infix: operator is between operands A + B. Postfix : operator follows operands A B + The name came according to position of Prefix: operator precedes operands + A B the operator Example 1 (Infix to Postfix): a + b * c Infix form (precedence of * is higher than of +) a + (b * c) convert the multiplication a + ( b c * ) convert the addition a (b c * ) + Remove parentheses a b c * + Postfix form Note that there is no need of parentheses in postfix forms. NextHome

Unit-3 Overview of Stack Developed By Nitendra Example 2(Infix to Prefix): ( A + B ) * C Infix form ( + A B ) * C Convert the addition * (+ A B ) C Convert multiplication * + A B C Prefix form No need of parenthesis anywhere NextHome

Unit-3 Overview of Stack Developed By Nitendra Input : Infix(Q) Output : Postfix(P) Polish(Q,P) Step I : Push “(“ on to the stack and add “)” at the end of Q Step II: Scan Q from left to right and repeat Step III to Step IV for each element of Q until the stack is empty. Step III: If an operand is encountered add it to P. Step IV: It a left parenthesis “(“ is encountered push it on to Stack Step V: If an operator is encountered then:- a) Repeatedly pop from Stack and add to P of each operator which has the same precedence as or higher precedence then b) Add to Stack [End of If Statement] Algorithm : Infix to Postfix Conversion NextHome

Unit-3 Overview of Stack Developed By Nitendra Step Vi: If the right parenthesis is encountered then:- a) Repeatedly pop from Stack and add to P of each operator until a left parenthesis is encountered b) Remove left parenthesis from Stack [End of If Statement] [End of Step II Loop] Step VII: Exit Example : Q ≡ A + ( B * C - ( D / E ^ F ) * G ) * H [ Infix Notation ] P ≡ A B C * D E F ^ / G * - H * +[Postfix Notation] NextHome

Unit-3 Overview of Stack Developed By Nitendra Scan Symbols StackExpression P A(A +(+A ((+(A B AB *(+(*AB C(+(*ABC -(+(-ABC* ((+(-(ABC* D(+(-(ABC*D /(+(-(/ABC*D Scan Symbols StackExpression P E(+(-(/ABC*DE ^(+(-(/^ABC*DE F(+(-(/^ABC*DEF )(+(-ABC*DEF^/ *(+(-*ABC*DEF^/ G(+(-*ABC*DEF^/G )(+ABC*DEF^/G*- *(+*ABC*DEF^/G*- H(+*ABC*DEF^/G*-H )EmptyABC*DEF^/G*-H*+ Q ≡ A + ( B * C - ( D / E ^ F ) * G ) * H [ Infix Notation ] P ≡ A B C * D E F ^ / G * - H * + [ Postfix Notation ] (1) (2) NextHome

Unit-3 Overview of Stack Developed By Nitendra This algorithm evaluate an postfix expression (P) Step I : Add a right parenthesis “)” at the end of P Step II: Scan P from left to right and repeat Step III to Step IV for each element of P until the right parenthesis is encountered. Step III: If an operand is encountered then put it into the stack. Step IV: If an operator is encountered then:- a) Remove two top elements from Stack where A is the top and B is the next top element b) Evaluate B A and push into Stack [End of If Statement] [End of Step II Loop] Step V: Set value = top element of Stack Step VI: Exit Algorithm : Postfix Evaluation NextHome

Unit-3 Overview of Stack Developed By Nitendra Infix Expression : Q = 5 * ( ) - ( 12 / 4 ) Postfix Expression : P = * ) Result : 37 Scan Character Stack * / )Stop Postfix Evaluation NextHome

Unit-3 Overview of Stack Developed By Nitendra