链表

  链表 组成元素:由多个链接点组成,每个链接点是一个类的对象,里面装有需要的数据。并且还有对象自身的引用,用于定位下一个节点,或者加一个自身的引用定位上一个节点。

  下面展示一个单链表也即只能往前或者往后遍历链表的使用。

  1 package com.test;
  2 
  3 public class LinkListApp {
  4 
  5     public static void main(String[] args) {
  6         
  7         LinkList linkList = new LinkList();
  8         linkList.insertFirst(1, 1.0);
  9         linkList.insertFirst(2, 2.0);
 10         linkList.insertFirst(3, 3.0);
 11         linkList.insertFirst(4, 4.0);
 12         
 13         linkList.displayList();
 14         
 15         System.out.println("删除末端节点:"+linkList.deleteFirst()); 
 16         System.out.println("根据关键字查找:"+linkList.find(2));
 17         System.out.println("根据关键字删除:"+linkList.delete(2));
 18         
 19         
 20     }
 21 }
 22 
 23 
 24 /**
 25  * 链表对象
 26  * @author jingxin
 27  *
 28  */
 29 class Link{
 30     /**
 31      * 链表对象里的数据(iData,dData)
 32      * 链表对象的下一个引用 next
 33      */
 34     public int iData;
 35     public double dData;
 36     public Link next;
 37     
 38     public Link(int id,double dd){  // 构造函数
 39         this.iData = id;
 40         this.dData = dd;
 41     }
 42     
 43     public void displayLink(){  // 显示当前链表元素
 44         System.out.print("{"+iData+","+dData+"}");
 45     }
 46 
 47     @Override
 48     public String toString() {
 49         return "Link [iData=" + iData + ", dData=" + dData + "]";
 50     }
 51     
 52 }
 53 
 54 /**
 55  * 单向链表
 56  * @author jingxin
 57  *
 58  */
 59 class LinkList{
 60     /**
 61      * 链表中第一个链接点的引用,从first出发可以通过Link对象的next指向下一个链接点
 62      * first:也即当前节点
 63      */
 64     private Link first;
 65     
 66     public LinkList(){  // 构造函数
 67         first = null;
 68     }
 69     
 70     public boolean isEmpty(){
 71         return first == null;
 72     }
 73     
 74     /**
 75      * 添加节点
 76      * 因为初始first为空,新插入的节点,给first,然后该节点的next指向空
 77      * @param id
 78      * @param dd
 79      */
 80     public void insertFirst(int id,double dd){
 81         Link newlink = new Link(id, dd);
 82         newlink.next = first;
 83         first = newlink;
 84     }
 85     
 86     /**
 87      * 删除节点
 88      * 返回删除操作针对的节点
 89      * 将该节点的下一个节点指向当前节点,改变引用,达到访问不到要删除节点的目的
 90      * @return
 91      */
 92     public Link deleteFirst(){
 93         if(isEmpty()){
 94             return null;
 95         }
 96         Link oldLink = first;
 97         first = first.next;
 98         return oldLink;
 99     }
100     
101     /**
102      * 查找节点
103      * @param key
104      * @return
105      */
106     public Link find(int key){
107         if(!isEmpty()){
108             Link current = first;
109             while(current.iData!=key){
110                 if(current.next == null){
111                     return null;
112                 }
113                 else{
114                     current = current.next;
115                 }
116             }
117             // 找到节点了
118             return current;
119         }
120         return null;
121     }
122     
123     /**
124      * 根据关键字删除节点
125      * @param key
126      */
127     public Link delete(int key){
128         if(!isEmpty()){
129             // 当前节点
130             Link current = first;
131             // 前一个节点
132             Link previous = first;
133             while(current.iData!=key){
134                 if(current.next==null){
135                     return null;
136                 }else{
137                     previous = current;
138                     current = current.next;
139                 }
140             }
141             // 找到了
142             if(current == first){  // 删除的是第一个节点
143                 first = first.next;
144             }else{
145                  previous.next = current.next;
146             }
147             return current;
148         }
149         return null;
150     }
151     
152     /**
153      * 显示整个链表
154      */
155     public void displayList(){
156         Link current = first;
157         while(current!=null){
158             current.displayLink();
159             current = current.next;
160         }
161         System.out.println("");
162     }
163     
164 }

 

posted @ 2017-05-13 16:45  x_jingxin  阅读(213)  评论(0编辑  收藏  举报