A Linear Data Structure: Stack

You might also like

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 23

Stack

A Linear Data Structure

BY :- SANTOSH K DEVAL
PGT(Comp.Sc.)
SANTOSH K DEVAL, PGT (CS), KV2,
JAMNAGAR
DATA STRUCTURE:-

A data structure is a named group of data of different


data types which can be processed as a single unit.

Need of data Structure-

Some times a need arises to treat a group of different


data types as a single unit.Data structure are very
important in a computer system as these not only allow
the user to combine various data types in a group but
also allow processing of the
group as a single unit .
S. K. DEVAL, PGT(CS), KV2, JAMNAGAR
Different Data Structure

Data Structure

Simple Compound

Array Structure Linear Non Linear

Stack Queue Tree

S. K. DEVAL, PGT(CS), KV2, JAMNAGAR


Linear Data Structure
A data structure is said to be linear if its
elements form a sequence.

Linear data structure

Stack Queue Linked list


S. K. DEVAL, PGT(CS), KV2, JAMNAGAR
Stack:
Stack data structures refer to the lists stored
and accessed in a special way ,where LIFO
(last in first out ) techniques is followed.
In stacks, insertions and deletion take place
only at one end, called top. This mechanism is
similar to a stack of plates in a dinner party.

S. K. DEVAL, PGT(CS), KV2, JAMNAGAR


Stack of Plates
S. K. DEVAL, PGT(CS), KV2, JAMNAGAR
Basic Operation on Stack

 Push- Insertion in a stack


 Pop- Deletion in stack

S. K. DEVAL, PGT(CS), KV2, JAMNAGAR


Push Operation
top P
4
4

top X X
3
3

Z Push P
2 Z
2

Y 1 Y
1

N 0 N
0
S. K. DEVAL, PGT(CS), KV2, JAMNAGAR
Algorithm for pushing in stack array
/* Assuming that array-stack can hold maximum N elements
1. top = -1
2. Read Item
3. If (top = = N-1)
{
4 Print “ Overflow’
}
5. Else
{
6. top = top +1
7. stack [top] = Item
}
8. End.
S. K. DEVAL, PGT(CS), KV2, JAMNAGAR
POP Operation

4
4

X 3
3

Z Pop once
2 Z
2

Y 1 Y
1

N 0 N
0
S. K. DEVAL, PGT(CS), KV2, JAMNAGAR
Algorithm for popping from stack array
/* Firstly , check for underflow condition N elements
1. If top == -1 then
{
2. Print “ Underflow’
3. Exit from program
}
4. Else
{
5. Print stack(top)
6. top = top -1
}
8. End. S. K. DEVAL, PGT(CS), KV2, JAMNAGAR
Programming Example (Pushing)

int Push (int stack[ ], int & top, int ele ) // function to pop element
{
if (top == size-1)
return (-1);
else
{
top++;
stack[ top] = ele;
}
return 0;
}
S. K. DEVAL, PGT(CS), KV2, JAMNAGAR
Programming Example (Poping)

int Pop (int stack[ ], int & top) // function to pop element
{
int ret;
if (top == -1)
return (-1);
else
{
ret=stack[ top] ;
top--;
}
return ret;
}
S. K. DEVAL, PGT(CS), KV2, JAMNAGAR
Application of Stacks

The stack are basically applied where LIFO (Last in first out)
scheme is required

 First Example: reversing a line


 Second Example: Polish String
Another application of stacks is in the conversion of
arithmetic expression in high level programming
language into machine readable form.

S. K. DEVAL, PGT(CS), KV2, JAMNAGAR


Polish string

As our computer system can only understand and work


on a binary language, it assumes that an arithmetic
operation can take place in two operands only e.g. A +
B, C + D, D/A etc.

But in our usual form an arithmetic expression may


consist of more than one operator and two operands e.
g. (A+ B) * C( D/(J+D)) . These complex arithmetic
operations can be converted into polish strings using
stack which can be executed in two operands and a
operator form
S. K. DEVAL, PGT(CS), KV2, JAMNAGAR
Polish string, named after a polish mathematician , jan
Lukasiewicz , refers to the notation in which the operator
symbol is placed either before its operand (prefix)
or after its operands (postfix)in contrast to usual form where
operator is placed in between the operands

Conversion of Infix expression to Postfix expression

There is a evaluation order according to which

I Brackets or Parenthesis
II Exponentiation
III Multiplication or Division
IV Addition or subtraction

Take place in the above specific order. The operator with the sam
( e.g, * and /) are evaluated from left to right)
S. K. DEVAL, PGT(CS), KV2, JAMNAGAR
Steps
 Determine the actual evaluation order by
inserting braces
 Convert the expression in the innermost
braces into postfix notation by putting the
operator after the operands
 Repeat step(ii) until entire expression is
converted into postfix notation

S. K. DEVAL, PGT(CS), KV2, JAMNAGAR


Expression in infix , prefix, postfix notation

Infix Notation Prefix Notation Postfix notation

A+B + AB AB +

(A-C) * B * - ACB AC- B*

A + (B*C) + A * BC ABC * +

(A+B)/(C-D)
/+AB-CD AB+CD-/
S. K. DEVAL, PGT(CS), KV2, JAMNAGAR
Example – Convert ( A + B) * C / D in to postfix notation .

Solution –

Step I - (( A + B) * C) / D

Step II - = ((AB+)* C) / D

= (AB+C*) / D

= A B+ C * D /

S. K. DEVAL, PGT(CS), KV2, JAMNAGAR


Evaluation of a postfix expression

 While reading the expression from left to right , push


the element in the stack if it is an operand.
 Pop the two operands from the stack , if the element
is an operator ( except NOT operator). Incase of
NOT operator , pop one operand from the stack and
then evaluate it.
 Push back the result of the evaluation. Repeat it till
the end of the expression

S. K. DEVAL, PGT(CS), KV2, JAMNAGAR


Evaluate the expression AB + C*D / using stack If A= 2 , B=3,C=4 and D=5

+
B A + B = 3+2 =5
A A

*
C
5
5 C * 5 = 5 * 4 = 20

/ 20/D = 20/4 = 5 Ans.


D
20 5 S. K. DEVAL, PGT(CS), KV2, JAMNAGAR
Assignment –

Evaluate the following expression I postfix form using stack and sho
content of the stack after execution of each operation-

true false true NOT false true OR NOT AND OR AND

S. K. DEVAL, PGT(CS), KV2, JAMNAGAR


S. K. DEVAL, PGT(CS), KV2, JAMNAGAR

You might also like