Friday, January 8, 2016

What is STACK in Java with Algorithm and Example

STACK:

Stack is a linear Data Structure. It represents group of elements stored in LIFO(Last In First Out) Order.This means the element which is stored as  last element into the stack will be the first element to be removed from the stack. In this insertion & deletion of an element can be done at the same end (only one end).Stack is a static (or) dynamic Data Structure. It is very useful to solve the arithmetic expressions,memory management operation.

Also Read :  Introduction to Data Structure in java


  • In Stack recently entered  element is treated as a TOP element.
  • If you want to  insert an element into the Stack is called as PUSH Operation
  • If you want to Delete an element from the Stack is called as POP operation
  • Visit or Display the element in the Stack is called Traverse Operation.
  • If we push more elements than the Stack size then it is called as Stack is Overflow
  • If we want to delete the element from the empty Stack then it is called as Stack is Underflow

Stack can be represented in two ways: They are

a)Linear Stack
b)Linked Stack

a) Linear Stack:

The Stack that is implemented  by using Arrays is called "Linear Stack" The representation of Linear Stack is as follows.

















In the above representation Stack contains four elements like A,B,C,D. In that 'D' is a top element because it it a recently entered element.
The fallowing are the operation on the linear stack. They are

1)Push
2)Pop
3)Traverse

1.Push:

In Stack, 'Push' is nothing but Insertion. That means inserting an element in the Stack is called Push Operation.In this first we check the condition either Stack is full or not,if the Stack is full then it display the message as "Stack is Overflow". It the Stack is not full we can insert an element into the Stack by Increasing the TOP position and assigning that number at TOP Position. 

Algorithm:
Push(num)

Step 1: Start
Step 2: if(top=size-1) then Print "Stack is Overflow"
Step 3: else
             set top=top+1
             set Stack[top]=num
Step 4: End


2)Pop:

In Stack 'Pop'  is nothing but "Deletion". That means deletion of an element from the stack is called 'Pop' operation.In this first we check the condition either "Stack is empty or not".
It it is empty then display a message as "Stack is empty" or Stack is Under flow". If it is not empty then delete the element in the TOP position and decrease Top Position.

Algorithm: pop(num)

Step 1: Start
Step 2: if(top==-1)then print "Stack is empty"
Step 3:else
             delete stack[top]
              set top=top-1
Step 4: End

3.Traverse:

Display each and every element from the stack is called "traverse" Operation. In this we display the elements from the first element(top) to the last element.

Algorithm:

Step 1: Start
Step 2: set i=top
Step 3:Repeat the steps 4&5 until while(i>=0)
Step 4: print Stack[i]
Step 5:i=i-1
Step 6:End

b) Linked Stack:

The Stack that is implemented by using "Links or Pointers" is called "Linked Stack". The representation of Linked Stack is as fallows.























In the above stack contains four Nodes like 1,2,3,4. On that the fourth node is the top node,because it is recently entered element. 
The fallowing are the operations on the linear Stack. They are

1. Push
2. Pop
3. Traverse

1. Push:

In Stack, 'Push' is nothing but Insertion. That means inserting an element in the Stack is called Push Operation. In this first we check the condition either "Stack is empty or not". If stack is empty then insert a new node and assign the NULL pointer to the Link. Otherwise insert a new node and assign the next node address to the Link.

Algorithm:Push(num)

Step 1: Start
Step 2: if(top==NULL) then
             t=new Node
              t----->data=num
              t------>link=Null
             Set top=t

Step 3: else
             t=new node
             t----->data=num
             t----->link=top
             set top=t
Step 4:  End

2. Pop:

In Stack "POP" is nothing but 'Deletion'. That means deletion of an element from the Stack is called Pop Operation. In this first we check the condition either "Stack is empty or not". If Stack is empty then display the message as 'stack is empty or Stack is undrflow'. If it is not empty then delete the Top node and assign next node address to the Top.

Algorithm: pop(num)

Step 1: Start
Step 2:if(top==Null)then print "Stack is empty"
Step 3: else
              Set t=top
              top=top---->link
              delete t
Step 4: End


3. Traverse:

Display each and every element from the stack is called "Traverse" Operation. In this we display the elements from the first element(top)to the last element.

Algorithm: Traverse(num)

Step 1: Start
Step 2: set t=top
Step 3: repeat the steps 4&5 until while(t!=Null)
Step 4: print t---->data
Step 5: t=t----->link
Step 6: End

Example:
To perform different operations on a stack through a menu.

import java.io.*;
import java.util.*;
class StackDemo{
public static void main(String args[]) throws Exception
{
//create an empty stack to contain integer objects
Stack<Integer> st=new Stack<Integer>();
//take vars
int choice=0;
int position,element;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
//display the meau as long as user choice<4
while(choice<4)
{
System.out.println("Stack Operations");
System.out.println("1.push");
System.out.println("2.pop");
System.out.println("3.search");
System.out.println("4.exit");

choice=Integer.parseInt(br.readLine());
//perform a task depending on user choice

switch(choice){
case 1: 
System.out.printlnk("enter element:");
element=Integer.parseInt(br.readLine());
//int type element converted into Integer object and then pushed into the stack
st.push(element);
break;
case 2: 
// the top-most Integer Object is popped
Integer obj=st.pop();
System.out.println("popped object:"+obj);
break;
case 3: 
System.out.println("which element?");
element=Integer.parseInt(br.readLine());

 //int type element converted into Integer object and then pushed into the stack
position=st.search(element);
if(position==-1)
System.out.println("element not found");
else
System.out.println("position"+position);
 break;
default:
//come out if user choice is other than 1,2,3
return;
}
System.out.println("stack contents"+st);
}
}
}
              

No comments:

Post a Comment

High Paying Jobs after Learning Python

Everyone knows Python is one of the most demand Programming Language. It is a computer programming language to build web applications and sc...