ToBeWantToBe 2017-11-19 03:35 采纳率: 0%
浏览 862

java用链表表示队列的问题

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的应该是队列的出端,怎么定位在进端了,请高人指点!

  • 写回答

2条回答

  • threenewbee 2017-11-19 04:23
    关注
    评论

报告相同问题?

悬赏问题

  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)