数据结构——Java实现队列

来源:互联网 发布:交友软件遇见 编辑:程序博客网 时间:2024/06/05 19:21

和栈相反,队列是一种先进先出(FIFO)的线性表

其限制为仅允许在表的一端进行插入,在表的另一端进行删除

可插入的一端称为队尾(rear),可进行删除的一端称为队头(front)


                          

向队列中插入新的元素称为入队,新元素进队后称为队尾元素,从队列中删除元素称为出队,元素出队后,其后继元素称为队头元素


1)顺序队——循环队列

循环队列是对普通顺序队的改进,顺序队进过多次入队、出队会“假溢出”,front和rear都在数据最后一个元素处,即使队列中没有元素也无法入队


package queue;import java.util.Arrays;/** * @author 芜情 * 顺序队——循环队列 */public class SequenceQueue<T> {private int front;//队首指针,指向队首元素private int rear;//队尾指针,指向队尾元素private int capacity;//队列容量private Object[] items;public SequenceQueue() {this(10);}public SequenceQueue(int capacity) {this.capacity = capacity;this.items = new Object[capacity];this.front = 0;this.rear = -1;}//清空队列public void clear(){Arrays.fill(this.items, null);this.front = 0;this.rear = -1;}//元素个数public int size(){if(rear == -1){return 0;}else{return rear > front ? rear - front+1 : rear + capacity - front+1;}}//判断队空public boolean isEmpty(){return this.front == this.rear || this.rear == -1;}//判断队满public boolean isFull(){return this.front == (this.rear + 1 ) % this.capacity && this.rear != -1;}//入队public void offer(T t){if(isFull()){throw new RuntimeException("队列已满");}else{rear = ++rear % capacity;items[rear] = t;}}//出队,返回队首元素public Object poll(){if(isEmpty()){throw new RuntimeException("队列为空");}else{Object obj = items[front];items[front] = null;front = ++front % capacity;return obj;}}//获取队首元素public Object peek(){if(isEmpty()){throw new RuntimeException("队列为空");}else{return items[front];}}//遍历public void display(){StringBuilder sb = new StringBuilder();if(front < rear){for(int i = front; i <= rear; i++){sb.append(items[i] + "  ");}}else{for(int i = front; i < capacity; i++){sb.append(items[i] + "  ");}for(int i = 0; i <= rear; i++){sb.append(items[i] + "  ");}}System.out.println(sb);}}


2)链队

package queue;import java.text.SimpleDateFormat;import java.util.Date;public class LinkedQueue<E> {private class Node{public Node next;public Object element;public Node() {}public Node(E element) {this.element = element;}}private Node head;//头结点private Node front;//队首指针,指向首节点private Node rear;//队尾指针,指向尾节点private int size;//元素数量public LinkedQueue() {this.head = new Node();this.head.element = "创建时间:"+new SimpleDateFormat("yyyy-MM-dd E HH:mm:ss").format(new Date());this.head.next = this.front;this.front = this.rear = null;this.size = 0;}//清空public void clear(){front = rear =null;this.size = 0;}//判断队空public boolean isEmpty(){return front == null && rear == null;}//元素个数public int size(){return this.size;}//入队public void offer(E e){if(front != null){this.rear.next  = new Node(e);this.rear = this.rear.next;}else{this.front = this.rear = new Node(e);}this.size++;}//出队,返回队首元素public Object poll(){if(isEmpty()){throw new RuntimeException("队列为空");}Object obj = this.front.element;this.front = this.front.next;this.size--;return obj;}//返回队首元素public Object peek(){return this.front.element;}//遍历public void display(){if(isEmpty()){throw new RuntimeException("栈空");}else{Node node = this.front;StringBuilder sb = new StringBuilder();while(node != rear){sb.append(node.element.toString() + "  ");node = node.next;}sb.append(rear.element.toString());System.out.println(sb);}}}


原创粉丝点击