数据结构 -- 单向链表

单向链表

单向链表(单链表)是链表的一种,其特点是链表的链接方向是单向的,对链表的访问要通过顺序读取从头部开始;链表是使用指针进行构造的列表;又称为结点列表,因为链表是由一个个结点组装起来的;其中每个结点都有指针成员变量指向列表中的下一个结点;
列表是由结点构成,head指针指向第一个成为表头结点,而终止于最后一个指向NULL的指针。

单向链表的结构

图片(注:百度搜索而来的图片)链接: link.
Alt

java代码实现

public class LinkedList<E> {
    private Node head;
    private int size;

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

    public static void main(String[] args) {
        int[] arr = new int[]{49, 45, 32, 32, 32, 6, 32, 36, 45, 48, 18, 13, 50, 46, 8 };

        LinkedList<Integer> list = new LinkedList<>();
        System.out.println(list);
//        for (int i = 0; i < arr.length; i++) {
//            System.out.println("add e: "+ arr[i]);
//            list.addFirst(arr[i]);
//            System.out.println("list's size is "+list.getSize());
//            System.out.println(list);
//            System.out.println("---------------------------------------");
//        }
        for (int i = 0; i < arr.length; i++) {
            System.out.println("add e: " + arr[i]);
            list.addLast(arr[i]);
            System.out.println("list's size is " + list.getSize());
            System.out.println(list);
            System.out.println("---------------------------------------");
        }
        System.out.println("+++++++++++++++++++++++++++++++++++++++++++++ ");
        System.out.println("add (index 5, e 24)");
        list.add(5, 24);
        System.out.println("list's size is " + list.getSize());
        System.out.println(list);
        System.out.println("---------------------------------------");

        System.out.println("list contail 3 :" + list.contail(3));
        System.out.println("get first: " + list.getFirst());
        System.out.println("get last: " + list.getLast());
        System.out.println("get 5: " + list.get(5));
        list.set(5, 100);
        System.out.println("set (5,100): " + list);
        list.remove(5);
        System.out.println("remove (5): " + list);
    }

    /**
     * 链表的长度
     *
     * @return
     */
    public int getSize() {
        return this.size;
    }

    /**
     * 链表是否为空
     *
     * @return
     */
    public boolean isEmpty() {
        return this.size == 0;
    }

    /**
     * 在链表头部加入元素 e
     *
     * @param e
     */
    public void addFirst(E e) {
        if (this.head == null) {
            this.head = new Node(e);
            this.size++;
            return;
        }
        Node h_node = this.head;
        Node new_head = new Node(e);
        new_head.next = h_node;
        this.head = new_head;
        this.size++;
    }

    /**
     * 在链表尾部添加元素 e
     *
     * @param e
     */
    public void addLast(E e) {
        if (this.head == null) {
            this.head = new Node(e);
            this.size++;
            return;
        }
        Node tail = this.head;
        while (tail.next != null) {
            tail = tail.next;
        }
        tail.next = new Node(e);
        this.size++;
    }

    /**
     * 在 [0,this.size]范围内添加元素e
     *
     * @param index
     * @param e
     */
    public void add(int index, E e) {
        if (this.size < 0 || index > this.size) {
            throw new IllegalArgumentException("Add failed. illegal index.");
        }
        if (index == 0) {
            addFirst(e);
        }
        Node pre_node = this.head;
        for (int i = 0; i < index - 1; i++) {
            pre_node = pre_node.next;
        }
        Node nextNode = pre_node.next;
        pre_node.next = new Node(e);
        pre_node.next.next = nextNode;
        this.size++;
    }

    @Override
    public String toString() {
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append("head : ");
        Node curNode = this.head;
        while (curNode != null) {
            stringBuffer.append(curNode.e + " => ");
            curNode = curNode.next;
        }
        stringBuffer.append(" tail.");
        return stringBuffer.toString();
    }

    /**
     * 获取第一个元素
     *
     * @return
     */
    public E getFirst() {
        return get(0);
    }

    /**
     * 获取索引为index的元素
     *
     * @param index
     * @return
     */
    public E get(int index) {
        if (index < 0 || index >= this.size) {
            throw new IllegalArgumentException("Get failed. illegal index");
        }
        Node curNode = this.head;
        for (int i = 0; i < index; i++) {
            curNode = curNode.next;
        }
        return curNode.e;
    }

    /**
     * 获取最后一位的元素
     *
     * @return
     */
    public E getLast() {
        return get(this.size - 1);
    }

