我正在尝试编写自己的队列类。我的入队方法只是将一个对象入队,然后如果我尝试将其他任何东西入队,几乎就像忽略它一样。这是我的代码:
public class myQueue {
private Node front;
private Node back;
private int s;
public myQueue() {
front = null;
back = null;
s = 0;
}
public void enqueue(Object x) {
if( isEmpty() )
back = front = new Node(x);
else
back = back.next = new Node(x);
s++;
}
public Object dequeue() {
Object x;
if( isEmpty() ) { System.out.println("nothing to dequeue.\nqueue empty."); }
x = front.data;
s--;
return x;
}
public boolean isEmpty() {
if(s == 0)
return true;
else
return false;
}
public void printQueue() {
if ( isEmpty() )
System.out.println("empty queue");
else {
Node temp = back;
while(temp != null) {
System.out.println(temp);
temp = temp.next;
}
}
}
}
这是我尝试将一些对象排队的主要方法:
public static void main(String[] args) {
int a = 5;
String b = "yo";
Object c = 5.5;
int d = 2;
String e = "Pen";
Object f = 9.2;
myQueue q = new myQueue();
q.enqueue(a);
q.enqueue(b);
q.enqueue(c);
q.enqueue(d);
q.enqueue(e);
q.enqueue(f);
System.out.println("\n");
q.printQueue();
}
然后我得到的输出是:
数据:9.2
关于为什么会发生这种情况的任何想法?