0% found this document useful (0 votes)
7 views24 pages

Stack

The document provides an overview of stacks, a data structure where elements are added and removed from the top, following the Last In, First Out (LIFO) principle. It describes basic operations such as Push and Pop, and explains how stacks can be represented in memory using arrays or linked lists. Additionally, it discusses the application of stacks in evaluating arithmetic expressions and converting infix notation to postfix notation.

Uploaded by

baktiavishek
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views24 pages

Stack

The document provides an overview of stacks, a data structure where elements are added and removed from the top, following the Last In, First Out (LIFO) principle. It describes basic operations such as Push and Pop, and explains how stacks can be represented in memory using arrays or linked lists. Additionally, it discusses the application of stacks in evaluating arithmetic expressions and converting infix notation to postfix notation.

Uploaded by

baktiavishek
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

Data Structur

ICT2101
Dr. Mohammad Abu Yousuf
[email protected]

1
STACKS

2
What is STACKS ?

• It is an ordered group of homogeneous items of


elements. Elements are added to and removed from the
top of the stack (the most recently added items are at the
top of the stack). The last element to be added is the first
to be removed (LIFO: Last In, First Out).

• A stack is a list of elements in which an element may be


inserted or deleted only at one end, called TOP of the
stack. The elements are removed in reverse order of
that in which they were inserted into the stack.

3
What is STACKS ?

4
Basic operations

• These are two basic operations associated with stack:


– Push() is the term used to insert/add an element into
a stack.
– Pop() is the term used to delete/remove an element
from a stack.
• There are two ways to represent Stack in memory. One is
using array and other is using linked list.

5
Array representation of stacks

• Usually the stacks are represented in the computer by a


linear array.
• In the following algorithms/procedures of pushing and
popping an item from the stacks, we have considered, a
linear array STACK, a variable TOP which contain the
location of the top element of the stack; and a variable
STACKSIZE which gives the maximum number of
elements that can be hold by the stack.

6
Array representation of stacks

7
Push Operation
• Push an item onto the top of the stack (insert an item)

8
Push Operation

• Push an item onto the top of the stack (insert an item)

9
Pop Operation
• Pop an item off the top of the stack (delete an item)

10
Pop Operation

• Pop an item off the top of the stack (delete an item)

11
Here are the minimal operations we'd need for an abstract stack (and their
typical names):
o Push: Places an element/value on top of the stack.
o Pop: Removes value/element from top of the stack.
o IsEmpty: Reports whether the stack is Empty or not.
o IsFull: Reports whether the stack is Full or not.

12
13
14
Application of the Stack (Arithmetic
Expressions)
INFIX , POST FIX AND PRE FIX NOTATIONS :

Example: 3+4 (infix), 3 4 + (postfix), + 3 4 (prefix)

• Stacks are used by compilers to help in the process of


converting infix to postfix arithmetic expressions and also
evaluating arithmetic expressions.
• Arithmetic expressions consisting variables, constants,
arithmetic operators and parentheses.
15
Application of the Stack (Arithmetic
Expressions)
• To evaluate a complex infix expression, a compiler would
first convert the expression to postfix notation, and then
evaluate the postfix version of the expression.
• We use the following three levels of precedence for the
five binary operations.

16
Application of the Stack (Arithmetic
Expressions)
• Example:
(66 + 22) * 5 – 567 / 42
to postfix
66 22 + 5 * 567 42 / –

17
Transforming Infix Expression into Postfix
Expression
• The following algorithm transform the infix expression Q
into its equivalent postfix expression P. It uses a stack to
temporary hold the operators and left parenthesis.

• The postfix expression will be constructed from left to


right using operands from Q and operators popped from
STACK.

18
Transforming Infix Expression into Postfix
Expression

19
• Convert Q: A+( B * C – ( D / E ^ F ) * G ) * H into postfix form
showing stack status .
• Now add “)” at the end of expression
A+( B * C – ( D / E ^ F ) * G ) * H ) and also Push a “(“ on Stack.

Example

20
Evaluation of Postfix Expression
• If P is an arithmetic expression written in postfix
notation. This algorithm uses STACK to hold operands,
and evaluate P.

21
Evaluation of Postfix Expression

22
Evaluation of Postfix Expression

23
Thank you

24

You might also like