I want to program a enqueue(elem) in Java, so I have programmed the following modules:
public class Node{
public int e;
Node next;
public Node(int e){
this.e=e;
}
}
and now I want to use a Linked List to store my elements, so I made a class with a enqueue() function like this:
public class Queue{
Node q; //represents a queue
Node first;
public void enqueue(int n){
Node t=new Node(n); //represents a temporal node
if(q==null){
first=t;
}
else{
t.next=first;
t=first;
}
q=t; //to store the node into the queue
}
}
but when I want to print the elements of my queue:
public void print(){
Node current=first;
while (current!=null){
System.out.println(current.e);
current=current.next;
}
}
it only prints me the first element that I enter, for example if I put 10,20,30,40 it only prints 10. What am I doing wrong?
Thanks
qshould be your queue tail?