0

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;      
}
theastros
  • 3
  • 4
  • This might be a good time to learn how to debug. I would recommend either actually using the debugger or even just printing each line as you go to see whether the code matches your expectations line by line. Maybe research *rubber duck debugging*. – Henry Twist Apr 29 '21 at 11:58
  • thank you @HenryTwist i will try it – theastros Apr 29 '21 at 13:54
  • @JakubDóka that's unkind of you, while you yourself are sending someone else's code and asking me to find the bug myself – theastros Apr 29 '21 at 13:55
  • Yes this seems a bit hypocritical @JakubDóka. You're suggesting they find the bug themselves (which I agree with by the way), but then posting an answer anyway? Doesn't seem very helpful either way. Also you never really addressed in your answer *why* the OPs implementation wasn't working. – Henry Twist Apr 29 '21 at 14:12
  • if he at least bothered to paste his code to online formatter so it is readable i would actually spend my time to understand it. – Jakub Dóka Apr 29 '21 at 14:21
  • Absolutely @JakubDóka, that's why you shouldn't spend time on it. Either downvote, leave some criticism or flag it to be closed, there's no need to do anything else. – Henry Twist Apr 29 '21 at 15:46

0 Answers0