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
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 虚心请教几个问题,小生先有礼了
  • ¥30 截图中的mathematics程序转换成matlab