Friday, March 13, 2015

Doubly linked list implementation and array based implementation in java



Doubly linked list implementation

package com.java2novice.ds.linkedlist;
import java.util.NoSuchElementException;
public class DoublyLinkedListImpl
{
    private Node head;
    private Node tail;
    private int size;
    public DoublyLinkedListImpl()
    {
        size = 0;
    }
      private class Node
    {
        E element;
        Node next;
        Node prev;
        public Node(E element, Node next, Node prev)
       {
            this.element = element;
            this.next = next;
            this.prev = prev;
        }
    }
    public int size() { return size; }
    public boolean isEmpty() { return size == 0; }
    public void addFirst(E element)
     {
        Node tmp = new Node(element, head, null);
        if(head != null ) {head.prev = tmp;}
        head = tmp;
        if(tail == null) { tail = tmp;}
        size++;
        System.out.println("adding: "+element);
    }
    public void addLast(E element)
    {
        Node tmp = new Node(element, null, tail);
        if(tail != null) {tail.next = tmp;}
        tail = tmp;
        if(head == null) { head = tmp;}
        size++;
        System.out.println("adding: "+element);
    }
      public void iterateForward()
       {
        System.out.println("iterating forward..");
        Node tmp = head;
        while(tmp != null)
        {
            System.out.println(tmp.element);
            tmp = tmp.next;
        }
    }
    public void iterateBackward()
   
        System.out.println("iterating backword..");
        Node tmp = tail;
        while(tmp != null)
        {
            System.out.println(tmp.element);
            tmp = tmp.prev;
        }
    }
    public E removeFirst()
    {
        if (size == 0) throw new NoSuchElementException();
        Node tmp = head;
        head = head.next;
        head.prev = null;
        size--;
        System.out.println("deleted: "+tmp.element);
        return tmp.element;
    }   
    public E removeLast()
    {
        if (size == 0) throw new NoSuchElementException();
        Node tmp = tail;
        tail = tail.prev;
        tail.next = null;
        size--;
        System.out.println("deleted: "+tmp.element);
        return tmp.element;
    }
    public static void main(String a[])
   {
             DoublyLinkedListImpl dll = new         DoublyLinkedListImpl();
        dll.addFirst(10);
        dll.addFirst(34);
        dll.addLast(56);
        dll.addLast(364);
        dll.iterateForward();
        dll.removeFirst();
        dll.removeLast();
        dll.iterateBackward();
    }
}

Queue introduction & array based implementation

package com.java2novice.ds.queue;
public class QueueImpl
  {
    private int capacity;
    int queueArr[];
    int front = 0;
    int rear = -1;
    int currentSize = 0;
     public QueueImpl(int queueSize)
     {
        this.capacity = queueSize;
        queueArr = new int[this.capacity];
    }
    public void enqueue(int item)
      {
        if (isQueueFull())
         {
            System.out.println("Overflow ! Unable to add element: "+item);
        }
          else
           {
            rear++;
            if(rear == capacity-1)
            {
                rear = 0;
            }
            queueArr[rear] = item;
            currentSize++;
            System.out.println("Element " + item+ " is pushed to Queue !");
        }
    }    
    public void dequeue()
    {
        if (isQueueEmpty())
        {
            System.out.println("Underflow ! Unable to remove element from Queue");
        }
          else
          {
            front++;
            if(front == capacity-1)
            {
                System.out.println("Pop operation done ! removed: "+queueArr[front-1]);
                front = 0;
            }
            else
            {
                System.out.println("Pop operation done ! removed: "+queueArr[front-1]);
            }
            currentSize--;
        }
   }
    public boolean isQueueFull()
     {
        boolean status = false;
        if (currentSize == capacity)
       {
            status = true;
        }
        return status;
    }
    public boolean isQueueEmpty()
      {
        boolean status = false;
        if (currentSize == 0)
         {
            status = true;
        }
        return status;
    }
    public static void main(String a[])
    {
        QueueImpl queue = new QueueImpl(4);
        queue.enqueue(4);
        queue.dequeue();
        queue.enqueue(56);
        queue.enqueue(2);
        queue.enqueue(67);
        queue.dequeue();
        queue.dequeue();
        queue.enqueue(24);
        queue.dequeue();
        queue.enqueue(98);
        queue.enqueue(45);
        queue.enqueue(23);
        queue.enqueue(435);
    }
}

No comments:

Post a Comment