JAVA实现单链表

来源:互联网 发布:js undefend 判断 编辑:程序博客网 时间:2024/06/07 10:27

节点

package com.ghg.data_structure.sit;/** * 节点 * @author Administrator * */public class Node {    /**     * 数据     */    public int data;    /**     * 下一个元素     */    public Node next;    public Node(int data, Node next) {        super();        this.data = data;        this.next = next;    }}

链表

package com.ghg.data_structure.sit;import com.ghg.data_structure.singlinktab.Node;import sun.reflect.generics.tree.VoidDescriptor;public class LinkList {    public Node first;     public LinkList() {           this. first = null;     }       /**      * 添加元素到第个位置      * @param node      */     public void addFirst(Node node){         node.next=first;         first=node;     }     /**      * 删除第一个元素      */     public void deleteFirst(){         first=first.next;     }     public void addNode(Node node){         //一个指针         Node current =first;         /**          * 找到最后个元素          */         while(current.next!=null){             current=current.next;         }         //添加到最后         current.next=node;     }     /**      * 插入指定位置      * @param index      * @param node      */     public void addNodeByIndex(int index,Node node){         if(index<1 || index >length()+1){             System.out.println(index+ " 位置不正确 length  "+length());             return;         }         /*          * 记录我们遍历到第几个结点了,也就是记录位置  要把index添加到length后面          */         int length=1;         /**          * 指针          */         Node current=first;         while(current.next!=null){             /**              * 判断是否到达指定位置。              */             if(index==length++){                 //插入操作。  把原来的下一个赋值给新的节点                 node.next=current.next;                 current.next=node;                 return;             }             current=current.next;         }     }     /**      * 删除节点      */     public void delNodeByIndex(int index){         if(index<1 || index >length()+1){             System.out.println(index+ " 位置不正确 length  "+length());             return;         }         /*          * 记录我们遍历到第几个结点了,也就是记录位置  要把index添加到length后面          */         int length=1;         /**          * 指针          */         Node current=first;         while(current.next!=null){             if(index==length++){                 /**                  * 当前=下一个一下一个                  */                 current.next=current.next.next;                 return;             }             current=current.next;         }     }     /**      * 获取链表的长度      */     public int length(){         int length=1;         Node current =first;         /**          * 找到最后个元素          */         while(current.next!=null){             length++;             current=current.next;         }         return length;     }     /**      * 显示      */     public void display(){         Node current = first;           while (current != null) {              current.display();              current = current. next;          }          System. out.println();      }}
原创粉丝点击