zoukankan      html  css  js  c++  java
  • 6、队列的链式存储、含首尾指针的单链表

     1 package ren.laughing.datastructure.baseImpl;
     2 
     3 import ren.laughing.datastructure.base.Queue;
     4 import ren.laughing.datastructure.exception.QueueEmptyException;
     5 /**
     6  * 队列的链式存储
     7  * 单链表、含有队首和队尾指针
     8  * @author Laughing_Lz
     9  * @time 2016年4月7日
    10  */
    11 public class QueueLinked implements Queue {
    12     private SLNode front;//队首指针,指向链表空的头结点
    13     private SLNode rear;//队尾指针,指向链表的队尾元素
    14     private int size;//队的容量
    15     
    16     public QueueLinked() {
    17         front = new SLNode();
    18         rear = front;//队列初始为空,队首指针和队尾指针均指向空的头结点
    19         size = 0;
    20     }
    21     
    22     @Override
    23     public int getSize() {
    24         return size;
    25     }
    26 
    27     @Override
    28     public boolean isEmpty() {
    29         return size == 0;
    30     }
    31     //入队:从队尾入队,相当于insertAfter队尾结点rear
    32     @Override
    33     public void enqueue(Object e) {
    34         SLNode node =new SLNode(e, null);//写出rear.getNext()应该也可以?
    35         rear.setNext(node);
    36         rear = node;
    37         size++;
    38     }
    39     //出队:从队首出队,相当于removeAfter队首头结点front
    40     @Override
    41     public Object dequeue() throws QueueEmptyException {
    42         if(size <= 0){
    43             throw new QueueEmptyException("错误:队列为空");
    44         }
    45         SLNode node = front.getNext();//要删除的队首数据元素
    46         front.setNext(node.getNext());
    47         size--;
    48         if(size <= 0){//★再次判断size,若已全部出队,则将rear指针也和front指针一样指向空的头结点
    49             rear = front;
    50         }
    51         return node.getData();
    52     }
    53     
    54     @Override
    55     public Object peek() throws QueueEmptyException {
    56         if(size <= 0){
    57             throw new QueueEmptyException("错误:队列为空");
    58         }
    59         SLNode node = front.getNext();
    60         return node.getData();
    61     }
    62 
    63 }
    —————————————————————————————————————行走在人猿的并行线——Laughing_Lz
  • 相关阅读:
    堆栈(线性表)
    链表 -- 循环链表(线性表)
    链表 -- 单向链表(线性表)
    排序算法--归并算法(强分治)
    sqlhelper
    sqlite与sqlserver区别
    常用sql集锦
    外出实施问题总结
    JS深入理解系列(一):编写高质量代码
    通用分页(Jquery版)
  • 原文地址:https://www.cnblogs.com/Laughing-Lz/p/5365285.html
Copyright © 2011-2022 走看看