Dequeue

import java.util.*;
class Node
{
int data;
Node link;

public Node()
{
link = null;
data = 0;
}

public Node(int d,Node n)
{
data = d;
link = n;
}

public void setLink(Node n)
{
link = n;
}

public void setData(int d)
{
data = d;
}

public Node getLink()
{
return link;
}

public int getData()
{
return data;
}
}

class Dequeue
{
private Node front, rear;
private int size;

/* Constructor */
public Dequeue()
{
front = null;
rear = null;
size = 0;
}

/* Function to check if queue is empty */
public boolean isEmpty()
{
return front == null;
}

/* Function to get the size of the queue */
public int getSize()
{
return size;
}

/* Clear dequeue */
public void clear()
{
front = null;
rear = null;
size = 0;
}

/* Function to insert an element at begining */
public void insertAtFront(int val)
{
Node nptr = new Node(val, null);
size++ ;
if (front == null)
{
front = nptr;
rear = front;
}
else
{
nptr.setLink(front);
front = nptr;
}
}

/* Function to insert an element at end */
public void insertAtRear(int val)
{
Node nptr = new Node(val,null);
size++ ;
if (rear == null)
{
rear = nptr;
front = rear;
}
else
{
rear.setLink(nptr);
rear = nptr;
}
}

/* Function to remove front element from the queue */
public int removeAtFront()
{
if (isEmpty() )
System.out.println(“Underflow Exception”);
System.exit(0);
Node ptr = front;
front = ptr.getLink();

if (front == null)
rear = null;
size– ;

return ptr.getData();
}

/* Function to remove rear element from the queue */
public int removeAtRear()
{
if (isEmpty() )
System.out.println(“Underflow Exception”);
System.exit(0);

int ele = rear.getData();
Node s = front;
Node t = front;
while (s != rear)
{
t = s;
s = s.getLink();
}
rear = t;
rear.setLink(null);
size –;

return ele;
}

/* Function to check the front element of the queue */
public int peekAtFront()
{
if (isEmpty() )
System.out.println(“Underflow Exception”);
System.exit(0);
return front.getData();
}

/* Function to check the front element of the queue */
public int peekAtRear()
{
if (isEmpty() )
System.out.println(“Underflow Exception”);
System.exit(0);
return rear.getData();
}

/* Function to display the status of the queue */
public void display()
{
System.out.print(“\nDequeue = “);
if (size == 0)
{
System.out.print(“Empty\n”);
return ;
}
Node ptr = front;
while (ptr != rear.getLink() )
{
System.out.print(ptr.getData()+” “);
ptr = ptr.getLink();
}

}

public static void main()
{
Scanner scan = new Scanner(System.in);
Dequeue dq = new Dequeue();
System.out.println(“Dequeue Test”);

do
{
System.out.println(“Dequeue Operations”);
System.out.println(“1. Insert at front”);
System.out.println(“2. Insert at rear”);
System.out.println(“3. Delete at front”);
System.out.println(“4. Delete at rear”);
System.out.println(“5. Display front”);
System.out.println(“6. Display rear”);
System.out.println(“7. Check current size”);
System.out.println(“8. Check if empty”);
System.out.println(“9. Clear the Dequeue”);
System.out.println(“10. Quit”);
int choice = scan.nextInt();
switch (choice)
{
case 1 :
System.out.println(“Enter integer element to insert”);
dq.insertAtFront( scan.nextInt() );
break;
case 2 :
System.out.println(“Enter integer element to insert”);
dq.insertAtRear( scan.nextInt() );
break;
case 3 :
System.out.println(“Removed Element = “+ dq.removeAtFront());
break;
case 4 :
System.out.println(“Removed Element = “+ dq.removeAtRear());
break;
case 5 :
System.out.println(“Element = “+ dq.peekAtFront());
break;
case 6 :
System.out.println(“Element = “+ dq.peekAtRear());
break;
case 7 :
System.out.println(“Size = “+ dq.getSize());
break;
case 8 :
System.out.println(“Empty status = “+ dq.isEmpty());
break;
case 9 :
System.out.println(“Dequeue Cleared”);
dq.clear();
break;
case 10: System.exit(0);
default :
System.out.println(“Wrong Entry”);
break;
}
/* display dequeue */
dq.display();
System.out.println(“\nPress 1 to continue…”);
int pseudo = scan.nextInt();
} while (true);
}
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s