STACK
1
A Stack is a linear data structure in which items may be inserted or removed
only at one end called the top of the stack . Stacks are also called LIFO (Last in First
Out) or FILO (First In Last Out)lists.
Operation related to stack are push & pop.
Push is an operation used to insert an element at the top.
Pop is an operation used to delete an element from the top.
2
Implementation of stack
A Stack is generally implemented with two basic operation –push or pop .
Push means to insert an item on to stack .
Algorithm for push operation is:
Step 1: First check for stack overflow
if top>=MAXSIZE
print “ Stack overflow” and exit
Step 2: Increment the pointer value by one
top=top+1
Step 3:Insert the item
arr[top]=value
Step 4: Exit
Here, top is a pointer which denotes the position of top most item in the
stack .Stack is represented by the array arr and MAXSIZE represents the
maximum possible number of elements in the stack.
3
The pop operation removes the topmost item from the stack . After removal
of topmost value top is decremented by 1
Algorithm to pop an element from the stack
Step 1:First check wheather the stack is empty or not
if top=0
print “ stack underflow” and exit
Step 2:Remove the top most item
value =arr[top]
top=top-1
Step 3:Return the item of the stack
return (value)
4
Implementation of stack using array
Void push()
{
if(top<=size)
{
cout<<“Enter the element to be pushed to stack” ;
cin>>var;
size[top]=element;
top++;
}
else
{
printf(“Stack is full”);
}
return;
}
5
Void pop()
{
if (top>0)
{
top--;
var =size[top];
cout <<“popped element :”<<var;
}
else
{
cout<<“stack is empty”;
}
return;
}
push and pop will perform the operation of pushing the element to the stack and
popping the element from the stack respectively.
6

Stack PPT.pptx

  • 1.
  • 2.
    A Stack isa linear data structure in which items may be inserted or removed only at one end called the top of the stack . Stacks are also called LIFO (Last in First Out) or FILO (First In Last Out)lists. Operation related to stack are push & pop. Push is an operation used to insert an element at the top. Pop is an operation used to delete an element from the top. 2
  • 3.
    Implementation of stack AStack is generally implemented with two basic operation –push or pop . Push means to insert an item on to stack . Algorithm for push operation is: Step 1: First check for stack overflow if top>=MAXSIZE print “ Stack overflow” and exit Step 2: Increment the pointer value by one top=top+1 Step 3:Insert the item arr[top]=value Step 4: Exit Here, top is a pointer which denotes the position of top most item in the stack .Stack is represented by the array arr and MAXSIZE represents the maximum possible number of elements in the stack. 3
  • 4.
    The pop operationremoves the topmost item from the stack . After removal of topmost value top is decremented by 1 Algorithm to pop an element from the stack Step 1:First check wheather the stack is empty or not if top=0 print “ stack underflow” and exit Step 2:Remove the top most item value =arr[top] top=top-1 Step 3:Return the item of the stack return (value) 4
  • 5.
    Implementation of stackusing array Void push() { if(top<=size) { cout<<“Enter the element to be pushed to stack” ; cin>>var; size[top]=element; top++; } else { printf(“Stack is full”); } return; } 5
  • 6.
    Void pop() { if (top>0) { top--; var=size[top]; cout <<“popped element :”<<var; } else { cout<<“stack is empty”; } return; } push and pop will perform the operation of pushing the element to the stack and popping the element from the stack respectively. 6