Implement a queue using doubly linked list

463 Views Asked by At

So, I'm trying to implement a queue using doubly linked list in java. But surprisingly, only first and last values of the input are getting stored in the queue. Can u guys find the error in my code.

listnode front;
listnode rear;
int length;
class listnode
{
    int data;
    listnode next;
    listnode prev;
    listnode(int data)
    {
        this.data=data;
        this.next=null;
    }
}
public void enqueue(int data)
  {
    listnode node=new listnode(data);
    listnode temp2=front;
    if(isempty())
    {
        node.prev=null;
        front= node;
        rear=node;
        length++;
        return;
    }
   
    while(temp2.next!=null)
        {
            temp2=temp2.next;
        }
        temp2.next=node;
        rear.next=node;
        node.prev=temp2;      
}
0

There are 0 best solutions below