• SoftwareDataStructure 三年一直迷糊的链表


    2017-11-05 23:31:32 三年来其实一直迷糊的链表顿悟

    三年前 2014年下半年一直未理解而死敲代码,希望能量变引起质变的 数据结构中  链表的顿悟

    (查找算法中的二分查找。排序中的快速排序已与2015年专攻 JavaScript 时理解突破了)

    第一本 算法学习书《算法精解》以及让我理解了排序和查找 分治算法的书

     

    在学习 Java 描述的 数据结构时,对链表有了新的理解,FirstNode 的 插入及重置,默认构造函数的实现。

    插入的部分:

    newNode 不再存在,参数 newEntry 也是如此,行为像是一个局部变量。

    继续插入新的节点时,注意 Node 的两个构造函数

    1. 第一个构造函数 Node Pointer 为null, 为第一个节点所准备的。

    2. 常规的链中的节点:有data,有 pointer. 与 Node自动对应。

     1 public final class LinkedBag<T> implements BagInterface<T>{
     2 
     3     private Node firstNode;
     4     private int numberOfEntries;
     5 
     6     public LinkedBag() {
     7         firstNode = null;
     8         numberOfEntries = 0;
     9     } // end default constructor
    10 
    11     private class Node // private inner class
    12     {
    13         private T data; // Entry in bag
    14         private Node next; // Link to next node
    15 
    16         private Node(T dataPortion)
    17         {
    18             this(dataPortion, null);
    19         } // end constructor
    20 
    21         private Node(T dataPortion, Node nextNode)
    22         {
    23             data = dataPortion;
    24             next = nextNode;
    25         } // end constructor
    26 
    27         private T getData(){
    28             return data;
    29         }
    30 
    31         private void setData(T newData){
    32             data = newData;
    33         }
    34 
    35         private Node getNextNode(){
    36             return next;
    37         }
    38 
    39         private void setNextNode(Node nextNode){
    40             next = nextNode;
    41         }
    42     }
    43 
    44     /** Adds a new entry to this bag.
    45      * @param newEntry  The object to be added as a new entry.
    46      *                  @return True
    47      */
    48     public boolean add(T newEntry) // OutOfMemoryError possible
    49     {
    50         // Add to beginning of chain.
    51         Node newNode = new Node(newEntry);
    52         //newNode.next = firstNode; // Make new node reference rest of chain.
    53                                     //(first node is null if chain is empty)
    54         newNode.setNextNode(firstNode);
    55 
    56         firstNode = newNode;
    57         numberOfEntries++;
    58 
    59         return true;
    60     }
    View Code
  • 相关阅读:
    关于链表的代码
    c++中的友元函数
    javaweb笔记全套
    包装类、object、单例模式、final、抽象类
    Linux变量内容的删除、代替与替换
    2014年工作中遇到的20个问题:181-200
    Qt中 QString 和int,double等的转换
    jsp学习笔记总结
    工作日志2014-07-04
    Maple入门使用教程
  • 原文地址:https://www.cnblogs.com/masterSoul/p/7790013.html
Copyright © 2020-2023  润新知