Linked List Queue

import java.io.*;
class Que_Node
{
int info;
Que_Node nptr;
static Que_Node front;
static Que_Node rear;

public static void main()throws IOException
{
InputStreamReader x=new InputStreamReader(System.in);
BufferedReader y=new BufferedReader(x);
int e,choice,flag;
front=null;
rear=null;

do
{
System.out.println(” \f 1. Add \n 2. Remove \n 3. Display \n 4. Exit”);
choice = Integer.parseInt(y.readLine());

switch(choice)
{
case 1: System.out.println(“Element”);
e=Integer.parseInt(y.readLine());
Que_Node current = new Que_Node();
current=current.createQue_Node(e);
current.addQue_Node(current);
break;
case 2:
front.removeQue_Node();
break;
case 3:
front.displayQue_Nodes();
break;

case 4: System.out.println(“Quitting…”);
System.exit(0);

default:
System.out.println(“Wrong choice…”);

}

System.out.println(“Enter: 1 to continue, 0 to terminate”);
flag = Integer.parseInt(y.readLine());

} while(flag==1);

}

public Que_Node createQue_Node(int e)
{
Que_Node temp = new Que_Node();
temp.info = e;
temp.nptr = null;
return temp;
}

public void addQue_Node(Que_Node nd)
{
if(front==null && rear==null)
{
front=nd;
rear=nd;
}

else
{
rear.nptr=nd;
rear=nd;
}

}

public void displayQue_Nodes()
{
if(front==null)
System.out.println(“Queue Empty…”);

else
{
Que_Node copy = front;
System.out.println(“The elements of the queue are :”);
System.out.print(“FRONT : “);
while(copy!=null)
{
System.out.print(copy.info + ” “);
copy=copy.nptr;
}
System.out.println(” : REAR”);
}
}

public void removeQue_Node()
{
if(front==null)
System.out.println(“Queue underflow…”);

else
{
System.out.println(“Element “+ front.info +” has been removed…”);
front=front.nptr;
}

}

}

Advertisements
Comments
  1. srijab says:

    This was very helpful. Thank you for posting up all these programs, since they are really difficult to find in a simplified code and a code acceptable in ISC examinations over the internet. They were a great help to me while I was creating a project.

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