Stack and Its Application

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 32

STACK and its Applications

1
Abstract Data Type

A mathematical definition of objects, with


operations defined on them

2
Examples

 Basic Types
 integer, real (floating point), boolean (0,1), character

 Arrays
 A[0..99] : integer array

 A[0..99] : array of images

0 1 2 3 4 5 6 7 99
A …
2 1 3 3 2 9 9 6 10

0 1 2 99
3
ADT: Array

A mapping from an index set, such as


{0,1,2,…,n}, into a cell type
Objects: set of cells
Operations:
 create(A,n)
 put(A,v,i) or A[i] = v
 value(A,i)

4
Abstract Data Types (ADTs)

 An abstract data type (ADT) is an


abstraction of a data structure

 An ADT specifies:
 Data stored
 Operations on the data
 Error conditions associated with
operations
5
ADT for stock trade

 The data stored are buy/sell orders


 The operations supported are
 order buy (stock, shares)
 order sell(stock, shares )
 void cancel(order)
 Error conditions:
 Buy/sell a nonexistent stock
 Cancel a nonexistent order

6
Stack ADT

Objects:
A finite sequence of nodes
Operations:
 Create
 Push: Insert element at top
 Top: Return top element
 Pop: Remove and return top element
 IsEmpty: test for emptyness
Exceptions

 Attempting the execution of an operation of ADT may


sometimes cause an error condition, called an exception
 Exceptions are said to be “thrown” by an operation that cannot
be executed
 In the Stack ADT, operations pop and top cannot be performed
if the stack is empty
 Attempting the execution of pop or top on an empty stack
throws an EmptyStackException

8
DATA
STRUCTURE

LINEAR DATA NON LINEAR


STRUCTURE DATA
STRUCTURE

ARRAY QUEUE STAC


K
9
What is Linear Data Structure?

 In linear data structure, data is arranged in linear


sequence.

 Data items can be traversed in a single run.

 In linear data structure elements are accessed or placed


in contiguous(together in sequence) memory location.

10
What is Stack?
 A stack is called a last-in-first-out (LIFO) collection.
This means that the last thing we added (pushed) is the
first thing that gets pulled (popped) off.

 A stack is a sequence of items that are accessible at only


one end of the sequence.

11
Examples of Stack:

12
Operations

 PUSH: It is used to insert


items into the stack

 POP: It is used to delete


items from stack.

13
Continue…

14
Insertion in Stack: (push)
Insertion(A,TOP,x,max)
Algorithm to push an item “x” into stack A

If TOP=Max then print ‘STACK OVERFLOW’


Exit
else TOP  TOP+1
A[TOP]  x
Exit

15
Deletion In Stack: (Pop)
Deletion(A,TOP,x)
Algorithm to pop an element “x” from stack A.

If TOP=0 then print ‘STACK UNDERFLOW’


Exit
else item  A[TOP]
TOP  TOP-1
Exit

16
Display Operation in Stack:

Display(TOP,i,A[i])
Algorithm to display the elements in stack “A”

If TOP=0 then print ‘STACK EMPTY’


Exit
else for i  TOP to 0 do
print A[i]
Exit

17
What is this good for ?

• Page-visited history in a Web browser

• Undo sequence in a text editor

• Saving local variables when one function calls


another, and this one calls another

18
Applications of Stacks
1. Reversing Strings:

 A simple application of stack is reversing strings.

 To reverse a string , the characters of string are pushed


onto the stack one by one as the string is read from left
to right.

 Once all the characters of string are pushed onto stack,


they are popped one by one. Since the character last
pushed in comes out first, subsequent pop operation
results in the reversal of the string.
19
For example:

To reverse the string ‘REVERSE’ the string is


read from left to right and its characters are
pushed .

20
Applications of Stacks

2. Checking the validity of an expression


containing nested parenthesis:
 Stacks are also used to check whether a given
arithmetic expressions containing nested
parenthesis is properly parenthesized.

 The program for checking the validity of an


expression verifies that for each left
parenthesis braces or bracket ,there is a
corresponding closing symbol and symbols
are appropriately nested.
21
For example:

VALID INPUTS INVALID INPUTS


{ } { ( }
({ [ ] } ) ([ (()] )
{ [ ] ()} { } [ ] )
[ { ({ } [ ] ([ { )} (] } ]
{
})}] 22
Parentheses Matching Algorithm

23
Applications of Stacks

3. Evaluating arithmetic expressions:

INFIX notation:
The general way of writing arithmetic expressions is
known as infix notation. e.g, (a+b)

PREFIX notation:
e.g, +AB

POSTFIX notation:
e.g: AB+

24
Algorithm to Convert INFIX into
POSTFIX

1. Scan the Infix string from left to right.


2. Initialize an empty stack.
3. If the scanned character is an operand, add it to the Postfix string.
4. If the scanned character is an operator and if the stack is empty
push the character to stack.
5. If the scanned character is an Operator and the stack is not empty,
compare the precedence of the character with the element on top of
the stack.
6. If top Stack has higher precedence over the scanned character pop
the stack else push the scanned character to stack. Repeat this step
until the stack is not empty and top Stack has precedence over the
character.
7. Repeat 4 and 5 steps till all the characters are scanned.

25
Conversion of Infix Into Postfix
Example: Convert Infix expression: (2+(4-1)*3)
into Postfix Expression
CURRENT ACTION STACK POSTFIX
SYMBOL PERFORMED STATUS EXPRESSION
( PUSH ( (
2 2
+ PUSH + (+ 2
( PUSH ( (+( 24
4 24
- PUSH - (+(- 24
1 241
) POP (+ 241-
* PUSH * (+* 241-
3 241-3
) POP * (+ 241-3*
POP + ) 241-3*+ 26
Continue…

 EXAMPLE: A+(B*C-(D/E-F)*G)*H

27
28
Step 1 : Scan the Infix Expression from left to right.

Step 2 : If the scanned character is an operand, append it with final Infix to
Postfix string.

Step 3 : Else,


Step 3.1 : If the precedence order of the scanned(incoming) operator is
greater than the precedence order of the operator in the stack (or the stack is
empty or the stack contains a ‘(‘ or ‘[‘ or ‘{‘), push it on stack.

Step 3.2 : Else, Pop all the operators from the stack which are greater than
or equal to in precedence than that of the scanned operator. After doing
that Push the scanned operator to the stack. (If you encounter parenthesis
while popping then stop there and push the scanned operator in the stack.)

Step 4 : If the scanned character is an ‘(‘ or ‘[‘ or ‘{‘, push it to the stack.

Step 5 : If the scanned character is an ‘)’or ‘]’ or ‘}’, pop the stack and output it
until a ‘(‘ or ‘[‘ or ‘{‘ respectively is encountered, and discard both the
parenthesis.

Step 6 : Repeat steps 2-5 until infix expression is scanned.


29
Step 7 : Print the output
Postfix Evaluation

 Infix expression: (2+(4-1)*3)into Postfix Expression 241-3*+

30
Algorithm to Evaluate Postfix Expression

1. Add ) to postfix expression.


2. Read postfix expression Left to Right until ) encountered
3. If operand is encountered then push it onto Stack

If operator is encounter then Pop two elements


i) A  Top
ii) B  Next_to_Top
iii) Evaluate B op A
push B op A onto Stack
4. Set result  pop
5. END

31
THANK YOU

32

You might also like