STACKS.

Slides:



Advertisements
Similar presentations
Data Structures Through C
Advertisements

CS Data Structures ( 資料結構 ) Chapter 3: Stacks and Queues Spring 2012.
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.
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.
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.
Infix, Postfix, Prefix.
CS 206 Introduction to Computer Science II 10 / 15 / 2008 Instructor: Michael Eckmann.
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 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 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,
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.
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, Queues & Recursion
 STACK STACK  BASIC STACK OPERATIONS BASIC STACK OPERATIONS  PUSH ALGORITHM PUSH ALGORITHM  POP ALGORITHM POP ALGORITHM  EVALUATING A POSTFIX EXPRESSION.
Stack Any Other Data Structure Array Linked List
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)
CHP-3 STACKS.
Part 2. Deletion from a linked list Let LIST be a linked list with a node N between nodes A and B. suppose node N is to be deleted from the linked list.
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.
Data Structures & Algorithm CS-102
Part 2. Deletion from a linked list Let LIST be a linked list with a node N between nodes A and B. suppose node N is to be deleted from the linked list.
© 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
STACKS & QUEUES for CLASS XII ( C++).
Chapter 4 Stacks
Data Structures Using C++ 2E
Stacks Chapter 5.
Introduction Of Stack.
Stacks Stacks.
MEMORY REPRESENTATION OF STACKS
DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING IN C++
Objectives In this lesson, you will learn to: Define stacks
Stacks.
Stacks Stack: restricted variant of list
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
INSERTION INTO A LINEAR ARRAY Set J = N Repeat step 3 and 4 while J>= K Set LA[ J+1] = LA [ J ] Set J = J-1 Set LA [K] = ITEM Set N = N+1 Exit.
Stacks Chapter 5 Adapted from Pearson Education, Inc.
More About Stacks: Stack Applications
Stack and Queues Stack implementation using Array
STACK, QUEUES, RECURSION
UNIT-I Topics to be covere d 1.Introduction to data structures.
The List, Stack, and Queue ADTs
Cs212: Data Structures Computer Science Department Lecture 6: Stacks.
Stacks Data structure Elements added, removed from one end only
(Part 2) Infix, Prefix & Postfix
More About Stacks: Stack Applications
Chapter 7 (continued) © 2011 Pearson Addison-Wesley. All rights reserved.
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:

STACKS

Stack- A stack is a linear data structure in which items may be added or removed only at one end . Accordingly, stacks are also called last-in-first-out or LIFO lists. The end at which element is added or removed is called the top of the stack. Two basic operations associated with stacks are : Push- Term used to denote insertion of an element onto a stack. Pop- Term used to describe deletion of an element from a stack. The order in which elements are pushed onto a stack is reverse of the order in which elements are popped from a stack

Representation of stacks Stacks may be represented in memory in various ways usually by means of one-way list or a linear array In array representation of stack, stack is maintained by an array named STACK , a variable TOP which contains the location/index of top element of stack and a variable MAXSTK giving the maximum number of elements that can be held by the stack. The condition TOP=0 or TOP=NULL indicates that stack is empty. The operation of addition of an item on stack and operation of removing an item from a stack may be implemented respectively by sub algorithms, called PUSH and POP. Before executing operation PUSH on to a stack, one must first test whether there is room in the stack for the new item. If not, then we have the condition known as overflow. Analogously, in executing the POP operation, one must first test where there is an element in stack to be deleted. If not, then we have condition known as underflow.

ARRAY IMPLEMENTATION OF STACK

Step 1: [Stack already filled] If TOP==MAXSTK-1, then: Algorithm: PUSH (STACK,TOP,MAXSTK,ITEM) This algorithm pushes an item onto the stack array. TOP stores the index of top element of the stack and MAXSTK stores the maximum size of the stack. Step 1: [Stack already filled] If TOP==MAXSTK-1, then: Write: ‘OVERFLOW’ Return Step 2: Set TOP:=TOP+1 Step 3: Set STACK[TOP]:=ITEM Step 4: Return

Step 2: Set ITEM:=STACK[TOP] Step 3: Set TOP:=TOP-1 Step 4: Return Algorithm: POP(STACK,TOP,ITEM) This procedure deletes the top element of STACK array and assign it to variable ITEM Step 1: If TOP== -1,then: Write: ‘UNDERFLOW’ Return Step 2: Set ITEM:=STACK[TOP] Step 3: Set TOP:=TOP-1 Step 4: Return

A stack represented using a linked list is also known as linked stack. The array based representation of stack suffers from following limitations: Size of stack must be known in advance Representing stack as an array prohibits the growth of stack beyond finite number of elements. In a linked list implementation of stack, each memory cell will contain the data part of the current element of stack and pointer that stores the address of its bottom element and the memory cell containing the bottom most element will have a NULL pointer

