Sunteți pe pagina 1din 13

Introduction to STACK

 A stack is an Abstract Data Type (ADT), commonly used in most programming


languages. It is named stack as it behaves like a real-world stack, for example – a deck
of cards or a pile of plates, etc.

Stack Example
 A real-world stack allows operations at one end only. For example, we can place or
remove a card or plate from the top of the stack only. Likewise, Stack ADT allows all
data operations at one end only. At any given time, we can only access the top
element of a stack.

 This feature makes it LIFO data structure. LIFO stands for Last-in-first-out. Here, the
element which is placed (inserted or added) last, is accessed first. In stack terminology,
insertion operation is called PUSH operation and removal operation is called POP
operation.
OPERATION ON STACK
• Basic
  operations required to manipulate a stack are:
PUSH : To insert an item into the stack
POP : To remove an item from a stack.
PEEP : To find element from stack.
• Initially top of the stack is zero as there is no
element in stack, top is incremented by one as
element is added to stack i.e. PUSH Operation and
top is decremented by one when element is deleted
from stack i.e. POP operation.
 PUSH Operation

• The process of putting a new data element onto stack is known as a Push Operation.

Algorithm for PUSH Operation:

Procedure: PUSH(S,TOP,X)
• This procedure inserts an element x to the top of a stack which is represented by a vector S
containing N elements with a pointer TOP denoting the top element in the stack.
1. [Check for stack overflow]
If TOP>=N
Then write (“ STACK OVERFLOW”)
Return
2. [ Increament TOP ]
TOP  TOP +1
3. [ Insert element ]
S[TOP]  X
4. [ Finished ]
Return
 Pop Operation
• Accessing the content while removing it from the stack, is known as a Pop Operation.
• In an array implementation of pop() operation, the data element is not actually
removed, instead top is decremented to a lower position in the stack to point to the
next value. But in linked-list implementation, pop() actually removes data element
and deallocates memory space.
Algorithm for POP operation:
Function: POP(S,TOP)
• This function removes the TOP element from a stack which is represented by a
vector S and returns this element. TOP is a pointer to the top element of the stack.
1. [Check for underflow of stack]
If TOP=0
Then write (“ STACK UNDERFLOW ON POP”)
Return
2. [ Decrement Pointer ]
TOP  TOP - 1
3. [ Return former top element of stack ]
Return (S[TOP+1]
 PEEP Operation

• PEEP : Find the element from the top of the stack.


•  
• Algorithm for POP operation:
• This function returns the value of element from the TOP of the stack which
is represented by a vector S containing N elements . The element is not
deleted by this function.
Function : PEEP( S, TOP , I)
1. [Check for stack underflow ]
If TOP-I+1 <=0
Then Write (“ STACK UNDERFLOW ON PEEP “)
2. [ Return element from top of the stack ]
S[ TOP – I+1 ]  X
3. [ Finished]
Return
Stack Representation

• A stack can be implemented by means of Array, Structure,


Pointer, and Linked List. Stack can either be a fixed size one or
it may have a sense of dynamic resizing.
• There are two ways to implement a stack:
1. Using array
2. linked list
 Array Implementation
• Here, we are going to implement stack using arrays, which makes it a fixed size stack
implementation.

#include<stdio.h>
#define size 5
int top =-1;
int st[size];
void push(int x)
{
top++;
st[top]=x;
}
int pop()
{
int x=st[top];
top--;
return x;
}
int peep()
{
int x=st[top];
return x;
}
int print()
{
int i;
for(i=top;i>=0;i--)
{
printf("\n|%d|",st[i]);
if(i==top)
{
printf("<--top");
}
}
}
int main()
{
int ch,x,st[size];
do
{
printf("\n\nEnter your CHOICE: ");
printf("\n1.PUSH");
printf("\n2.POP");
printf("\n3.PEEP");
printf("\n4.EXIT\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
if(top==size-1)
{
printf("\nTHE STACK IS FULL\n ");
}
else
{
printf("Enter the value:");
scanf("%d",&x);
push(x);
print();
}
break;
case 2:
if(top==-1)
{
printf("\nTHE STACK IS EMPTY\n");
}
else
{
x=pop();
print();
}
break;
case 3:
if(top==-1)
{
printf("\nTHE STACK IS EMPTY\n");
}
else
{
x= peep();
print();
}
break;
case 4:
break;
default:
printf("INVALID");
}
}while(ch!=4);
}
Application of Stack
• Three applications of stacks are presented here.  These
examples are central to many activities that a computer must
do and deserve time spent with them.

1. Expression evaluation
2. Backtracking (game playing, finding paths, exhaustive
searching)
3. Memory management, run-time environment for nested
language features.
1. Expression Evaluation :
In general there are 3 kinds of expressions available depending on the placement of the
operators & operands.
1. Infix expression :- It is the general notation used for representing expressions.
“In this expression the operator is fixed in between the operands”
Ex: a + b
2. Post fix expression :- (Reverse polish notation)
“In this expression the operator is placed after the operands”.
Ex : ab+
3. Prefix expression :- (Polish notation)
“In this expression the operators are followed by operands i.e the operators are fixed before
the operands”
Ex : +ab
All the infix expression will be converted into post fix notation with the help of stack in any
program . The stack will be useful in evaluating the postfix expressions also.

Infix Prefix Postfix


a+b +ab ab+
a+b*c +a*bc abc*+
(a + b) * (c - d) *+ab-cd ab+cd-*
b*b-4*a*c    
40 - 3 * 5 + 1    

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