博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
数据结构学习(五) Java链表实现队列
阅读量:5273 次
发布时间:2019-06-14

本文共 1790 字,大约阅读时间需要 5 分钟。

package queue;/** * @Title: LinkedListQueue * @ProjectName demo */public class LinkedListQueue
implements Queue
{ private class Node { private Node next; private E e; public Node(E e, Node next) { this.next = next; this.e = e; } public Node(E e) { this(e, null); } @Override public String toString() { return e.toString(); } } private int size; private Node head, tail; public LinkedListQueue() { size = 0; head = null; tail = null; } @Override public int getSize() { return size; } @Override public boolean isEmpty() { return size == 0; } @Override public void enqueue(E e) { if (tail == null) { tail = new Node(e); head = tail; }else{ tail.next = new Node(e); tail = tail.next; } size ++; } @Override public E dequeue() { if (isEmpty()){ throw new IllegalArgumentException("dequeue error.."); } Node cur = head; head = head.next; cur.next = null; if (head == null){ tail = null; } size--; return cur.e; } @Override public E getFront() { if (isEmpty()){ throw new IllegalArgumentException("GetFront error.."); } return head.e; } @Override public String toString() { StringBuilder sb = new StringBuilder("front:"); Node h = head; while (h != null) { sb.append("--->"+h.e); h = h.next; } sb.append("tail"); return sb.toString(); }}

  

转载于:https://www.cnblogs.com/412013cl/p/11008499.html

你可能感兴趣的文章