Saturday, 9 July 2011

Queue Implementation using Linked List in java

Here I am creating 3 files in order to implement the Queue using Linked list in java

1.QueueADT.java                   2. LLQueue.java              3. LLQueue_Test.java

//  QueueADT.java

package Queue;
public interface QueueADT<E>
{
    public Object getFront();
    public void enqueue(Object a);
    public Object dequeue();
    public boolean isEmpty();
   
}

// 2. LLQueu.java

import java.util.Scanner;
import Queue.*;
import java.util.*;
public class LLQueue<E> implements QueueADT<E>
{
    public ListNode front;
    public ListNode back;
   
    /*public LLQueue()
    {
        front=back=null;
    }*/
    // Test if the queue is logically empty, return true if empty else false
    public boolean isEmpty()
    {

        return front==null;
    }

    // Insert a new item in to queue;
    public void enqueue(Object x)
    {
        ListNode n=new ListNode(x);
        if(isEmpty())
            back=front=n;
        else
            back=back.next=n;
    }

    //Retun and remove the lest recently inserted item from the queue
   
    public Object dequeue()
    {
        if(isEmpty())
            throw new UnderflowException("Queue is empty");
        Object val=front.element;
        front=front.next;
        return val;
    }
   
    // get the least recently inserted element from the queue;

    public Object getFront()
    {
        if(isEmpty())
            throw new UnderflowException("Queue is empty");
        else
       
        return front.element;
    }
    // Make queue empty

    public void makeEmpty()
    {
        back=front=null;
    }
   
    // exit program
    public void exit()
    {
        System.exit(0);
    }
   
    // display all the itmes in the queue
    public void display()
    {
        if(!isEmpty())
        {

            ListNode current=front;
            while (current!=null)
            {
                System.out.println(current.element);
                current=current.next;
            }
        }
        System.out.println("\nQueue is empty\n");
    }
   
   
};

//Exception class for access in empty containers
 //* such as stacks, queues, and priority queues.
class UnderflowException extends RuntimeException
    {
        
        // Construct this exception object
        // Messages the error message
        public UnderflowException( String message )
        {
            super( message );
        }
    }
// class List Node
class ListNode
{
    public Object element;
    public ListNode next;

    // constructors
    public ListNode(Object Elem)
    {
        element=Elem;
    }

    public ListNode(ListNode n, Object Ele)
    {
        element=Ele;
        next=n;
    }

};
 
// LLQueue_Test.java

import java.util.*;
class Queue_Test
{
    public static void main(String[] args)
    {
        int choice,el;
     Scanner sc=new Scanner(System.in);
     LLQueue LLQ=new LLQueue();
        //size=sc.nextInt();
       
        while (true)
        {
            System.out.println("Please enter you choice\n");
            System.out.println(" 1 for insert or enqueue \n 2 for is Queue empty \n 3 to get front element \n 4 to dequeue the element");
            System.out.println(" 5 to display \n 6 to make Queue empty\n 7 to exit  ");
            System.out.print("\n Choice --> ");
            choice=sc.nextInt();
            switch (choice)
            {
            case 1:
                System.out.print("Please enter Enter your element --> ");
                el=sc.nextInt();
                LLQ.enqueue(el);
                System.out.println();
                break;
            case 2:
                System.out.println(LLQ.isEmpty());
            break;
            case 3:
                if(!LLQ.isEmpty())
                {

                    System.out.println("Front element is ");
                    System.out.println(LLQ.getFront());
                }
                else
                    System.out.println("\nArray stack is empty\n");
                break;
            case 4:
                System.out.println("\nPoped element is ");
                System.out.print(LLQ.dequeue());
                System.out.println();
                break;
            case 5:
                LLQ.display();
                break;
            case 6:
                LLQ.makeEmpty();
            break;
            case 7:
                LLQ.exit();
            default:
                System.out.println("Please choose correct choice");
               
           
            }
        }
    }
}

Stack implementation using Singly linked list

Here I am creating 3 files 1. StackADT.java          2. SLStack.java           3. SLStack_Test.java

// Save the following StackADT inter face code with the above mentioned name

public interface StackADT
{
    public int size();
    public boolean isEmpty();
    public Object top();
    public void push(Object element);
    public Object pop();


}

//  2. SLStack.java

import java.util.*;
class Node
{
    public Object element;
    public Node next;
    public Node()
    {
        this(null,null);
    }
    public Node(Object element, Node next)
    {
        this.element=element;
        this.next=next;
    }
   