Push operation on linked list representation of stack Algorithm: PUSH(INFO, LINK, TOP, ITEM, AVAIL) This algorithm pushes an element to the top of the stack Step 1: If AVAIL==NULL, then Write: ‘OVERFLOW’ Return Step 2: Set NEW=AVAIL and AVAIL = AVAIL->LINK Step 3: Set NEW->INFO =ITEM Step 4: Set NEW->LINK=TOP Set TOP=NEW Step 5: EXIT

POP operation on linked list representation of stack Algorithm: POP(INFO, LINK, TOP, AVAIL, ITEM) This algorithm deletes an element from the top of the stack Step 1: If TOP==NULL , then: Write: ‘UNDERFLOLW’ AND Exit Step 2: Set ITEM = TOP->INFO Step 3: Set TEMP = TOP AND TOP= TOP->LINK Step 4: TEMP->LINK = AVAIL AND AVAIL = TEMP Step 5: EXIT

Application of stack Evaluation of arithmetic expression. For most common arithmetic operations, operator symbol is placed between its operands. This is called infix notation of an expression. To use stack to evaluate an arithmetic expression, we have to convert the expression into its prefix or postfix notation. Polish notation , refers to the notation in which operator symbol is placed before its two operands. This is also called prefix notation of an arithmetic expression. The fundamental property of polish notation is that the order in which operations are to be performed is completely determined by positions of operators and operands in expression. Accordingly, one never needs parentheses when writing expression in polish notation. Reverse Polish notation refers to notation in which operator is placed after its two operands. This notation is frequently called postfix notation. Examples of three notations are:

INFIX NOTATION: A+B PREFIX OR POLISH NOTATION: +AB POSTFIX OR REVERSE POLISH NOATATION: AB+ Convert the following infix expressions to prefix and postfix forms A+(B*C) (A+B)/(C+D) Prefix: +A*BC Postfix: A BC*+ Prefix: / +AB+CD Postfix: AB+CD+/

Infix: Postfix/ Prefix Example: Infix: Postfix/ Prefix A + B - C AB + C - - + ABC (A + B) * (C - D) AB + CD - * * + AB - CD A ^ B * C - D + E / F /(G + H) AB ^ C * D - EF / GH + / + + - * ^ ABCD / / EF + GH ((A + B) * C – (D - E)) ^ (F + G) AB + C * DE - - FG + ^ ^ - * + ABC – DE + FG A - B/(C * D ^ E) ABCDE ^ * / - - A / B * C ^ DE

The computer usually evaluates an arithmetic expression written in infix notation in two steps. Converts expression to postfix notation Evaluates the postfix notation. Evaluation of postfix expression Suppose P is an arithmetic expression written in postfix notation. The following algorithm which uses a STACK to hold operands and evaluates P

Algorithm: This algorithm finds the VALUE of an arithmetic expression P written in Postfix notation. Step 1: Add a right parentheses “ )” at the end of P Step 2: Scan P from left to right and repeat step 3 and 4 for each element of P until “)” is encountered Step 3: If an operand is encountered, put the operand on the stack Step 4: If an operator is encountered , then: (a) Remove the two top elements of stack, where A is top element and B is next-top-element. (b) Evaluate B A (c ) Place the result of (b) back on stack [End of if structure] [End of step 2 loop] Step 5: Set VALUE equal to the top element of stack Step 6: Exit

Transforming Infix Expression into Postfix Expression The algorithm uses a stack to temporarily hold operators and left parentheses. The postfix expression P will be constructed from left to right using operands from Q and operators which are removed from STACK. The algorithm begins by pushing a left parentheses onto stack and adding a right parentheses at the end of Q Algorithm: POSTFIX (Q, P) Suppose Q is an arithmetic expression written in infix notation. This algorithm finds the equivalent postfix expression P Step 1: Push “(“ on to the STACK and add “)” to the end of Q Step 2: Scan Q from left to right and repeat step 3 to 6 for each element of Q until the STACK is empty Step 3: If an operand is encountered, add it to P Step 4: If left parentheses is encountered, add it to STACK

Step 5: If an operator is encountered, then: (a) Repeatedly pop from STACK and add to P each operator which has same precedence or higher precedence than (b) Add to STACK Step 6: If a right parentheses is encountered , then: (a) Repeatedly pop from STACK and add to P each operator until a left parentheses is encountered (b) Remove the left parentheses [End of Step 2 Loop] Step 7: Exit

