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.

Slides:



Advertisements
Similar presentations
Data Structures Through C
Advertisements

Stacks, Queues, and Linked Lists
§3 The Stack ADT 1. ADT A stack is a Last-In-First-Out (LIFO) list, that is, an ordered list in which insertions and deletions are.
CSCE 3110 Data Structures & Algorithm Analysis Stacks and Queues Reading: Chap.3 Weiss.
Stacks Chapter 11.
Stacks - 3 Nour El-Kadri CSI Evaluating arithmetic expressions Stack-based algorithms are used for syntactical analysis (parsing). For example.
Stacks Example: Stack of plates in cafeteria.
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.
 Balancing Symbols 3. Applications
Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
E.G.M. Petrakisstacks, queues1 Stacks  Stack: restricted variant of list  elements may by inserted or deleted from only one end : LIFO lists  top: the.
Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Data Structures Stacks.
The Stack and Queue Types Lecture 10 Hartmut Kaiser
Objectives of these slides:
Ceng-112 Data Structures ITurgut Kalfaoglu 1 Chapter 3 Stacks.
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,
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.
Week7 Stack Data Structures & Algorithms. Introduction to Stacks and Queues Widely used data structures Ordered List of element Easy to implement Easy.
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.
Review 1 Introduction Representation of Linear Array In Memory Operations on linear Arrays Traverse Insert Delete Example.
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.
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.
Stacks, Queues & Recursion
Data Structures – Week #3 Stacks. 9.Mart.2012Borahan Tümer, Ph.D.2 Outline Stacks Operations on Stacks Array Implementation of Stacks Linked List Implementation.
Data Structures Stack Namiq Sultan 1. Data Structure Definition: Data structures is a study of different methods of organizing the data and possible operations.
Stack Any Other Data Structure Array Linked List
CHAPTER 3 STACK CSEB324 DATA STRUCTURES & ALGORITHM.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues (part 2)
Review 1 Polish Notation Prefix Infix Postfix Precedence of Operators Converting Infix to Postfix Evaluating Postfix.
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.
CE 221 Data Structures and Algorithms Chapter 3: Lists, Stacks, and Queues - II Text: Read Weiss, §3.6 1Izmir University of Economics.
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.
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
 In general, Queue is line of person waiting for their turn at some service counter like ticket window at cinema hall, at bus stand or at railway station.
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
Chapter 3 Lists, Stacks, Queues. Abstract Data Types A set of items – Just items, not data types, nothing related to programming code A set of operations.
Review Use of Stack Introduction Stack in our life Stack Operations
STACKS & QUEUES for CLASS XII ( C++).
Data Structure By Amee Trivedi.
Introduction Of Stack.
Revised based on textbook author’s notes.
CS 201 Data Structures and Algorithms
MEMORY REPRESENTATION OF STACKS
Lecture No.06 Data Structures Dr. Sohail Aslam
Objectives In this lesson, you will learn to: Define stacks
STACKS.
Stacks Stack: restricted variant of list
Data Structures – Week #3
Algorithms and Data Structures
STACK CHAPTER 03 Developed By :- Misha Ann Alexander Data Structures.
Visit for more Learning Resources
UNIT-I Topics to be covere d 1.Introduction to data structures.
Cs212: Data Structures Computer Science Department Lecture 6: Stacks.
Stacks CS-240 Dick Steflik.
Data Structures – Week #3
Presented by : Aman Gupta PGT CS KV No.1, Narimedu, Madurai
LINEAR DATA STRUCTURES
DATA STRUCTURES IN PYTHON
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  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 can only be removed in the reverse order in which they were added.  The last item represents the top of the stack  A stack is a dynamic structure. It changes as elements are added and removed from it.  It is also known as a LIFO (Last In First Out) structure. Example: Pile of plates, Pile of books etc. TOP

The common operations associated with a stack are as follows: 1.push: adds a new item on top of a stack. 2.pop: removes the item from the top of a stack 3.isEmpty: Check to see if the stack is empty 4.isFull: Check to see if stack is already full 5.returnTop: Indicate which item is at the top

