Showing posts with label Queue using array. Show all posts
Showing posts with label Queue using array. Show all posts

Sunday, 16 December 2012

Queue implementation using Linked List in Java


import java.io.*;
class Node
{
public int item;
public Node next;
public Node(int val)
{
item = val;
}
}
class LinkedList
{
private Node front,rear;
public LinkedList()
{
front = null;
rear = null;
}
public void insert(int val)
{
Node newNode = new Node(val);
if (front == null) {
            front = rear = newNode;
        }
else {
            rear.next = newNode;
rear = newNode;
}
}
public int delete()
{
if(front==null)
{
System.out.println("Queue is Empty");
return 0;
}
else
{
int temp = front.item;
            front = front.next;
            return temp;
}
}
public void display()
{
if(front==null)
{
System.out.println("Queue is Empty");
}
else
{
System.out.println("Elements in the Queue");
Node current = front;
while(current != null)
{
System.out.println("[" + current.item + "] ");
current = current.next;
}
System.out.println("");
}
}

}

Queue implementation using Array in Java


import java.io.*;
class Aray
{
private int a[];
public int c=0,i,front,rear;
public Aray()
{
a=new int[5];
front=0;
rear=-1;
}
public void insert(int val)
{
if(c<=4)
{
rear=(rear+1)%5;
a[rear]=val;
c++;
}
else
System.out.println("Queue is full");
}
public int delete()
{
if(c==0)
{
System.out.println("Queue is Empty");
return 0;
}
else
{
int temp = a[front];
front=(front+1)%5;
c--;
return temp;
}
}
public void display()
{
if(c==0)
{
System.out.println("Queue is Empty");
}
else
{
System.out.println("Elements from front to rear");
int m=0;
for(i=front;m<c;i++,m++)
{
if(i>4)
i=i%5;
System.out.println(a[i]);
}
}
}

}

Best geography books for UPSC prelims, mains

This post is intended to clear the confusion that prevails among the aspirants over how to prepare for UPSC geography and the best books f...