0% found this document useful (0 votes)
27 views

Stack ADT

DATA STRUCTURES

Uploaded by

KALPANA DEVI M
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

Stack ADT

DATA STRUCTURES

Uploaded by

KALPANA DEVI M
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 59

STACK ADT

Stack Model
• A stack is a linear data structure which follows Last In
First Out (LIFO) principle, in which both insertion and
deletion occur at only one end of the list called the Top.
Example
• Pile of coins
• Stack of trays
Operations on Stack
• The fundamental operations performed on a stack are
1. Push
2. Pop
3. Stack Empty
4. Stack Full
PUSH :

• The process of inserting a new element to the top of the


stack. For every push operation the top is incremented by
1.
POP
• The process of deleting an element from the top of stack
is called pop operation. After every pop operation the top
pointer is decremented by 1.
Exceptional Condition
• OverFlow
Attempt to insert an element when the stack is full is
said to be overflow.
• UnderFlow
Attempt to delete an element, when the stack is empty
is said to be underflow.
Implementation of Stack
• There are two types of implementation
• Array Implementation of Stack
• Linked List Implementation of stack
Array Implementation of Stack

• In this implementation each stack is associated with a pop


pointer, which is -1 for an empty stack.
• To push an element X onto the stack, Top Pointer is
incremented and then set Stack [Top] = X.
• To pop an element, the stack [Top] value is returned
and the top pointer is decremented.
• pop on an empty stack or push on a full stack will
exceed the array bounds.
Routine to push an element onto a stack
void push (int x, Stack S)
{
if (IsFull (S))
Error ("Full Stack");
else
{
Top = Top + 1;
S[Top] = X;
}
}
int IsFull (Stack S)
{
if (Top = = Arraysize)

return (1);
}
Routine to POP an element from the stack
void pop (Stack S)
{
if (IsEmpty (S))
Error ("Empty Stack");
else
{
X = S [Top];
Top = Top - 1;
}
}
int IsEmpty (Stack S)
{
if (S Top = = -1)
return (1);
}
Routine to return top element of the stack
int TopElement (Stack S)
{
if (! IsEmpty (s))
return S[Top];
else
Error ("Empty Stack");
return 0;
}
LINKED LIST IMPLEMENTATION OF STACK

• Push operation is performed by inserting an element at


the front of the list.
• Pop operation is performed by deleting at the front of the
list.
• Top operation returns the element at the front of the list.
DECLARATION FOR LINKED LIST IMPLEMENTATION

Struct Node;
typedef Struct Node *Stack;
int IsEmpty (Stack S);
Stack CreateStack (void);
void MakeEmpty (Stack S);
void push (int X, Stack S);
int Top (Stack S);
void pop (Stack S);
Struct Node
{
int Element ;
Struct Node *Next;
};
ROUTINE TO CHECK WHETHER THE STACK IS EMPTY

int IsEmpty (Stack S)


{
if (S→Next = = NULL)
return (1);
}
ROUTINE TO CREATE AN EMPTY STACK

Stack CreateStack ( )
{
Stack S;
S = malloc (Sizeof (Struct Node));
if (S = = NULL)
Error (" Out of Space");
MakeEmpty (s);
return S;
}
void MakeEmpty (Stack S)
{
if (S = = NULL)
Error (" Create Stack First");
else
while (! IsEmpty (s))
pop (s);
}
ROUTINE TO PUSH AN ELEMENT ONTO A STACK

void push (int X, Stack S)


{
Struct Node * Tempcell;
Tempcell = malloc(sizeof(Struct Node));
If (Tempcell = = NULL)
Error ("Out of Space");
else
{
Tempcell-> Element = X;
Tempcell->Next = S->Next;
S→Next = Tempcell;
}
}
ROUTINE TO RETURN TOP ELEMENT IN A STACK

int Top (Stack S)


{
If (! IsEmpty (s))
return S→Next→Element;
Error ("Empty Stack");
return 0;
}
ROUTINE TO POP FROM A STACK

void pop (Stack S)


{
Struct Node *Tempcell;
If (IsEmpty (S))
Error ("Empty Stack");
else
{
Tempcell = S→Next;
S→Next = Tempcell→Next;
Free (Tempcell);
}
}
APPLICATIONS OF STACK

• Some of the applications of stack are :


• (i) Evaluating arithmetic expression
• (ii) Balancing the symbols
• (iii) Towers of Hanoi
• (iv) Function Calls.
• (v) 8 Queen Problem.
Different Types of Notations To Represent
Arithmetic Expression
• There are 3 different ways of representing the algebraic
expression.
• They are
• * INFIX NOTATION
• * POSTFIX NOTATION
• * PREFIX NOTATION
INFIX

• In Infix notation, The arithmetic operator appears between


the two operands to which it is being applied.

For example : - A / B + C
POSTFIX

• The arithmetic operator appears directly after the two


operands to which it applies. Also called reverse polish
notation. ((A/B) + C)
• For example :

AB / C +
PREFIX
• The arithmetic operator is placed before the two operands
to which it applies. Also called as polish notation. ((A/B) +
C)
• For example : -

+/ABC
INFIX PREFIX (or) POLISH POSTFIX (or) REVERSE POLISH

INFIX PREFIX POSTFIX

(A + B) / (C - D) /+AB - CD AB + CD - /

. A + B*(C - D) +A*B - CD ABCD - * +

X*A/B-D - / * XABD X A*B/D-

X + Y * (A - B) / (C - D) +X/*Y - AB - CD XYAB - *CD - / +

A * B/C + D + / * ABCD AB * C / D +
Evaluating postfix Expression
• Postfix expression is without parenthesis and can be
evaluated as two operands and an operator at a time.
• This becomes easier for the compiler and the computer to
handle.
Evaluation rule of a Postfix Expression states:
• 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 and then evaluate it.
• Push back the result of the evaluation. Repeat it till the
end of the expression.
Evaluating Arithmetic Expression
• To evaluate an arithmetic expressions, first convert the
given infix expression to postfix expression and then
evaluate the postfix expression using stack.
Infix to Postfix Conversion

• Read the infix expression one character at a time until it


encounters the delimiter. "#"
Step 1 : If the character is an operand, place it on to the
output.
Step 2 : If the character is an operator, push it onto the stack.
If the stack operator has a higher or equal priority than input
operator then pop that operator from the stack and place it
onto the output.
Step 3 : If the character is a left parenthesis, push it onto the
stack.
Step 4 : If the character is a right parenthesis, pop all the
operators from the stack till it encounters left parenthesis,
discard both the parenthesis in the output.
Infix to postfix conversion
Solution

You might also like