zoukankan      html  css  js  c++  java
  • 链表

    链表是动态的进行存储分配的一种结构。

    我们知道,用数组存放数据时,必须事先定义固定的长度(即元素个数)。如果事先难以确定元素个数,则必须把数组定义的足够大,以便存放,显然这样会浪费内存。
    而链表可根据需要开辟内存单元,不会浪费内存。
    链表有一个“头指针”变量,它存放一个地址,该地址指向一个元素,这个元素又指向下一个元素......直到最后一个元素,该元素不再指向其他元素,称为“表尾”,链表到此结束。

    单向链表:

    如果想要实现链表,肯定要设计一个链表的节点类,此类中必须有一个属性保存下一个节点的引用地址

    示例:具有增删查功能

      1 package com.matto.linkedList;
      2 
      3 /**
      4  * Created by matto on 6/17/16.
      5  */
      6 class Link {
      7 
      8     class Node {                            //节点类,因为操作对象是节点所以创建
      9         private String data;
     10         private Node next;
     11 
     12         public Node(String data) {
     13             this.data = data;
     14         }
     15 
     16         public void add(Node newNode) {     //定义添加函数
     17             if (this.next == null) {        //如果调用的实例的下一个节点为空
     18                 this.next = newNode;        //那么就把新节点设置给它
     19             } else {                        //如果下一个节点不为空
     20                 this.next.add(newNode);     //那么递归调用添加函数向后添加
     21             }
     22         }
     23 
     24         public void print() {               //定义打印函数
     25             System.out.println(this.data + "	");
     26             if (this.next != null) {        //如果调用的示例的下一个不为空
     27                 this.next.print();          //那么递归调用打印函数继续打印
     28             }
     29         }
     30 
     31         public boolean search(String data){ //内部的查找方法
     32             if(this.data.equals(data)){     //根据data判断当前节点是否存在
     33                 return true;
     34             }else {
     35                 if(this.next != null){      //如果调用search函数的实例的下一个节点不为空
     36                     return this.next.search(data);  //递归调用此查询函数继续查询
     37                 }else{                              //否则返回false,即没有查到
     38                     return false;
     39                 }
     40             }
     41         }
     42 
     43         public void delete(Node previous ,String data){ //删除函数
     44             if(data.equals(this.data)){                 //判断当前节点和传进来的节点是否是一个
     45                 previous.next = this.next;              //是的话,将当前节点的下一个节点的引用地址给当前节点的上一个节点
     46             }else{
     47                 if(this.next != null){                  //判断是否还有下一个节点
     48                     this.next.delete(this,data);        //不是的话,递归调用函数直到当前节点和传进来的节点是一个为止
     49                 }
     50             }
     51         }
     52     }
     53 
     54     private Node root;                      //链表类的根节点属性
     55 
     56     public void addNode(String data) {      //链表类的添加节点函数
     57         Node newNode = new Node(data);      //使用节点类创建一个新节点
     58         if (this.root != null) {            //如果根节点不为空
     59             this.root.add(newNode);         //递归调用添加节点函数向后添加节点
     60         } else {                            //如果根节点为空
     61             this.root = newNode;            //将新节点设置为根节点
     62         }
     63     }
     64 
     65     public void printNode() {               //链表类的打印函数
     66         if (this.root != null) {            //如果根节点不为空
     67             this.root.print();              //递归调用节点类的输出函数进行输出
     68         }
     69     }
     70 
     71     public boolean contains(String name){     //查找节点函数
     72         return this.root.search(name);        //调用内部类的查找函数
     73     }
     74 
     75     public void deleteNode(String data){      //链表类的删除函数
     76         if(this.contains(data)){              //判断要删的节点是否存在
     77             if(this.root.data.equals(data)){    //判断要删的是否是根节点
     78                 this.root = this.root.next;     //将根节点的下一个节点设置为根节点
     79             }else{
     80                 this.root.delete(root,data);    //执行节点类的delete函数,传入根节点和要删除的节点
     81             }
     82         }
     83     }
     84 }
     85 
     86 public class LinkDemo02 {
     87 
     88     public static void main(String[] args) {
     89         Link l = new Link();                //创建一个link实例
     90         l.addNode("A");                     //使用Link类的添加节点函数进行添加
     91         l.addNode("B");
     92         l.addNode("C");
     93         l.addNode("D");
     94         l.addNode("E");
     95 
     96         //l.printNode();                      //使用Link类的打印函数进行节点输出
     97         //System.out.println(l.contains("A"));//使用Link类的实例调用contains方法判断名字为f 的节点是否存在
     98         l.deleteNode("A");                      //删除节点A
     99         l.printNode();
    100 
    101     }
    102 }
  • 相关阅读:
    nginx-1.8.1的安装
    ElasticSearch 在3节点集群的启动
    The type java.lang.CharSequence cannot be resolved. It is indirectly referenced from required .class files
    sqoop导入导出对mysql再带数据库test能跑通用户自己建立的数据库则不行
    LeetCode 501. Find Mode in Binary Search Tree (找到二叉搜索树的众数)
    LeetCode 437. Path Sum III (路径之和之三)
    LeetCode 404. Sum of Left Leaves (左子叶之和)
    LeetCode 257. Binary Tree Paths (二叉树路径)
    LeetCode Questions List (LeetCode 问题列表)- Java Solutions
    LeetCode 561. Array Partition I (数组分隔之一)
  • 原文地址:https://www.cnblogs.com/blog4matto/p/5594714.html
Copyright © 2011-2022 走看看