Saturday, March 2, 2013
Today I am going to show how you can create your own stack using linked list. Hope that you already know how to create a stack. But that stack can take input of only one type of objects. For different types you have to write seperate programs. But this can be avoided if we use java.lang.Object as data type for stack elements. But if you use this then a problem will arise. When you are pushing into stack then you can push any object in this case. But while popping you won't be able to understand the data type of the elements and so won't be able to typecast and use it properly and hence not type safe. So a new feature GENERICS was introduced to make it type safe. Using generics you can only insert one type of object , only you have to mention about data type during creating stack object and thus only need to create seperate stack objects and not programs. The following code will explain better.
--------------------------------------------------------------------------------------------------------------------------
Java Source Code
--------------------------------------------------------------------------------------------------------------------------
/* The STACK class */
public class Stack<T> {
    //T is the type parameter
    Node top; //topmost element of stack

    //defines each node of stack
    class Node{
       T value; //value of each node
       Node next;  //pointer to next node
 
       public Node(T value){
         this.value=value;  //initializing
         next=null;
       }
    }
    //This function pushes new element
    public void push(T value){
     Node current=new Node(value);
     if(isEmpty())
        top=current; //if empty stack
     else{
        current.next=top;
        top=current;
     }
    }
    //This function pops topmost element
    public T pop(){
     T value=null;
     if(!isEmpty()){
         top=top.next;
         value=top.value;
     }
     return value;  //returning popped value
    }
    //This function returns the entire stack
    public String toString(){
     Node current=top;
        StringBuilder s=new StringBuilder();
     System.out.println("\n---------------STACK--------------");
     
     while(current!=null){
       s.append(current.value+"  ");
       current=current.next;
     }

        return s.toString();
    }
    //This function returns topmost element
    public T peek(){
     return top.value;
    }
    //This function checks emptiness of stack
    public boolean isEmpty(){
     return (top==null)?true:false;
    }    
}


/* The main class testing stack */
public class Test{
    public static void main(String[] args) {
        //creating and using stack of integers
        Stack<Integer> s=new Stack<Integer>();
        s.push(15);  s.push(10);  s.push(12);
        System.out.println(s);
        s.pop();  s.pop();
        System.out.println(s);

        //creating and using stack of floats
        Stack<Float> s2=new Stack<Float>();
        s2.push(5.5f);  s2.push(1.2f);  s2.push(8.6f);
        System.out.println(s2);
        s2.pop();  s2.pop();
        System.out.println(s2);
    }
}

NOTE : The type parameter while creating stack object of primitive data types, you should mention object type i.e. Integer and not int,Double and not double.
--------------------------------------------------------------------------------------------------------------------------
Output
--------------------------------------------------------------------------------------------------------------------------

---------------STACK--------------
12  10  15

---------------STACK--------------
15

---------------STACK--------------
8.6  1.2  5.5

---------------STACK--------------
5.5

--------------------------------------------------------------------------------------------------------------------------
Download Links
--------------------------------------------------------------------------------------------------------------------------

0 comments:

Post a Comment

Total Pageviews

Followers


Labels

Popular Posts

free counters