2. ILOs
The end of this session, student will be able to,
Define what is stack data structure
Explain the LIFO technique with real world example
Explain the primary operation on stack data structure
Draw a diagram for a given primary operation on stack
Explain what are prefix, infix and postfix operation for a given algorithm
Apply the stack data structure to convert the infix formula to postfix and so on.
2
3. Introduction
Stacks and queues are the two data structures where
insert and delete operations are applied at specific
ends only.
These are special cases of ordered lists and are also
called controlled linear lists.
3
4. Introduction…
There is a wide variety of software applications where
we need these restricted data structure operations.
The following are some examples where stacks and
queues are generally used:
4
5. Introduction…
used in "undo" mechanism in text editor.
Language processing:
space for parameters and local variables is created internally using a stack.
compiler's syntax check for matching braces is implemented by using stack.
support for recursion.
Expression evaluation like postfix or prefix in compilers.
3. Backtracking (game playing, finding paths, exhaustive searching.
4. Memory management, run-time environment for nested language
features. etc
5
6. Introduction…
Stack is a data structure which is used to
handle data in a last-in-first-out (LIFO)
method. That is we can remove the most
recently added element from the stack first.
6
7. Introduction…
In our everyday life, we come across many examples of
stacks, for example, a stack of books, a stack of dishes,
or a stack of chairs. The data structure stack is very
similar to these practical examples
7
8. Introduction…
Consider a stack of books on a table. We can easily
put a new book on the top of the stack, and similarly,
we can easily remove the topmost book as compared
to the books lying in-between or at the bottom
positions.
In the same way, only the topmost element of a stack
can be accessed while direct access of other
intermediate positions is not feasible. Elements may
be added to or removed from only one end, called
the top of a stack.
8
9. Introduction…
The linear data structures such as arrays and
linked lists allow users to insert or delete an
element at any position in the list, that is, we
can insert or delete an element at the
beginning, at the end, or at any intermediate
position.
9
10. Primitive Operations
The three basic stack operations are push, pop, and getTop.
Besides these, there are some more operations that can be
implemented on a stack such as stack_initialization,
stack_empty, and stack_full.
The stack_initialization operation prepares the stack for use
and sets it to a vacant state. The stack_empty operation
simply tests whether the stack is empty.
The stack_empty operation is useful as a safeguard against an
attempt to pop an element from an empty stack.
10
11. Primitive Operations…
Popping an empty stack is an error condition. The
stack_empty condition is also termed stack underflow.
Another stack operation is GetTop. This returns the top
element of the stack without actually popping it.
A few more stack operations include traversing the stack,
counting the total number of elements in the stack, and
copying the stack.
11
12. Primitive Operations…
1. Push—inserts an element on the top of the stack
2. Pop—deletes an element from the top of the stack
3. GetTop—reads (only reading, not deleting) an element from
the top of the stack
4. Stack_initialization—sets up the stack in an empty condition
5. Empty—checks whether the stack is empty
6. Full—checks whether the stack is full
12
21. ALGORITHM OF INSERTION IN STACK: (PUSH)
21
Insertion(a,top,item,max)
If top=max then
print ‘STACK OVERFLOW’
Exit
else
top=top+1 end if
a[top]=item
Exit
22. ALGORITHM OF DELETION IN STACK: (POP)
22
Deletion(a,top,item)
If top=0 then
print ‘STACK UNDERFLOW’
Exit
else
item=a[top]
end if
top=top-1
Exit
23. ALGORITHM OF DISPLAY IN STACK:
23
Display(top,i,a[i])
If top=0 then
Print ‘STACK IS EMPTY’
exit
else
For i=top to 0
Print a[i] End for
exit
24. APPLICATIONS OF STACKS ARE:
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.
24
25. APPLICATIONS OF STACKS ARE:…
To reverse the string ‘REVERSE’ the string is read
from left to right and its characters are pushed .
LIKE:
25
26. APPLICATIONS OF STACKS ARE:…
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.
26
28. APPLICATIONS OF STACKS ARE:…
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+
28
29. Infix to Postfix Conversion
Algorithm basics
Scan expression left to right
When operand found, place at end of new
expression
When operator found, save to determine new
position
29
30. Infix to Postfix Conversion…
1. Operand
Append to end of output expression
2. Operator ^
Push ^ onto stack
3. Operators -,+,/,*
i. Pop and append to the postfix string every operator on the stack that
a. is above the most recently scanned left parenthesis, and
b. has precedence higher than or is a right-associative operator
of equal precedence to that of the new operator symbol.
i. ii. Push the new operator onto the stack.
30
31. Infix to Postfix Conversion…
4. Open parenthesis
Push ( onto stack)
5. Close parenthesis
Pop operators from stack and append to output
Until open parenthesis is popped.
Discard both parentheses
31
32. Infix to Postfix…
Converting the infix expression a + b * c to postfix form
32
33. Infix to Postfix…
Converting an infix expression to
postfix form: A*(B*C+D*E)+F
33