[数据结构]-链表实现队列
[数据结构]-链表实现队列
如有问题,请指出呀。感谢。
package com.cn.jichu.day09; public class LinkedQueue<E> { /** * 首节点 */ private Node head; /** * 尾节点 */ private Node tail; /** * 当前栈中元素个数 */ private int size; public LinkedQueue(){ this.size = 0; } /** * 入列 * @param e */ public void enqueue(E e){ Node node = new Node(e); if(head == null && tail == null){ head = tail = node; size ++; return; } tail.next = node; tail = node; size ++; } /** * 出列 * @return */ public E dequeue(){ if(head==null){ throw new IllegalArgumentException("当前队列无元素"); } if(head.equals(tail)){ System.out.println("head.equals(tail)"); E e = head.data; head = tail = null; size --; return e; } E e = head.data; head = head.next; size --; return e; } /** * 获取队列中的元素个数 * @return */ public int getSize(){ return size; } private class Node{ private Node next; private E data; public Node(E data) { this.data = data; } } @Override public String toString(){ StringBuilder ret = new StringBuilder(); ret.append("queue head["); Node node = head; while (node!=null){ ret.append(node.data + "-->"); node = node.next; } ret.append("null ] tail"); return ret.toString(); } }
Don't stop!Never give up!