zoukankan      html  css  js  c++  java
  • 线性表之链表操作

      1 package cn.mdj.singlelink;
      2 
      3 class Link{
      4     //链表是由节点组成,所以定义一个Node节点
      5     private class Node{
      6         //节点的数据分为 数据域 和指针域 所以: 每一个指针域指向下一个节点,而数据域是可以存放任何数据类型
      7         private Object data;
      8         private Node next;
      9         //数据传入,即节点实例化后,就要将数据封装起来
     10         public Node(Object data){
     11             this.data = data;
     12         }
     13         
     14         public void addNode(Node newNode){
     15             if(this.next == null){
     16                 this.next = newNode;
     17             }else{
     18                 this.next.addNode(newNode);
     19             }
     20         }
     21         
     22         public boolean containsNode(Object data){
     23             if(this.data.equals(data)){
     24                 return true;
     25             }else{
     26                 if(this.next == null){
     27                     return false;
     28                 }else{
     29                     return  this.next.containsNode(data);
     30                 }
     31             }
     32         }
     33         
     34         public void removeNode(Node pre,Object data){
     35             if(data.equals(this.data)){
     36                 pre.next = this.next;
     37             }else{
     38                 if(this.next != null){
     39                     this.next.removeNode(this, data);
     40                 }
     41             }
     42         }
     43         
     44         public Object getNode(int index){
     45             if(Link.this.foot == index){
     46                 return this.data;
     47             }else{
     48                 Link.this.foot++;
     49                 return this.next.getNode(index);
     50             }
     51         }
     52         
     53         public void toArrayNode(){
     54             Link.this.restData[Link.this.foot++] = this.data;
     55             if(this.next != null){
     56                 this.next.toArrayNode();
     57             }
     58         }
     59     }
     60     
     61     //节点定义好后,那么首先需要的是一个根节点
     62     private Node root;
     63     private int  count = 0;
     64     private int foot =0;
     65     private Object[] restData = null;
     66     //添加节点
     67     public boolean add(Object data){
     68         //将数据包装成节点
     69         Node newNode = new Node(data);
     70         if(this.root == null){
     71             this.root = newNode;
     72         }else{
     73             this.root.addNode(newNode);
     74         }
     75         this.count++;
     76         return true;
     77     }
     78     
     79     //既然可以添加节点,当然是可以删除节点的,但要删除,那么首先肯定要查找是否包含这个节点,所以:
     80     public boolean contains(Object data){
     81         if(this.root == null || data == null){
     82             return false;
     83         }
     84         return this.root.containsNode(data);
     85     }
     86     
     87     //有了节点的查找,下面来删除节点
     88     public void remove(Object data){
     89         //首先查找,是否存在
     90         if(this.contains(data)){
     91             //首先判断是否是根节点
     92             if(this.root.data.equals(data)){
     93                 this.root = this.root.next;
     94                 
     95             }else{
     96                 this.root.next.removeNode(this.root,data);
     97             }
     98         }
     99         this.count--;
    100     }
    101     
    102     //有了增加 删除 查找,当然应该有获取指定的对象,和判断是否为空链表
    103     
    104     //获取
    105     public Object get(int index){
    106         
    107         //而要获取指定位置的索引,索引要合法,即不能超过链表的总数,所以在上面定义一个count
    108         if(this.count > index){
    109             //需要一个角标,从而来去和index,判断所以定义一个foot
    110             this.foot = 0;
    111             return this.root.getNode(index);
    112         }else{
    113             return null;
    114         }
    115     }
    116     
    117     public boolean emptyLink(){
    118         return this.count == 0;
    119     }
    120     
    121     public int size(){
    122         return this.count;
    123     }
    124     
    125     //对象数组输出
    126     public Object[] toArray(){
    127         if(this.root == null){
    128             return null;
    129         }
    130         this.foot = 0;
    131         this.restData = new Object[this.count];
    132         this.root.toArrayNode();
    133         return this.restData;
    134     }
    135     
    136 }
    137 
    138 
    139 
    140 public class SingleLink {
    141 
    142     /**
    143      * @param args
    144      */
    145     public static void main(String[] args) {
    146         Link all = new Link() ;
    147         all.add("Hello") ;
    148         all.add("World") ;
    149         all.remove("World") ;
    150         Object array [] = all.toArray() ;    // 取得全部数据
    151         for (int x = 0 ; x < array.length ; x ++ ) {
    152             System.out.println(array[x]) ;
    153         }
    154         System.out.println(all.contains("Hello")) ;
    155         System.out.println(all.get(0)) ;
    156         System.out.println(all.get(1)) ;
    157     }
    158 }
  • 相关阅读:
    步步为营——算法初阶 1.算法概述
    Spring学习【Spring概述】
    Codeforces 490F Treeland Tour 树上的最长上升子序列
    汽车电商竞争白热化,“五大门派”谁能登顶?
    程序猿的出路
    Android Studio Share Project On Github
    hdu 1022 Train Problem I(栈的应用+STL)
    Scala, Groovy, Clojure, Jython, JRuby and Java ----我们的工作语言
    Oracle GoldenGate (ogg) 11.2.1.0.20 是最后一个支持oracle db 10g的 ogg版本号
    javascript异步代码的回调地狱以及JQuery.deferred提供的promise解决方式
  • 原文地址:https://www.cnblogs.com/pony1223/p/2633795.html
Copyright © 2011-2022 走看看