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;
}
}
}
队列(java实现)
最新推荐文章于 2024-08-08 14:30:51 发布