    public Object getElement()
    {
        return element;
    }
    public Node getNode()
    {
        return next;
    }
    public void seElement(Object obj)
    {
        this.element=obj;
    }
    public void setNode(Node node)
    {
        this.next=node;
    }

}
public class SLStack implements StackADT
{
    private Node top;
    private int size;
    public boolean isEmpty()
    {
        return (size==0);
    }
    public void makeEmpty()
    {
        top=null;
        size=0;
    }
    public Object pop()
    {
        if (top==null)
        {
            return null;
        }
        Object val=top.getElement();
        top=top.getNode();
        size--;
        return val;
    }
    public void push(Object obj)
    {
        Node v=new Node(obj,top);
        size++;
        top=v;
    }
    public int size()
    {
        return size;
    }
    public Object top()
    {
        Node temp=top;
        if(!isEmpty())
       

        try
        {
            return temp.getElement();
        }
        catch (Exception e)
        {
            return e.getMessage();
        }
        else
            return null;

       
        //return null;
    }
    public void display()
    {
        Node current=top;
        while (current!=null)
        {
            System.out.println(current.getElement());
            current=current.next;
        }
       
    }
    public void exit()
    {
        System.exit(0);
    }

};

//  3. SLStack_Test.java

import java.util.*;
public class SLStack_Test
{
    public static void main(String[] args)
    {
     int choice,el;
     Scanner sc=new Scanner(System.in);
     SLStack SLS=new SLStack();
        //size=sc.nextInt();
       
        while (true)
        {
            System.out.println("Please enter you choice\n");
            System.out.println(" 1 for insert or push \n 2 for is Stack empty \n 3 to get top element \n 4 to pop the lement");
            System.out.println(" 5 to display \n 6 to exit");
            choice=sc.nextInt();
            switch (choice)
            {
            case 1:
                System.out.print("Please enter Enter your element --> ");
                el=sc.nextInt();
                SLS.push(el);
                System.out.println();
                break;
            case 2:
                System.out.println(SLS.isEmpty());
            break;
            case 3:
                if(!SLS.isEmpty())
                {

                    System.out.println("top element is ");
                    System.out.println(SLS.top());
                }
                else
                    System.out.println("\nArray stack is empty\n");
                break;
            case 4:
                System.out.println("\nPoped element is ");
                System.out.print(SLS.pop());
                System.out.println();
                break;
            case 5:
                SLS.display();
                break;
            case 6:
                SLS.exit();
            default:
                System.out.println("Please choose correct choice");
               
           
            }
        }

    }
}

Stack Array in java

Here I am implementing Stack Array in java with StackADT, I have created 3 files
 1. StackADT.java               2. AStack.java                     3. AStack_Test.java

// StackADT.java 

public interface StackADT
{
    public int size();
    public boolean isEmpty();
    public Object top();
    public void push(Object element);
    public Object pop();


}

// 2. AStack.java


public class AStack implements StackADT
{
    Object Array[];
    int top=-1;

    AStack(int size)
    {
     Array=new Object[size];
    }
   
    public int size()
    {
        return top;
    }

    public boolean isEmpty()
    {
        if (top<0)
        {
            return true;
        }
        else
            return false;
    }

    public Object top()
    {
        return Array[top];
    }

    public void push(Object element)
    {
        try
        {
                Array[++top]=element;
        }
        catch (Exception e)
        {
            System.out.println(e.getMessage());
        }
   

    }
    public Object pop()
    {
        try
        {
            Object result=Array[top];
        top=top-1;
        return result;
        }
        catch (ArrayIndexOutOfBoundsException e)
        {
            return e.getMessage();
        }
       
    }
    public void exit()
    {
        System.exit(0);
    }

   
}

// 3.  AStack_Test.java

import java.util.Scanner;
class AStack_Test
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        int choice,size,el;
        System.out.println("Please enter size of the Array stack");
        size=sc.nextInt();
        AStack AS=new AStack(size);
        while (true)
        {
            System.out.println("Please enter you choice");
            System.out.println(" 1 for insert or push \n 2 for is Stack empty \n 3 to get top element \n 4 to pop the lement");
            System.out.println(" 5 to exit");
            choice=sc.nextInt();
            switch (choice)
            {
            case 1:
                System.out.println("Please enter Enter your element");
                el=sc.nextInt();
                AS.push(el);
                break;
            case 2:
                System.out.println(AS.isEmpty());
            break;
            case 3:
                if(!AS.isEmpty())
                {

                    System.out.println("top element is ");
                    System.out.println(AS.top());
                }
                else
                    System.out.println("\nArray stack is empty\n");
                break;
            case 4:
                System.out.println("\nPoped element is\n ");
                System.out.println(AS.pop());
                break;
            case 5:
                AS.exit();
            default:
                System.out.println("Please choose correct choice");
               
           
            }
        }
    }
}

Java Collection Stack implementation

