java实现单链表

接下来,我们开始学习java实现单链表。

 

package base.structure;

import java.util.Iterator;
import java.util.NoSuchElementException;

/**
 * @program: Algorithm4J
 * @description: 单链表的实现
 * @author: Mr.Dai
 * @create: 2018-12-07 19:35
 **/
public class LinkedList implements Iterable{

    /**
     * 链表节点
     * @param 
     */
    private class Node{
        T data;
        Node next;

        Node(){
            data=null;
            next=null;
        }
    }

    // 维护链表size
    transient  int n;

    transient Node head;

    /**
     * 初始化一个链表
     */
    public LinkedList(){
        n=0;
        head=new Node<>();
        head.next=null;
        head.data=null;
    }

    /**
     * 尾插法加入一个节点
     * @param item
     */
   public void add(T item){
        Node node=new Node<>();
        if(head.next==null){
            node.data=item;
            head.next=node;
        }else{
            // 临时节点获取头结点指向
            Node temp=head;
            while (temp.next!=null){
                temp=temp.next;
            }
            node.data=item;
            temp.next=node;
        }
        n++;
    }

    /**
     * 头插法加入节点
     * @return
     */
    public void addFirst(T item){
        Node node=new Node<>();
        if(head.next==null){
            node.data=item;
            head.next=node;
        }else{
            Node tempnode=head.next;
            node.data=item;
            head.next=node;
            node.next=tempnode;
        }
        n++;
    }

    /**
     * 获取元素
     * @return
     */
    public T get(int i){
        if(i<0||i>n) throw new NoSuchElementException();
        if(i==0)return head.next.data;
        int index=0;
        Node tempnode=head;
        for (int j = index; j < i+1; j++) {
            tempnode=tempnode.next;
        }
        return tempnode.data;
    }

    /**
     * 删除元素
     * @return
     */
    public void delelte(T item){
        // 临时节点指向
       Node tempnode=head;

       while (tempnode.next!=null){

           if(tempnode.next.data.equals(item)){
               tempnode.next=tempnode.next.next;
               break;
           }
           tempnode=tempnode.next;
       }
       n--;
    }

    public int Size(){return n; }

    public boolean isEmpty(){return n==0;}


    @Override
    public Iterator iterator() {
        return new LinkedListIterator(head);
    }

    private class LinkedListIterator implements Iterator{

        // 维护一个内部的指向
        private Node current;

        public LinkedListIterator(Node current) {
            this.current = current.next;
        }

        @Override
        public boolean hasNext() {
            return current!=null;
        }

        @Override
        public T next() {
            T t = current.data;
            current=current.next;
            return t;
        }
    }

}

java测试类

package Test.base.structure;

import base.structure.LinkedList;


public class LinkedListTest {

    public static void main(String[] args) {
        LinkedList linkedList = new LinkedList<>();

        linkedList.addFirst("a");
        linkedList.addFirst("b");
        linkedList.addFirst("c");

        linkedList.addFirst("d");

        linkedList.add("aa");
        linkedList.add("aa");
        for (String s : linkedList) {
            System.out.println(s);
        }
        System.out.println(linkedList.get(1));

        linkedList.delelte("a");
        for (String s : linkedList) {
            System.out.println(s);
        }
    }
}

 

你可能感兴趣的:(java实现单链表)