Sunteți pe pagina 1din 18

DATA STRUCTURE : ‘STACKS’.

PPO Presentation.
Objectives:

To Understand and apply the knowledge


of the Data Structure ‘Stack’ in the
Application Programs .
Introduction To Stack.
 A stack is a linear data structure in which addition or deletion
of element take place at one end only.

 Stack is a special type of data structure where insertion is done


from one end called 'top' of stack and deletion will be done
from the same end. Here, the last element inserted will be on
the top of the stack. Since deletion is done from the same end,
last element inserted is the first element to be deleted.

 So, stack is also called Last In First Out(LIFO)data structure.


Definition of Stack :

 A stack is an ordered collection of items where


the addition of new items and the removal of
existing items always takes place at the same
end. This end is commonly referred to as the
"TOP". The end opposite the top is known as the
"BASE".
Basic Concept of Stack

 A stack is a data structure that is used to store


elements but all the elements can be inserted or
deleted from the TOP of the stack.

 The top of the stack is the top most part of the stack,
something like a rectangular box which has only
opening at the top.
Simple representation of a
Stack.

 The shown figure is a simple


representation of a stack. As we can
notice that the TOP of the stack is the
only means by which we can insert or
delete elements to the stack.

* All the elements in the stack follow the simple rule of Last In First
Out (LIFO). This means that the elements inserted in the last are the
elements to be deleted first.
Terminology:

 1. Max size: This term is used to refer the maximum size of


the stack. This is not a standard term.

 2. Stack empty or underflow : If stack has no elements and


pop operation is performed or an element is deleted from
empty stack then such condition is called as "Underflow".

 3. Overflow or stack full: If stack is full i.e. there is no more


space for new item and push operation is done then such
condition is called as '‘Overflow.''
Primitive operations on
stacks
 createstack( ) :To initializes as an empty stack.

 Insertion / (PUSH) :The process of adding new elements to


the top of stack is called push operation. Pushing of an
element is nothing but adding element to the stack.

 (iii) Deletion / (POP) :The process of deleting an element


from top of a stack is called POP.
TOP : A pointer, which keeps track of the top element in the Stack.
If an array of size N is declared to be a stack, then TOP will be -1
when the stack is empty and is N when the stack is full.

isEmpty( ) : This Function is used to check whether a stack is


empty or not. This function returns Boolean value TRUE if stack is
empty .

isFull( ) : This Function is used to check weather a stack becomes


full or not. It returns Boolean TRUE if stack is full else FALSE.
Understanding Push and
Pop Operation .
Types of Notations:

 Infix expressions
 An operator appears between its operands
 Example: a + b
 Prefix expressions
 An operator appears before its operands
 Example: + a b
 Postfix expressions
 An operator appears after its operands
 Example: a b +
Converting an Infix to
Prefix Expression.

 To convert a fully parenthesized infix expression to a prefix


form
 Move each operator to the position marked by its corresponding
open parenthesis
 Remove the parentheses
 Example
 Infix expression: ( ( a + b ) * c )
 Prefix expression: * + a b c
Converting an Infix to
Postfix Expression.

 To convert a fully parenthesized infix expression to a


postfix form
 Move each operator to the position marked by its
corresponding closing parenthesis
 Remove the parentheses
 Example
 Infix form: ( ( a + b ) * c )
 Postfix form: a b + c *
PROGRAMING
Program for static Implementation of
Stack using Array.
void push()
{
if(top==MAX)
printf("\n\n*** STACK OVERFLOW ***\n\n");
else
{
printf("\n\n Enter the item to be pushed into the stack:\n\n");
scanf("%d",&item);
top++;
stack[top]=item;
}
}
void pop()
{
if(top==-1)
printf("\n\n\n*** STACK IS EMPTY !!! ***\n\n");
else
{
item=stack[top];
top--;
printf("\n\n\n The deleted item from stack is %d\n\n",item);
}
}
Summary
• A stack is a container of objects that are inserted and removed
according to the Last-In-First-Out (LIFO) principle .

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


inserted or deleted at one end which is known as TOP of the
stack .

• PUSH operation adds an item to the top of the stack while


POP removes the item from the top .

• The process of writing the operators of an expression either


before their operands or after them is called Polish notation .
Thank you !!!

S-ar putea să vă placă și