Double-ended queue Implementation
package com.java2novice.ds.queue;
import java.util.ArrayList;
import java.util.List;
public class DoubleEndedQueueImpl
{
private List deque = new
ArrayList();
public void
insertFront(int item)
{
System.out.println("adding
at front: "+item);
deque.add(0,item);
System.out.println(deque);
}
public void
insertRear(int item)
{
System.out.println("adding
at rear: "+item);
deque.add(item);
System.out.println(deque);
}
public void
removeFront()
{
if(deque.isEmpty())
{
System.out.println("Deque
underflow!! unable to remove.");
return;
}
int
rem = deque.remove(0);
System.out.println("removed
from front: "+rem);
System.out.println(deque);
}
public void
removeRear()
{
if(deque.isEmpty())
{
System.out.println("Deque
underflow!! unable to remove.");
return;
}
int
rem = deque.remove(deque.size()-1);
System.out.println("removed
from front: "+rem);
System.out.println(deque);
}
public
int peakFront()
{
int
item = deque.get(0);
System.out.println("Element
at first: "+item);
return
item;
}
public int peakRear()
{
int
item = deque.get(deque.size()-1);
System.out.println("Element
at rear: "+item);
return
item;
}
public static
void main(String a[])
{
DoubleEndedQueueImpl
deq = new DoubleEndedQueueImpl();
deq.insertFront(34);
deq.insertRear(45);
deq.removeFront();
deq.removeFront();
deq.removeFront();
deq.insertFront(21);
deq.insertFront(98);
deq.insertRear(5);
deq.insertFront(43);
deq.removeRear();
}
}
Double-ended queue implementation using Doubly linked
list
package com.java2novice.ds.queue;
public class DequeDblLinkedListImpl
{
private Node
front;
private Node
rear;
public void
insertFront(T item)
{
System.out.println("adding
at front: "+item);
Node
nd = new Node();
nd.setValue(item);
nd.setNext(front);
if(front
!= null) front.setPrev(nd);
if(front
== null) rear = nd;
front
= nd;
}
public void
insertRear(T item)
{
System.out.println("adding
at rear: "+item);
Node
nd = new Node();
nd.setValue(item);
nd.setPrev(rear);
if(rear
!= null) rear.setNext(nd);
if(rear
== null) front = nd;
rear
= nd;
}
public void
removeFront()
{
if(front
== null)
{
System.out.println("Deque
underflow!! unable to remove.");
return;
}
Node
tmpFront = front.getNext();
if(tmpFront
!= null) tmpFront.setPrev(null);
if(tmpFront
== null) rear = null;
System.out.println("removed
from front: "+front.getValue());
front
= tmpFront;
}
public void
removeRear()
{
if(rear
== null)
{
System.out.println("Deque
underflow!! unable to remove.");
return;
}
Node
tmpRear = rear.getPrev();
if(tmpRear
!= null) tmpRear.setNext(null);
if(tmpRear
== null) front = null;
System.out.println("removed
from rear: "+rear.getValue());
rear
= tmpRear;
}
public
static void main(String a[])
{
DequeDblLinkedListImpl
deque = new DequeDblLinkedListImpl();
deque.insertFront(34);
deque.insertFront(67);
deque.insertFront(29);
deque.insertFront(765);
deque.removeFront();
deque.removeFront();
deque.removeFront();
deque.insertRear(43);
deque.insertRear(83);
deque.insertRear(84);
deque.insertRear(546);
deque.insertRear(356);
deque.removeRear();
deque.removeRear();
deque.removeRear();
deque.removeRear();
deque.removeFront();
deque.removeFront();
deque.removeFront();
}
}
class Node
{
private Node
prev;
private Node
next;
private T value;
public Node
getPrev()
{
return
prev;
}
public void
setPrev(Node prev)
{
this.prev
= prev;
}
public Node
getNext()
{
return
next;
}
public void
setNext(Node next)
{
this.next
= next;
}
public T getValue()
{
return
value;
}
public void setValue(T
value)
{
this.value
= value;
}
}
No comments:
Post a Comment