发布于2021-03-13 14:12 阅读(911) 评论(0) 点赞(17) 收藏(4)
概念:
只允许在一端进行数据插入操作,再另一端进行删除数据的特殊线性表,具有先进先出(FIFO)特性,进行插入的一端称为队尾,进行删除操作的一端称为对头。
基本方法:
1.E offer(); 入队
2.E poll(); 出队
3.E peek(); 查看队首元素(不删除)
实现:(以int类型数据为例)
class Node{
public int val;
public Node next;
public Node(int val) {
this.val = val;
}
}
public class MyQueue {
public Node first;//头
public Node last;//尾
public int size;
public boolean offer(int val){
Node node = new Node(val);
if(this.first == null){
this.first = node;
this.last = node;
}else{
this.last.next = node;
this.last = node;
}
size++;
return true;
}
public int poll() throws RuntimeException{
if(isEmpty()){
throw new RuntimeException("队列为空!");
}
int ret = this.first.val;
this.first = this.first.next;
size--;
return ret;
}
public int peek() throws RuntimeException{
if(isEmpty()){
throw new RuntimeException("队列为空!");
}
return this.first.val;
}
public boolean isEmpty(){
if(this.first == null && this.last == null){
return true;
}
return false;
}
public void show(){
for(int i = 0; i < size; i++){
System.out.println(this.first.val);
this.first = this.first.next;
}
}
public static void main(String[] args) {
MyQueue myQueue = new MyQueue();
myQueue.offer(5);
myQueue.offer(9);
myQueue.offer(3);
// System.out.println(myQueue.peek());
// myQueue.poll();
// System.out.println(myQueue.size);
myQueue.show();
}
}
原文链接:https://blog.csdn.net/DavenportChen/article/details/114675027
作者:欧美丽
链接:http://www.javaheidong.com/blog/article/114324/f758c7934abc4d28cce3/
来源:java黑洞网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
---无人问津也好,技不如人也罢,你都要试着安静下来,去做自己该做的事,而不是让内心的烦躁、焦虑,坏掉你本来就不多的热情和定力
Copyright © 2018-2021 java黑洞网 All Rights Reserved 版权所有,并保留所有权利。京ICP备18063182号-2
投诉与举报,广告合作请联系vgs_info@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!