Insert ABCDE A B C D E Delete EDC TOP A B

Stack can be implemented in two ways – 1.Contiguous stack – Using array 2.Linked stack – Using pointer and dynamic memory allocation or using linked list

Algorithm Push(STK, Item): This algorithm inserts an element into stack STK. Item is element which is to be inserted. 1.If(STK -> TOP == (SIZE – 1))\\ Check overflow condition 2. Display “Stack is full” 3. Exit 4.STK -> TOP = STK -> TOP STK -> array [STK ->TOP] = Item Complexity = O(1)

Algorithm Pop(STK): This algorithm returns an element from stack TOP. Item is local variable. 1.If(STK -> TOP == – 1) \\ Check underflow condition 2. Display “Stack is empty” 3. Return NULL 4.Item = STK -> array [STK ->TOP] 5.STK ->TOP = STK ->TOP Return Item Complexity = O(1)

SIZE = 8Push 8, 56, 57, 7, 56, 9 Push 89, 90 5 TOP Push 34 Stack Overflow 4 Times Pop 3 TOP

 Size of stack must be known in advance. In real scenario, it is not possible.  Stack growth should not restricted to finite number of elements.

C code: struct Node{ int INFO; struct Node *NEXT; }STK; STK *TOP; Initialize stack: Algorithm Init_LStack(TOP): This algorithm initialize a stack by creating empty linked list. TOP represents the last item of stack and pointing to empty linked list. 1. TOP = NULL // NULL means stack is empty

Algorithm LPush(TOP, Item): This algorithm inserts an element into stack by inserting as first node into linked list. Item is element which is to be inserted. TOP is pointing to last inserted node i.e. first node of linked list. 1.STK Temp 2.Temp = Allocate memory 3.If(Temp == NULL)\\ Check overflow condition 4. Display “Unable to allocate memory” 5. Exit 6.Temp -> INFO = Item 7.Temp ->NEXT = TOP 8.TOP = Temp Complexity = O(1)

Push 8, 56, 57 8NULL 1. Push 8 TOP 2. Push 56 8NULL56 TOP 3. Push 57 8NULL TOP

Algorithm LPop(TOP): This algorithm returns an element from stack TOP. Item is local variable. 1.STK Temp 2.Temp = TOP 3.If(Temp == NULL) \\ Check underflow condition 4. Display “Stack is empty” 5. Return NULL 6.Item = Temp ->INFO 7.TOP = Temp -> NEXT 8.Free Temp 9.Return Item Complexity = O(1)

8NULL 3. PopTOP = NULL TOP 1. Pop 8NULL 56 TOP 2. Pop 8NULL 5657 TOP 4. PopUnderflow

Algorithm LDispose(TOP): This algorithm deletes stack. 1.STK Temp 2.While(TOP != NULL) \\ Check underflow condition 3. Temp = TOP 4. TOP = TOP ->NEXT 5. Free Temp Complexity = O(n) – n is number of elements in stack

Stack is one of the most commonly used data structures.  Recursive Function – function calls itself. Parameter, local variable status is saved in stack and return address where control is to be return is also saved.  Calling Function – Pass the parameter between function and parameter and local variable are stored in stack.  Arithmetic Expression Evaluation  Expression Conversion  Tower of hanoi  Parsing

We assume that given expression does not contain any unary operation. For same level operations, precedence are performed from left to right. OperationsPrecedence Exponential (↑)1 Multiplication (*)2 Division (/)2 Modulus (%)2 Addition (+)3 Subtraction (-)3 Left Parenthesis 4 NOTE: Lower the number higher the precedence.

An arithmetic expression may be in following forms – 1.Infix – Operator symbol is placed between its two operands like X + Y 2.Prefix – Operator symbol is placed before its two operands like + X Y 3.Postfix – Operator symbol is placed after its two operands like X Y +  In our usual practice, we solve the expressions in infix form. But, for computer program it is very complex to solve an expression in infix form. Therefore, to evaluate arithmetic, first we convert it into prefix or postfix form.

