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
  • 相关阅读:
    终于有人讲清楚准确率(accuracy)、召唤率(recall)和精确率(precision)的关系了
    leetcode之两数相加解题思路
    Django-rest-framwork——认证
    py2neo详细介绍第一章
    springdata的懒加载时机
    错题记录
    SSM框架的优势?
    2020-06-30 pol实现execl导入导出
    2020-06-30 动态sql
    String 、StringBuilder 、StringBuffer 的区别? 什么情况下用“+”运算符进行字符串连接比调用 StringBuffer/StringBuilder对象的 append 方法连接字符串性能更好?.
  • 原文地址:https://www.cnblogs.com/Laughing-Lz/p/5365285.html
Copyright © 2011-2022 走看看