《Java数据结构与算法》笔记-CH5-链表-2单链表,增加根据关键字查找和删除

  1 /**
  2  * Link节点 有数据项和next指向下一个Link引用
  3  */
  4 class Link {
  5     private int iData;// 数据
  6     private double dData;// 数据
  7     public Link next;// 下一个link节点的引用
  8 
  9     public int getiData() {
 10         return iData;
 11     }
 12 
 13     public Link(int i, double d) {
 14         iData = i;
 15         dData = d;
 16     }
 17 
 18     public String toString() {
 19         return "[" + iData + "," + dData + "]";
 20     }
 21 
 22     public void displayLink() {
 23         System.out.println(toString());
 24     }
 25 }
 26 
 27 /**
 28  * 链表类 维护一个头节点
 29  */
 30 class LinkList {
 31     private Link first;
 32 
 33     /**
 34      * 初始化的时候头结点置空
 35      */
 36     public LinkList() {
 37         first = null;
 38     }
 39 
 40     /**
 41      * 头结点是空代表链表为空
 42      * 
 43      * @return
 44      */
 45     public boolean isEmpty() {
 46         return first == null;
 47     }
 48 
 49     /**
 50      * 向链表头插入Link节点
 51      * 
 52      * @param link
 53      */
 54     public void insertFirst(Link link) {
 55         link.next = first;// 要插入的节点的next指针指向链表的头节点
 56         first = link;// 链表的头结点指向被插入的节点
 57     }
 58 
 59     /**
 60      * 从链表头删除头结点并返回
 61      * 
 62      * @return
 63      */
 64     public Link deleteFirst() {
 65         Link temp = first;// temp临时存上头结点
 66         first = first.next;// 将头节点的next指针指向下一个节点
 67         return temp;// 返回temp
 68     }
 69 
 70     @Override
 71     public String toString() {
 72         if (isEmpty())
 73             return "{}";
 74         StringBuilder sb = new StringBuilder();
 75         sb.append("{");
 76         Link current = first;
 77         while (current != null) {
 78             sb.append(current.toString());
 79             sb.append(",");
 80             current = current.next;
 81         }
 82         sb.deleteCharAt(sb.length() - 1);
 83         sb.append("}");
 84         return sb.toString();
 85     }
 86 
 87     /**
 88      * 根据key寻找节点
 89      * 
 90      * @param key
 91      * @return
 92      */
 93     public Link find(int key) {
 94         Link curr = first;
 95         while (curr.getiData() != key) {
 96             if (curr.next == null)
 97                 return null;
 98             else
 99                 curr = curr.next;
100         }
101         return curr;
102     }
103 
104     /**
105      * 根据key值删除节点
106      * 
107      * @param key
108      * @return
109      */
110     public Link deleteByKey(int key) {
111         Link current = first;
112         Link previous = first;
113         // 根据key值遍历
114         while (current.getiData() != key) {
115             if (current.next == null)//找不到,返回null
116                 return null;
117             else {
118                 previous = current;
119                 current = current.next;
120             }
121         }
122         //找到,此时头结点就是要删除的节点,直接把头结点置为头结点的next节点
123         if (current == first)
124             first = first.next;
125         else//找到,此时把前一个节点的next指向当前节点的next即可
126             previous.next = current.next;
127         return current;
128     }
129 
130     public void displayList() {
131         System.out.println(toString());
132     }
133 }
134 
135 public class LinkListDemo {
136     public static void main(String[] args) {
137         LinkList ll = new LinkList();
138         for (int i = 0; i < 10; i++) {
139             Link l = new Link(i, i + 11.23);
140             ll.insertFirst(l);
141             ll.displayList();
142         }
143         ll.deleteFirst();
144         ll.deleteFirst();
145         ll.displayList();
146         Link l3 = ll.find(3);
147         System.out.println("key 为3的节点是: "+l3.toString());
148         ll.deleteByKey(3);
149         System.out.print("删除该节点后,链表是:");
150         ll.displayList();
151     }
152 }
原文地址:https://www.cnblogs.com/fstack/p/5617254.html