Following conversions are possible – 1.Infix to postfix 2.Infix to prefix 3.Postfix to infix 4.Prefix to infix

Algorithm Infix_To_Post (InFix) – This algorithm takes infix expression and returns equivalent postfix expression. 1.Create empty stack, say STK 2.While (!(InFix END)) 3. pick up the element from InFix and say it is Current 4. If (Current == left Parenthesis) 5. Push(STK, Current) 6. Else If (Current == right Parenthesis) 7. While (!(isEmpty(STK)) && STK -> TOP != left Parenthesis) 8. Operator = Pop(STK) 9. AppendTo(PostFix, Operator) 10. Pop(STK) // remove the left parenthesis and do not append to PostFix 11. Else If(Current == Operand) 12. AppendTo(PostFix, Current)

13. Else If (Current == Operator) 14. While (!(isEmpty(STK)) && Current TOP)) 15. Operator=Pop(STK) 16. AppendTo(PostFix, Operator) 17. Push(STK, Current) 18. Else 19. Display “Incorrect Expression” 20. Exit 21. While (!(isEmpty(STK)) && STK -> TOP != left Parenthesis) 22. Operator = Pop(STK) 23. AppendTo(PostFix, Operator) 24. If (STK -> TOP == left Parenthesis) 25. Display “Incorrect Expression” 26. Exit

