Showing posts with label lab programs. Show all posts
Showing posts with label lab programs. 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("");
}
}

}

Stack 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 first;
public LinkedList()
{
first = null;
}
public void push(int val)
{
Node newNode = new Node(val);
newNode.next = first;
first = newNode;
}
public int pop()
{
if(first==null)
{
System.out.println("Stack is Empty");
return 0;
}
else
{
int temp = first.item;
first = first.next;
return temp;
}
}
public void display()
{
if(first==null)
{
System.out.println("Stack is Empty");
}
else
{
System.out.println("Elements from top to bottom");
Node current = first;
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]);
}
}
}

}

Stack implementation using Array in Java


import java.io.*;
class Aray
{
private int a[];
public int t=-1,i;
public Aray()
{
a=new int[5];
}
public void push(int val)
{
if(t<4)
{
t++;
a[t]=val;
}
else
System.out.println("Stack is full");
}
public int pop()
{
if(t==-1)
{
System.out.println("Stack is Empty");
return 0;
}
else
{
int temp = a[t];
t--;
return temp;
}
}
public void display()
{
if(t==-1)
{
System.out.println("Stack is Empty");
}
else
{
System.out.println("Elements from top to bottom");
for(i=t;i>-1;i--)
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...