zoukankan      html  css  js  c++  java
  • 单链表的基本操作

     1 public class Node{
     2     public long data;
     3     public Node next;
     4     
     5     public Node(long value){
     6         this.data = value;
     7     }
     8     
     9     public void display(){
    10         System.out.println(data+"");
    11     }
    12 }
    13 //链表操作
    14 public class LinkList{
    15     private Node first;
    16     public LinkList(){
    17         first = null;
    18     }
    19     public void insertFirst(long value){
    20         Node node = new Node(value);
    21         if(first == null){
    22             first=node;
    23         }
    24         node.next = first.next;
    25         first = node;
    26     }
    27     public Node deleteFirst(){
    28         Node tmp = first;
    29         first = tmp.next;
    30         return tmp;
    31     }
    32     public void show(){
    33         Node current = first;
    34         while(current!= null){
    35             current.display();
    36             current = current.next;
    37         }
    38     }
    39     public Node find(long value){
    40         Node current = first;
    41         while(current.data!=value){
    42             if(current.next==null){
    43                 return null;
    44             }
    45             current=current.next;
    46         }
    47         return current;
    48     }
    49     public Node delete(long value){
    50         Node current = first;
    51         Node previous = first;
    52         while(current.data!=value){
    53             if(current.next==null){
    54                 return null;
    55             }
    56             previos = current;
    57             current = current.next;
    58         }
    59         if(current == first){
    60             first = first.next;
    61         }else{
    62             previous.next = current.next;
    63         }
    64         return current;
    65     }
    66 }
  • 相关阅读:
    练习四十:数组逆向输出
    练习三十九:数组插入排序
    Java awt组件间的继承关系
    java ArrayList
    java Scanner中的hasNext()方法
    java中String对象的存储位置
    java初始化块执行顺序
    java二维数组
    java数组变量
    Java数组初始
  • 原文地址:https://www.cnblogs.com/fxust/p/4547492.html
Copyright © 2011-2022 走看看