Can someone please review my code? Is it the correct implementation of a queue? If not, please give suggestions on how to implement it using a linked list.
class Queue
{
Node front,rear;
class Node
{
int data;
Node next;
public Node(int d)
{
data=d;
next=null;
}
}
void enqueue(int new_data)
{
Node new_node=new Node(new_data);
if(front==null)
{
new_node.next=front;
front=new_node;
rear=front;
}
else if(rear.next==null)
{
rear.next=new_node;
rear=rear.next;
}
System.out.println(rear.data+"\t Enqueued");
}
Node dequeue()
{
if(front==null && rear==null)
{
System.out.println("\t Queue is Empty");
return null;
}
else {
Node temp=front;
front=temp.next;
temp.next=null;
System.out.println(temp.data+"\t Dequeued");
if(front==null)
{ rear=front; }
System.out.println(front +" "+ rear);
return temp;
}
}
public static void main(String[] args)
{
Queue q=new Queue();
q.enqueue(1);
q.enqueue(2);
q.enqueue(3);
q.enqueue(4);
q.dequeue();
q.dequeue();
}
}