InFix Expression – (A – B) * (C / D) Current CharacterPostFix ExpressionStack (( AA( -A( - BA B( - )A B -Empty *A B -* ( * ( CA B – C* ( /A B – C* ( / DA B – C D* ( / )A B – C D /* End of expressionA B – C D / *Empty

InFix Expression – 1. A + B * C - D / E 2. A * B - ( C + D ) + E 3. A + (B * C – (D / E ↑ F) * G) * H PostFix Expression – 1. A B C * + D E / - 2. A B * C D + - E + 3. A B C * D E F ↑ / G * - H * +

Algorithm Infix_To_Pre (InFix) – This algorithm takes infix expression and returns equivalent prefix expression. 1.Create empty stack, say STK 2.RInFix = Reverse(InFix) 3.While (!(RInFix END)) 4. pick up the element from RInFix and say it is Current 5. If (Current == right Parenthesis) 6. Push(STK, Current) 7. Else If (Current == left Parenthesis) 8. While (!(isEmpty(STK)) && STK -> TOP != right Parenthesis) 9. Operator = Pop(STK) 10. AppendTo(PreFix, Operator) 11. Pop(STK) // remove the right parenthesis and do not append to PreFix 12. Else If(Current == Operand) 13. AppendTo(PreFix, Current)

13. Else If (Current == Operator) 14. While (!(isEmpty(STK)) && Current TOP)) 15. Operator=Pop(STK) 16. AppendTo(PreFix, Operator) 17. Push(STK, Current) 18. Else 19. Display “Incorrect Expression” 20. Exit 21. While (!(isEmpty(STK)) && STK -> TOP != right Parenthesis) 22. Operator = Pop(STK) 23. AppendTo(PreFix, Operator) 24. If (STK -> TOP == right Parenthesis) 25. Display “Incorrect Expression” 26. Exit 27. PreFix = Reverse(PreFix)

InFix Expression – (A – B) * (C / D) = Reverse of ) D / C ( * ) B – A ( Current CharacterPreFix ExpressionStack )) DD) /D) / CD C) / (D C /Empty *D C /* ) * ) BD C / B* ) -D C / B* ) - AD C / B A* ) - (D C / B A -* End of ExpressionD C / B A - *Empty Final PreFix * - A B / C D

InFix Expression – 1. A + B * C - D / E 2. A * B - ( C + D ) + E 3. A + (B * C – (D / E ↑ F) * G) * H PreFix Expression – A * B C / D E * A B + C D E 3. + A * - * B C * / D ↑ E F G H

Algorithm Postfix_To_Infix (PostFix) – This algorithm takes postfix expression and returns equivalent infix expression. In this case, we push and pop the strings. str1, str2 and str3 are the temporary variables holding strings. 1.Create empty stack, say STK 2.While (!(PostFix END)) 3. pick up the element from PostFix and say it is Current 4. If(Current == Oprator) 5. str2 = Pop(STK) 6. str3 = Pop(STK) 7. str1 = str3 8. Concatenate str1 and Current 9. Concatenate str1 and str2 10. Push(STK, str1) 11. Else 12. Push(STK, Current) 13. PostFix = Pop(STK)

PostFix Expression - A B C * D E / - + A BABA CBACBA B * C A D B * C A E D B * C A D / E B * C A B * C - D / E AA + B * C - D / E InFix Expression - A + B * C – D / E

Algorithm Prefix_To_Infix (PreFix) – This algorithm takes prefix expression and returns equivalent infix expression. In this case, we push and pop the strings. str1, str2 and str3 are the temporary variables holding strings. 1.Create empty stack, say STK 2.RPreFix = Reverse (PreFix) 3.While (!(RPreFix END)) 4. pick up the element from PreFix and say it is Current 5. If(Current == Oprator) 6. str2 = Pop(STK) 7. str3 = Pop(STK) 8. str1 = str3 9. Concatenate str1 and Current 10. Concatenate str1 and str2 11. Push(STK, str1) 12. Else 13. Push(STK, Current) 14.RInFix = Pop(STK) 15.InFix = Reverse(RInFix)

PreFix Expression - Reverse of * - A B / C D = D C / B A - * D CDCD D / C B D / C A B D / C B - A D / C D / C * B - A InFix Expression - A – B * C / D

 We follow same algorithm as for Postfix to infix.  Here, we compute each step value and then push into stack. Example – 2 3 – * + 2 – ( 4 * 3) = – 3 =

PostFix * * 3 – ^ * 4 / 8 - Infix 9 * 9 – 81 = 0 (2 * 2 ) – 3 = 1 7 * 6 ^ 2 / 4 – 8 = 55

Algorithm Prefix_Eval (PreFix) – This algorithm takes prefix expression and returns evaluated result. In this case, we push and pop the strings. str1, str2 and str3 are the temporary variables holding strings. 1.Create empty stack, say STK 2.RPreFix = Reverse (PreFix) 3.While (!(RPreFix END)) 4. pick up the element from PreFix and say it is Current 5. If(Current == Oprator) 6. str2 = Pop(STK) 7. str3 = Pop(STK) 8. str1 = str3 9. Concatenate str2and Current 10. Concatenate str2and str1 11. Push(STK, str1) 12. Else 13. Push(STK, Current) 14.InFix = Pop(STK)

 Here, we compute each step value and then push into stack. Example – + * * * 2 = Reverse Infix: (2 * 2) + 3 = 7 7

PreFix 1.- * ^ * 4 / 8 – * 4 3 Infix 9 * 9 – 81 = 0 7 * 6 ^ 2 / 4 – 8 = 55 2 – ( 4 * 3) =12

 Its is mathematical puzzle.  It consists of three disks or plates (in standard case) of different sizes.  Disks or plates slide round the one rod.  The puzzle was invented by the French mathematician Édouard Lucas in 1883 Task: Shift all the disks from one rod another with following constraint – 1.Only one disk can be move at a time. 2.Only top most disk can moved. 3.No disk can be placed over the smaller disk. Peg A Peg B

Recurrence relation for Tower of Hanoi – T(n) = 1if n = 1 = 2 T(n-1) + 1 n >1 By solving it, Time complexity = O(2 n )

 Basic operation on stack – 1. Push 2. Pop 3. Dispose  Applications of stack – 1. Evaluation of mathematical expression 2. Conversion of expression from one form to another (Polish Notation) 3. Tower of Hanoi problem