package com.hmx.datastructure;
import javax.xml.soap.Node;
/**
* @program: datastructureandalgorithm
* @description:
* @author: hmx
* @create: 2021-06-29 17:07
**/
public class MyQueue<E> {
private int size;
private Node<E> head;
private Node<E> tail;
public MyQueue() {
head = null;
tail = null;
}
public boolean empty(){
return size == 0;
}
public int length(){
return size;
}
public boolean add(E e){
if(tail != null){
tail.next = new Node(e, null);
size++;
return true;
}
head = new Node<>(e,null);
tail = head;
size++;
return true;
}
public E get(){
if(empty()){
throw new RuntimeException("空队列异常");
} else {
Node<E> node = head;
/*if(head.next == null){
head = null;
tail = null;
} else{
head = head.next;
}
size--;*/
head = head.next;
node.next = null;
return node.value;
}
}
private class Node<E>{
E value;
Node next;
public Node(){}
public Node(E value,Node next){
this.value = value;
this.next = next;
}
}
}
队列
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 除了上边提到的『主线程』中调用『主队列』+『同步执行』会导致死锁问题。实际在使用『串行队列』的时候,也可能出现阻塞...
- //循环队列顺序表示,队列初始化,求队列长度,入队,出队,打印队列中的元素,求队头元素 include <stdi...
- GCD的基本思想就是将操作(任务)放在队列中去执行 队列负责调度任务执行所在的线程以及具体的执行时间 队列的特点是...
- GCD中涉及到两个十分重要的概念, 就是任务和队列 任务(Task): 你需要执行的操作 队列(Queue): 存...