Tuesday, 21 August 2012

To implement queue using linked list.

class Nodetype
{
int info;
Nodetype next;
Nodetype(int i)
{
info=i;
next=null;
}
}
class Operations
{
Nodetype front=null,rear=null;
/*Where front is the pointer which will point to the front of the queue
and rear will point to rear end of the queue*/
void display()
{
/*display all elements of linked list*/

Nodetype temp;
if(front==null)
System.out.println("\nEmpty linked list");
else
{
System.out.print("\nfront");
temp=front;
while(temp!=null)
{
System.out.print("-->"+temp.info);
temp=temp.next;
}
System.out.println("<--rear");
}
} /* end display */
//--------------------------------------------------------------------------------------------------------------
void insert(int x)
{
Nodetype node=new Nodetype(x);
node.info=x;
node.next=null;
if (front==null&&rear==null)
{
front=node;
rear=node;
}
else
{
rear.next=node;
rear=node;
}
display();
}/*end insert*/
//--------------------------------------------------------------------------------------------------------------
void remove()
{
Nodetype p=null;
int x;
p=front;
if(p==null)
{
System.out.println("\nUnderflow\n");
return;
}
System.out.print("\nThe element removed is "+p.info);
front=front.next;
if(front==null)
rear=null;
display();
}/*end removee*/
}
//--------------------------------------------------------------------------------------------------------------
class LLQueue
{
public static void main(String args[])
{
Operations L =new Operations();
int i,j;
for(i=0;i<5;i++)
{
j = new Integer((int)(Math.random() * 100));
System.out.print("\n\nInsert: " + j);
L.insert(j);
}
//L.display();
for(i=0;i<3;i++)
L.remove();
for(i=0;i<2;i++)
{
j = new Integer((int)(Math.random() * 100));
System.out.print("\n\nInsert: " + j);
L.insert(j);
}
}/*end main*/
}

/*
Sample Input and Output :
Insert: 32
front-->32<--rear

Insert: 72
front-->32-->72<--rear

Insert: 10
front-->32-->72-->10<--rear

Insert: 88
front-->32-->72-->10-->88<--rear

Insert: 14
front-->32-->72-->10-->88-->14<--rear

The element removed is 32
front-->72-->10-->88-->14<--rear

The element removed is 72
front-->10-->88-->14<--rear

The element removed is 10
front-->88-->14<--rear

Insert: 11
front-->88-->14-->11<--rear

Insert: 27
front-->88-->14-->11-->27<--rear
*/

 
To implement priority queue using array.
/* implementation of descending priority queue*/
class Queue
{
/*array is used to hold queue elements*/
/*two integer variables are used for front and rear pointers*/
int items[]=new int[10];
int front,rear;
Queue()
{
front=1;
rear=0;
}
int empty()
{
if((front==1)&&(rear==0) ||(front>rear))
return 1;
else
return 0;
}
void display( )
{
/* function to display queue elements */
int i=front;
if(empty()==1)
System.out.println("Q is empty ");
else
{
System.out.print("Front-->");
while(i<=rear)
{
System.out.print("  "+items[i]);
i++;
}
System.out.println("<--Rear");
}
}
void add(int x)
{
/*function to add new element*/
System.out.println("\nInsert: " + x);
if(rear==9)
System.out.println("Queue is Full");
else
{
rear++;
items[rear]=x;
display();
}
}
void delet( )
{
/* Function to delete the largest number */
int large,i,j,k;
if(empty()==1)
System.out.println("Q is empty ");
else
{
i=front+1;
large=items[front];
k=front;
while(i<=rear)
{
if (items[i]>large)
{
large=items[i];
k=i;
}
i++;
}
System.out.println("\nNumber being deleted is : "+items[k]);
if(k==front)
                                  /*if first element is deleted increment front pointer*/
front++;
else
{
/*if middle element or last element is deleted decrement rear pointer*/
/*copy all elements forward*/
for(i=k;i<rear;i++)
items[i]=items[i+1];
rear--;
}
display();
}
}
}
class QPriority
{
public static void main(String args[])
{
Queue q=new Queue();
int i,j;
System.out.println("Starting...");
for(i=0;i<10;i++)
{
   j = new Integer((int)(Math.random() * 100));
   q.add(j);
}
while(q.empty()==0)
{
    q.delet();
}
System.out.println("Done ;-)");
}
}

/*
Sample Input and Output :
Starting...

Insert: 60
Front-->  60<--Rear

Insert: 66
Front-->  60  66<--Rear

Insert: 40
Front-->  60  66  40<--Rear

Insert: 22
Front-->  60  66  40  22<--Rear

Insert: 36
Front-->  60  66  40  22  36<--Rear

Insert: 49
Front-->  60  66  40  22  36  49<--Rear

Insert: 51
Front-->  60  66  40  22  36  49  51<--Rear

Insert: 66
Front-->  60  66  40  22  36  49  51  66<--Rear

Insert: 68
Front-->  60  66  40  22  36  49  51  66  68<--Rear

Insert: 87
Queue is Full

Number being deleted is : 68
Front-->  60  66  40  22  36  49  51  66<--Rear

Number being deleted is : 66
Front-->  60  40  22  36  49  51  66<--Rear
Number being deleted is : 66
Front-->  60  40  22  36  49  51<--Rear

Number being deleted is : 60
Front-->  40  22  36  49  51<--Rear

Number being deleted is : 51
Front-->  40  22  36  49<--Rear

Number being deleted is : 49
Front-->  40  22  36<--Rear

Number being deleted is : 40
Front-->  22  36<--Rear

Number being deleted is : 36
Front-->  22<--Rear

Number being deleted is : 22
Q is empty
Done ;-)
*/

No comments:

Post a Comment