java实现单链表

---恢复内容开始---

单链表上的基本运算

2016-06-17

对于单链表,系统建立单链表过程,就是不断添加节点的过程。动态建立单链表有两种方式。

1:头插法建表

2:尾插法建表

常用的操作有:查找,插入,删除

1:查找:按序号查找第index个节点

    在链表中查找指定的element元素

2:插入:首先找到索引为index-1的节点,然后生成一个新节点newNode,另index-1的next引用新节点,新节点的next域执行原来index节点

3:删除:首先找到索引为index-1的节点,让index-1的next引用原来的index+1节点,释放index节点

 

  1 package consume;
  2 
  3 import java.io.ObjectInputStream.GetField;
  4 
  5 public class LinkList<T> {
  6     private class Node{
  7         private T element;//数据域
  8         private Node next;//执行下一个节点的引用
  9         public Node(){//无参构造方法
 10             
 11         }
 12         public Node(T element,Node next){//有参数的构造方法
 13             this.element=element;
 14             this.next=next;
 15         }
 16     }
 17     private int size;//保存链表一有的节点数
 18     private Node header;//头结点
 19     private Node tail;//尾节点
 20     public LinkList(){//链表的构造方法
 21         header=null;
 22         tail=null;
 23         size=0;
 24     }
 25     public LinkList(T element){//构造头结点的构造方法
 26         header=new Node(element,null);
 27         tail=header;
 28         size++;
 29     }
 30     //返回链表的长度
 31     public int length(){
 32         return size;
 33     }
 34     //返回指定位置的节点
 35     public Node getNode(int index){
 36         if(index<0||index>size-1){
 37             throw new IndexOutOfBoundsException("索引越界");
 38         }
 39         Node current=header;//新建一个节点为header
 40         for(int i=0;i<size&&current!=null;i++,current=current.next){
 41             if(i==index){
 42                 return current;
 43             }
 44         }
 45         return  null;
 46     }
 47     //返回指定位置节点的数据
 48     public T get(int index){
 49         return getNode(index).element;
 50     }
 51     //查找指定元素的索引
 52     public int locate(T date){
 53         Node current=header;
 54         for(int i=0;i<size&&current!=null;i++,current=current.next){
 55             if(current.element.equals(date)){
 56                 return i;
 57             }
 58         }
 59         return -1;
 60     }
 61     //向指定位置插入节点
 62     public void insert(T data,int index){
 63         if(index<0||index>size){
 64             throw new IndexOutOfBoundsException("索引越界");
 65         }
 66         if(header==null){//为空链表时,用尾插法插入
 67             add(data);
 68         }
 69         else{
 70             if(index==0){//当位置为0时,为头插法
 71                 addAtHeaer(data);
 72             }else{
 73                 Node prev=getNode(index-1);//获取前驱节点
 74                 prev.next=new Node(data,prev.next);//前驱节点的next指向新节点,新节点的next指向原来prev的next
 75             }
 76         }
 77         size++;
 78         
 79     }
 80     //采用尾插法为链表添加新节点
 81     public void add(T data){
 82         //如果该链表还是空链表
 83         if(header==null){
 84             header=new Node(data,null);
 85             tail=header;
 86         }else{
 87             Node newNode=new Node(data,null);
 88             tail.next=newNode;//为链表的next指向新节点
 89             tail=newNode;//更新尾节点
 90         }
 91         size++;
 92     }
 93     //采用头插法为链表添加新节点
 94     public void addAtHeaer(T data){
 95         header=new Node(data,header);
 96         if(tail==null){
 97             tail=header;
 98         }
 99         size++;
100     }
101     //删除链表指定位置的元素
102     public T delete(int index){
103         if(index<0||index>size-1){
104             throw new IndexOutOfBoundsException("索引越界");
105         }
106         Node del=null;
107         if(index==0){
108             del=header;
109             header=header.next;
110         }
111         else{
112             Node prev=getNode(index-1);
113             del=prev.next;
114             prev.next=del.next;
115             del.next=null;
116         }
117         size--;
118         return del.element;
119     }
120     //删除链表最后一个元素
121     public T remove(){
122         return delete(size-1);
123     }
124     //判断链表是否为空
125     public boolean empty(){
126         return size==0;
127     }
128     //清空链表
129     public void clear(){
130         header=null;
131         tail=null;
132         size=0;
133     }
134     public static void main(String[] args) {
135         LinkList<String> list=new LinkList<String>();
136         list.insert("a", 0);
137         list.add("b");
138         list.add("c");
139         System.out.println(list.getNode(1).element);
140     }
141 }

 

---恢复内容结束---

posted @ 2016-06-17 14:53  哈哈好梦  阅读(80)  评论(0)    收藏  举报