链表__JAVA版

0. 链表 Linked List

  • 数据存储在”节点”(Node) 中
class Node{
    E  e;
    Node  next;
}
  • 优点: 真正的动态,不需要处理数组固定容量的问题
  • 缺点: 丧失了随机访问能力,只能从依次顺序访问

1. 创建节点

  • 内部类的形式创建Node节点
/**
 * Created by Enzo Cotter on 2018/7/12.
 */
public class LinkedList<E> {
    private class Node{
         public E e;
         public Node next;

         public Node(E e,Node next){
             this.e = e;
             this.next = next;
         }

         public Node(E e){
             this(e,null);
         }

         public Node(){
             this(null ,null);
         }

        @Override
        public String toString() {
            return e.toString();
        }
    }
    private Node head;
    private int size;

    public LinkedList(){
        head = null;
        size = 0;
    }

    //获取链表中的元素个数
    public int getSize(){
        return size;
    }
    //返回链表是否为空
    public boolean isEmpty(){
        return size == 0;
    }
}

2. 添加元素

  • 头节点指向链表中的第一个节点
//为链表头添加元素e
    public void addFirst(E e){
//        Node node = new Node(e);  //申请一个元素为e的节点,这里暂不考虑创建失败
//        node.next = head;
//        head = node;
        head = new Node(e,head);  //功能同上面注释的三句
        size++;
    }
    //在链表中间添加元素e
    //在链表的index(0-based)位置添加新的元素e,此操作不常用
    public void add(int index,E e){
        if (index <0 || index > size)
            throw new IllegalArgumentException("Add failed, Illegal index.");
        if(index == 0)
            addFirst(e);
        else{
            Node prev = head;
            for (int i = 0; i < index -1 ; i ++)
                prev = prev.next;
            prev.next = new Node(e,prev.next);
            size++;
        }
    }
    //在链表末尾添加新的元素e
 public void addLast(E e){
        add(size,e);
    }
  • 为链表设立虚拟头节点 dummyHead
 //在链表中间添加元素e
    //在链表的index(0-based)位置添加新的元素e,此操作不常用
public void add(int index,E e){
        if (index <0 || index > size)
            throw new IllegalArgumentException("Add failed, Illegal index.");

            Node prev = dummyhead;
            for (int i = 0; i < index ; i ++)
                prev = prev.next;
            prev.next = new Node(e,prev.next);
            size++;

    }
    //为链表头添加元素e
public void addFirst(E e){
        add(0,e);
    }
    //在链表末尾添加新的元素e
public void addLast(E e){
        add(size,e);
    }

3. 查找和修改

  • 平均时间复杂度O(N);
//获得链表的第index(0-based)个位置的元素
 public E get(int index){
        if (index <0 || index >= size)
            throw new IllegalArgumentException("Get failed, Illegal index.");
        Node cur = dummyhead.next;
        for (int i = 0; i < index ; i++)
            cur = cur.next;
        return cur.e;
    }
    //获得链表的第一个元素
    public E getFirst(){
        return get(0);
    }
    //获得链表的最后一个元素
    public E getLast(){
        return get(size - 1);
    }

    //修改链表的第index(0-based)个位置的元素为e
public void set(int index,E e){
        if (index <0 || index >= size)
            throw new IllegalArgumentException("Set failed, Illegal index.");

        Node cur = dummyhead.next;
        for (int i = 0; i < index ; i++)
            cur = cur.next;
        cur.e = e;
    }
    // 查找链表中是否有元素e
public boolean contains(E e){
        Node cur = dummyhead.next;
        while(cur != null){
            if(cur.e.equals(e))
                return true;
            cur = cur.next;
        }
        return false;
    }

 @Override
 public String toString() {
        StringBuilder res = new StringBuilder();
        Node cur = dummyhead.next;
        while (cur != null){
            res.append(cur + "->");
            cur = cur.next;
        }
        res.append("null");
        return res.toString();
    }

4. 链表中删除元素

  • 平均时间复杂度O(N)
// 从链表中删除index(0-based)位置的元素,返回删除的元素
public E remove( int index){
    if (index <0 || index >= size)
        throw new IllegalArgumentException("Remove failed, Illegal index.");
    Node prev = dummyhead;
    for (int i = 0 ; i < index ; i++)
        prev = prev.next;

    Node retNode = prev.next;
    prev.next = retNode.next;
    retNode.next = null;
    size --;
    return retNode.e;
}
//删除第一个元素,返回删除的元素
public E removeFirst(){
    return remove(0);
}
//删除最后一个元素,返回删除的元素
public E removeLast(){
    return remove(size-1);
}

5. 用链表实现一个栈

  • 栈的接口
public interface Stack<E> {
    void push(E);
    E pop();
    E peek();
    int getSize();
    boolean isEmpty();
}
  • 链栈的实现
public class LinkedListStack<E> implements Stack<E> {
    private LinkedList<E> list;
    public LinkedListStack(){
        list = new LinkedList<>();
    }

    @Override
    public int getSize() {
        return list.getSize();
    }

    @Override
    public boolean isEmpty() {
        return list.isEmpty();
    }
    @Override
    public void push(E e){
        list.addFirst(e);
    }
    @Override
    public E pop(){
      return list.removeFirst();
    }
    @Override
    public E peek(){
        return list.getFirst();
    }

    @Override
    public String toString() {
        StringBuilder res = new StringBuilder();
        res.append("Stack: top ");
        res.append(list);
        return res.toString();
    }
}

6.用链表实现一个队列

  • 由于链表的特殊性,需要借助个尾指针来指向最后一个节点,这样队列中添加元素时间复杂度O(N);
/**
 * Created by Enzo Cotter on 2018/7/16.
 */
public class LinkedListQueue<E> implements Queue<E> {
    private class Node{
        public E e;
        public Node next;

        public Node(E e,Node next){
            this.e = e;
            this.next = next;
        }

        public Node(E e){
            this(e,null);
        }

        public Node(){
            this(null ,null);
        }

        @Override
        public String toString() {
            return e.toString();
        }
    }

    private Node head,tail;
    private int size;

    public LinkedListQueue(){
        head = null;
        tail = null;
        size = 0;
    }

    @Override
    public int getSize() {
        return size;
    }

    @Override
    public boolean isEmpty() {
        return size == 0;
    }

    @Override
    public void enqueue(E e) {
        if (tail == null ){ //头和尾都是空
            tail = new Node(e);
            head = tail;
        }
        else{
            tail.next = new Node(e);
            tail = tail.next;
        }
        size ++;
    }

    @Override
    public E dequeue() {
        if (isEmpty())
            throw new IllegalArgumentException("Cannot dequeue from an empty queue.");

        Node retNode = head;
        head = head.next;
        retNode.next = null;
        if(head == null)
            tail = null;
        size -- ;
        return retNode.e;
    }

    @Override
    public E getFront() {
        if (isEmpty())
            throw new IllegalArgumentException("Queue is empty");
        return head.e;
    }

    @Override
    public String toString() {
        StringBuilder res = new StringBuilder();
        res.append("Queue: front ");
        Node cur = head;
        while(cur != null){
            res.append(cur + "->");
            cur = cur.next;
        }
        res.append("NULL tail");
        return res.toString();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值