public class Linked_List_Queue {
public QueueNode front;
public QueueNode rear;
public Linked_List_Queue(){
rear=null;front=null;
}
public Boolean enqueue(int data) {
QueueNode node=new QueueNode(data);
if(rear==null) {
front=node;
}else {
rear.next=node;
rear=node;
}
return true;
}
public int dequeue() {
if(!(front==null)){
if(rear==front)
rear=null;
int val;
QueueNode temp;
temp=front;
val=temp.data;
front=front.next;
return val;
}
else {
return -1;
}
}
}
public class QueueImplementByList {
public static void main(String args[])throws IOException {
Linked_List_Queue linked_List_Queue=new Linked_List_Queue();
int temp;
System.out.println("以链表来实现队列");
System.out.println("=========================");
System.out.println("在队列前端加入第1个数据,此数据值为1");
linked_List_Queue.enqueue(1);
System.out.println("在队列前端加入第2个数据,此数据值为3");
linked_List_Queue.enqueue(3);
System.out.println("在队列前端加入第3个数据,此数据值为5");
linked_List_Queue.enqueue(5);
System.out.println("在队列前端加入第4个数据,此数据值为7");
linked_List_Queue.enqueue(7);
System.out.println("在队列前端加入第5个数据,此数据值为9");
linked_List_Queue.enqueue(9);
System.out.println("=========================");
while(true) {
if(!(linked_List_Queue.front==null)) {
temp=linked_List_Queue.dequeue();
System.out.print("取出的元素是:"+temp);
}else {
break;
}
}
System.out.print("\n");
}
}
输出:以链表来实现队列
在队列前端加入第1个数据,此数据值为1
在队列前端加入第2个数据,此数据值为3
在队列前端加入第3个数据,此数据值为5
在队列前端加入第4个数据,此数据值为7
在队列前端加入第5个数据,此数据值为9
取出的元素是:9
显示完毕!!
自学数据结构用java 问题最后一行:应该是1,3,5,7,9么,rear是队列的进端,
front的应该是队列的出端,怎么定位在进端了,请高人指点!