zoukankan      html  css  js  c++  java
  • java实现链表指定值得删除,查找第二部分代码

      1 package com.liu.Link;
      2 
      3 public class LinkList1 {
      4     private Link first;
      5     public LinkList1()
      6     {
      7         first = null;
      8     }
      9     
     10     public void insertFirst(int id,double dd)
     11     {
     12         Link newLink = new Link(id,dd);
     13         newLink.next = first;
     14         first = newLink;
     15     }
     16     
     17     public Link find(int key)
     18     {
     19         Link current = first;
     20         while(current.iData!=key)
     21         {
     22             if(current.next == null)
     23                 return null;
     24             else
     25                 current = current.next;
     26         }
     27         return current;
     28     }
     29     
     30     public Link delete(int key)
     31     {
     32         Link current = first;
     33         Link previous = first;
     34         while(current.iData != key)
     35         {
     36             if(current.next == null)
     37                 return null;
     38             else
     39             {
     40                 previous = current;
     41                 current = current.next;
     42             }
     43         }
     44         //情况1,删除的数字就是first指针指向的位置
     45         if(current == first)
     46         {
     47             first = first.next;
     48         }
     49         //情况2,删除的数字是中间位置
     50         else
     51         {
     52             previous.next = current.next;
     53         }
     54         //current就是previous.next
     55         return current;
     56     }
     57     
     58     public void displayList()
     59     {
     60         System.out.print("List (first-->last):");
     61         Link current = first;
     62         while(current != null)
     63         {
     64             current.displayLink();
     65             current = current.next;
     66         }
     67         System.out.println("");
     68     }
     69 }
     70 
     71 //链表数据类
     72 class Link
     73 {
     74     public int iData;
     75     public double dData;
     76     public Link next;
     77     
     78     public Link(int id,double dd)
     79     {
     80         iData = id;
     81         dData = dd;
     82     }
     83     
     84     public void displayLink()
     85     {
     86         System.out.print("{"+ iData +", "+ dData +"}");
     87     }
     88     
     89 }
     90 
     91 class LinkListApp
     92 {
     93     public static void main(String args[])
     94     {
     95         LinkList1 theList = new LinkList1();
     96         theList.insertFirst(22, 2.99);
     97         theList.insertFirst(44, 4.99);
     98         theList.insertFirst(66, 6.99);
     99         theList.insertFirst(88, 8.99);
    100         theList.insertFirst(33, 3.99);
    101         
    102         theList.displayList();
    103         
    104         Link f = theList.find(44);
    105         if(f!=null)
    106             System.out.println("Fount link with key "+f.iData);
    107         else
    108             System.out.println("can't find link");
    109         
    110         Link d = theList.delete(66);
    111         if( d != null )
    112              System.out.println("Deleted link with key " + d.iData);
    113           else
    114              System.out.println("Can't delete link");
    115         
    116         theList.displayList();
    117     }
    118 }
  • 相关阅读:
    微信小程序购物商城系统开发系列-目录结构
    微信小程序购物商城系统开发系列-工具篇
    如何用js获取浏览器URL中查询字符串的参数
    Search for a Range
    Search in Rotated Sorted Array
    permutation递归方法
    Permutations
    next permutation
    4Sum
    3Sum Closest
  • 原文地址:https://www.cnblogs.com/speaklessdomore/p/3672479.html
Copyright © 2011-2022 走看看