    /**
     * 修改索引为index的元素为e
     *
     * @param index
     * @param e
     */
    public void set(int index, E e) {
        if (index < 0 || index >= this.size) {
            throw new IllegalArgumentException("Set failed. illegal index");
        }
        Node curNode = this.head;
        for (int i = 0; i < index; i++) {
            curNode = curNode.next;
        }
        curNode.e = e;
    }

    /**
     * 链表中是否包含元素e
     *
     * @param e
     * @return
     */
    public boolean contail(E e) {
        Node curNode = this.head;
        while (curNode != null) {
            if (curNode.e.equals(e)) {
                return true;
            }
            curNode = curNode.next;
        }
        return false;
    }

    /**
     * 删除索引为index的元素
     *
     * @param index
     * @return
     */
    public E remove(int index) {
        if (index < 0 || index >= this.size) {
            throw new IllegalArgumentException("Remove fail,illegal index.");
        }
        Node curNode = this.head;
        for (int i = 0; i < index - 1; i++) {
            curNode = curNode.next;
        }
        Node nextNode = curNode.next;
        curNode.next = nextNode.next;
        nextNode.next = null;
        this.size--;
        return nextNode.e;
    }

    /**
     * 删除链表头部的元素
     *
     * @return
     */
    public E removeFirst() {
        return remove(0);
    }

    /**
     * 删除链表尾部的元素
     *
     * @return
     */
    public E removeLast() {
        return remove(this.size - 1);
    }

    /**
     * 节点
     */
    private class Node {
        private E e;
        private Node next;

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

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

head :  tail.
add e: 49
list's size is 1
head : 49 =>  tail.
---------------------------------------
add e: 45
list's size is 2
head : 49 => 45 =>  tail.
---------------------------------------
add e: 32
list's size is 3
head : 49 => 45 => 32 =>  tail.
---------------------------------------
add e: 32
list's size is 4
head : 49 => 45 => 32 => 32 =>  tail.
---------------------------------------
add e: 32
list's size is 5
head : 49 => 45 => 32 => 32 => 32 =>  tail.
---------------------------------------
add e: 6
list's size is 6
head : 49 => 45 => 32 => 32 => 32 => 6 =>  tail.
---------------------------------------
add e: 32
list's size is 7
head : 49 => 45 => 32 => 32 => 32 => 6 => 32 =>  tail.
---------------------------------------
add e: 36
list's size is 8
head : 49 => 45 => 32 => 32 => 32 => 6 => 32 => 36 =>  tail.
---------------------------------------
add e: 45
list's size is 9
head : 49 => 45 => 32 => 32 => 32 => 6 => 32 => 36 => 45 =>  tail.
---------------------------------------
add e: 48
list's size is 10
head : 49 => 45 => 32 => 32 => 32 => 6 => 32 => 36 => 45 => 48 =>  tail.
---------------------------------------
add e: 18
list's size is 11
head : 49 => 45 => 32 => 32 => 32 => 6 => 32 => 36 => 45 => 48 => 18 =>  tail.
---------------------------------------
add e: 13
list's size is 12
head : 49 => 45 => 32 => 32 => 32 => 6 => 32 => 36 => 45 => 48 => 18 => 13 =>  tail.
---------------------------------------
add e: 50
list's size is 13
head : 49 => 45 => 32 => 32 => 32 => 6 => 32 => 36 => 45 => 48 => 18 => 13 => 50 =>  tail.
---------------------------------------
add e: 46
list's size is 14
head : 49 => 45 => 32 => 32 => 32 => 6 => 32 => 36 => 45 => 48 => 18 => 13 => 50 => 46 =>  tail.
---------------------------------------
add e: 8
list's size is 15
head : 49 => 45 => 32 => 32 => 32 => 6 => 32 => 36 => 45 => 48 => 18 => 13 => 50 => 46 => 8 =>  tail.
---------------------------------------
+++++++++++++++++++++++++++++++++++++++++++++ 
add (index 5, e 24)
list's size is 16
head : 49 => 45 => 32 => 32 => 32 => 24 => 6 => 32 => 36 => 45 => 48 => 18 => 13 => 50 => 46 => 8 =>  tail.
---------------------------------------
list contail 3 :false
get first: 49
get last: 8
get 5: 24
set (5,100): head : 49 => 45 => 32 => 32 => 32 => 100 => 6 => 32 => 36 => 45 => 48 => 18 => 13 => 50 => 46 => 8 =>  tail.
remove (5): head : 49 => 45 => 32 => 32 => 32 => 6 => 32 => 36 => 45 => 48 => 18 => 13 => 50 => 46 => 8 =>  tail.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值