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<T> implements Iterable<T>{

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

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

    // 维护链表size
    transient  int n;

    transient Node<T> head;

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

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

    /**
     * 头插法加入节点
     * @return
     */
    public void addFirst(T item){
        Node<T> node=new Node<>();
        if(head.next==null){
            node.data=item;
            head.next=node;
        }else{
            Node<T> 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<T> tempnode=head;
        for (int j = index; j < i+1; j++) {
            tempnode=tempnode.next;
        }
        return tempnode.data;
    }

    /**
     * 删除元素
     * @return
     */
    public void delelte(T item){
        // 临时节点指向
       Node<T> 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<T> iterator() {
        return new LinkedListIterator(head);
    }

    private class LinkedListIterator implements Iterator<T>{

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

        public LinkedListIterator(Node<T> 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<String> 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);
        }
    }
}
原文地址:https://www.cnblogs.com/dgwblog/p/10085050.html