Here I am implementing Java collection stack, here I am creating three files
1 is StackADT.java. 2 JCStack.java. 3 JCStack_Test.java

// save it as StackADT
public interface StackADT
{
public int size();
public boolean isEmpty();
public Object top();
public void push(Object element);
public Object pop();


}

// save it as JCStack.java
import java.util.Scanner;
import java.util.Stack;
import java.util.EmptyStackException;
public class JCStack implements StackADT
{
Stack st=new Stack();
int top=0;
public int size()
{
return st.size();
}
public boolean isEmpty()
{
return st.isEmpty();
}
public Object top()
{
return st.peek();
}
public void push(Object element)
{
st.push(element);
top++;
}
public Object pop()
{
try
{
return st.pop();
}
catch (EmptyStackException e )
{
return e.getMessage();
}

}
public void exit()
{
System.exit(0);
}

}

// Save it as JCStack_Test.java
import java.util.Scanner;
class JCStack_Test
{
public static void main(String[] args)
{
JCStack ST=new JCStack();
Scanner sc=new Scanner(System.in);
int choice,el;
while (true)
{
System.out.println("Please enter you choice");
System.out.println(" 1 for insert or push \n 2 for is Stack empty \n 3 to get top element \n 4 to pop the lement");
System.out.println(" 5 to exit");
choice=sc.nextInt();
switch (choice)
{
case 1:
System.out.println("Please enter Enter your element");
el=sc.nextInt();
ST.push(el);
break;
case 2:
System.out.println(ST.isEmpty());
break;
case 3:
System.out.println("top element is ");
System.out.println(ST.top());
break;
case 4:
System.out.println("Poped element is ");
System.out.println(ST.pop());
break;
case 5:
ST.exit();
default:
System.out.println("Plese choose correct choice");


}
}
}
}

Singly linked List

import java.util.*;
class Elem
{
public Elem next;
public Object data;
};
class Link
{
static Elem front=null;
static Elem back=null;
public static void main(String[] args)
{
Link l= new Link();
Scanner sc=new Scanner(System.in);
int a;
while(true)
{

System.out.println("enter your choice\n 1 for insert\n 2 for display\n 3 for exit");
a=sc.nextInt();
switch (a)
{

case 1:
System.out.println("enter a number");
String b=sc.next();
l.insert(b);
break;
case 2:
l.display();
break;
case 3:
exit();
break;
default:
System.out.println("Please enter correct choice");


}
}
}
public static void insert(Object a)
{
Elem e=new Elem(); // Create a new list element.
e.data=a; // Set the data field.
if (front==null)
{
front=e; // Back element will be set below.

}
else
{
back.next=e; // Link last elem to new element.
}
back=e; // Update back to link to new element.
}

public static void display()
{
Elem curr=front;
while (curr!=null)
{
System.out.println(curr.data);
curr=curr.next;
}

}

public static void exit()
{
System.exit(0);
}
}

doubly Linked list in java

import java.util.*;
class Elem2
{
    public Elem2 next;
    public Elem2 prev;
    public Object data;

};
class  SimpleDoublyList
{
    static Elem2 front = null;
    static Elem2 back = null;
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        SimpleDoublyList ll=new SimpleDoublyList();
        while (true)
        {
            System.out.println("Please enter your choice\n 1 for insertion \n 2 for display \n 3 for delete\n 4 for exit");
            int a=sc.nextInt();
            switch (a)
            {
            case 1:
                System.out.println("Please enter data");
                String s=sc.next();
                ll.insert(s);
            break;
            case 2:
                ll.display();
            break;
            case 3:
                System.out.println("Enter the element that you want to delete");
                String el=sc.next();
                ll.delete(el);
            case 4:
                ll.exit();
            break;
            default:
                System.out.println("Please enter correct choice");

           
            }
        }
    }
    public static void insert(Object o)
    {
      Elem2 e=new Elem2();
      e.data=o;
      if (front==null)
      {
          front = e;
      }
      else
        {
          back.next=e;
        }
        e.prev=back;
        back=e;
     
    }
    public static void display()
    {
        for (Elem2 e=front;e!=null ;e=e.next )
        {
            System.out.println(e.data);
        }
    }
    private static void exit()
    {
        System.exit(0);
    }
    private static void delete(Object o)
    {

        Elem2 temp=front;
        Elem2 prev=null;
        do
        {
            if(temp.data.equals(o))
            {
                prev.next=temp.next;
                break;
            }
            prev=temp;
            temp=temp.next;
        }
        while (temp!=null);
        System.out.println("List after deletion");
        for (Elem2 e=front;e!=null ;e=e.next )
        {
            System.out.println(e.data);
        }


    }
}