Example: Convert Q=A+(B*C) / D to its corresponding postfix form Solution: put “)” at the end of Q and put “(“ on stack Starting from left: Operand A , put it on P Operator + move to stack as no operator there ( move on stack Operand B, put it on P Operator * , move to stack as no operator Operand C , move to P ) , pop from stack and put on P until “ (“ is encountered. Pop “(“ also operator /, as precedence of / is higher than + on stack, no pop possible. Push / on stack Operand D , put it on P Right parentheses ), Pop all the elements and add the P until ( is encountered. Also remove ( from stack P= A B C* D / + * + ( / + ( ( + (

Example: Consider the following infix expression Q: Q : A + ( B * C – ( D / E ^ F) * G) * H Convert Q into it’s postfix expression p using stack. Ans: P : A B C * D E F ^ / G * - H * + Example: Consider the following infix expression : A + B / C + D * (E – F) ^ G Convert Q into it’s postfix expression using stack. Ans: A B C / + D E F - G ^ * +

Practical applications of stack Stacks are used for implementing function calls in a program Used for implementing recursion. Used for conversion of infix expression to its postfix or prefix form Used for evaluation of postfix expression. Used in sorting of arrays (quicksort and mergesort technique)

Infix to Postfix Example A + B * C - D / E Solve by Yourself: Infix Stack Postfix Expression

Postfix Evaulation Operand: push Operator: pop 2 operands, do the math, pop result back onto stack 1 2 3 + * Postfix Stack[top ] 2 3 + * 1 3 + * 1 2 + * 1 2 3 * 1 5 // 5 from 2 + 3 5 // 5 from 1 * 5

Ex: Evaluate the following postfix expression 1- p= 5, 6, 2, +, *, 12, 4, /, - Ans: 37 2- p=6, 2, 3, +, -, 3, 8, 2, /, +, *, 2, ^, 3, + Ans: 52 3- Convert infix to postfix and evaluate for a=3, b=4, c=5 Infix: (A+B) *C

Recursive Solution

Recursive Solution

Recursive Solution

Recursive Solution

Tower of Hanoi

Tower of Hanoi

Tower of Hanoi

Tower of Hanoi

Tower of Hanoi

Tower of Hanoi

Tower of Hanoi

Tower of Hanoi

Recursive Algorithm TOWER(N, BEG, AUX, END) 1- (If N==1) 1- Write BEG ->END 2- Return [End of If structure] 2- Call TOWER(N-1, BEG, END, AUX) 3- Write: BEG->END 4- Call TOWER(N-1, AUX, BEG, END) 5- Return

Let Gn(i) be a function from [0,…,2n-1] Total no of moves = 2n-1

Application of Stack: Quick Sort It is an algorithm of divide and conquer.

Example We are given array of n integers to sort: 40 20 10 80 60 50 7 30 70

Pick Pivot Element There are a number of ways to pick the pivot element. In this example, we will use the first element in the array: 40 20 10 80 60 50 7 30 70

Partition Result 7 20 10 30 40 50 60 80 70 [0] [1] [2] [3] [4] [5] [6] [7] [8]

Quick sort algorithm PARTITION(A, BEG, END, LOC) 1- Set LEFT = BEG, RIGHT=END, LOC=BEG 2- [Scan from right to left] (a) Repeat while A[LOC] <=A[RIGHT] and LOC != RIGHT RIGHT = RIGHT-1 [End of Loop] (b) If LOC == RIGHT then return (c) If A[LOC] > A[RIGHT] then (i) Interchange A[LOC] and A[RIGHT] (ii) Set LOC = RIGHT [End of if structure] 3- [Scan from left to right] (a) Repeat while A[LEFT] <=A[LOC] and LEFT != LOC LEFT = LEFT+1 (b) If LOC == LEFT then return (c) If A[LEFT] > A[LOC] then (i) Interchange A[LEFT] and A[LOC] (ii) Set LOC = LEFT (iii) Go to step 2.

Quick sort algorithm QUICKSORT() 1- TOP = -1 2- If N >1 then TOP = TOP + 1, LOWER[TOP] = 0, UPPER[TOP] = N-1 3- Repeat steps 4 to 7 while TOP != -1 4- [Pop sublist from stacks] Set BEG = LOWER[TOP] , END = UPPER[TOP] TOP = TOP – 1 5- Call PARTITION (A, BEG, END, LOC) 6- [ Push left sublist onto stacks when it has 2 or more elements] If BEG < LOC-1 then TOP = TOP + 1 LOWER[TOP] = BEG UPPER[TOP] = LOC-1 [End of if Structure] 7- [ Push right sublist onto stacks when it has 2 or more elements] If LOC+1 < END then TOP = TOP+1 LOWER[TOP] = LOC+1 UPPER[TOP] = END [End of step 3 loop] 8- Exit

Complexity of Quick Sort Worst case O(n2) Average case O(n log n)