数据结构之线性表:单链表

代码实现
package top.gldwolf.java.datastructure.linkedtable;

/**
 * @author: Gldwolf
 * @email: ZengqiangZhao@sina.com
 * @date: 2020/4/15 12:39
 */

/**
 * 线性表的链式存储结构
 *
 * @param <T> 存储的数据类型
 */
public class LinkedTable<T> {
    private Node head = new Node();
    private Node last; // 最后一个节点
    private int length = 1; // 初始值为 0,只有一个头节点

    public LinkedTable() {
    }

    /**
     * 添加一个新的数据到表尾
     *
     * @param data 新添加的数据
     * @return 成功返回 true,失败返回 false
     */
    public boolean add(T data) {
        Node<T> node = new Node<>(data);
        // 现在最后一个元素为新添加的元素
        if (head.next == null) { // 如果头节点指针为空,则表示表中无数据,新添加的数据为第一个节点
            head.next = node; // 头节点指向第一个节点
        } else { // 如果原来不是空表的话
            last.next = node; // 原来的最后一个元素指针指向新添加的元素
        }
        last = node; // 最后一个节点为新添加的数据
        length++;
        return true;
    }

    /**
     * 获取指定索引位置的数据,通过遍历实现
     *
     * @param index
     * @return 该节点的数据
     * @throws Exception 当下标越界时抛出异常
     */
    public T get(int index) throws Exception { // 下标从 1 开始
        if (index <= 0 || index >= length) { // 如果索引大于或等于表长度: 如果有一个数据,则表长度为 2,取索引大于或等于 2 的数据则抛出异常
            throw new Exception("下标越界");
        } else { // 如果索引不越界
            /*
                如果索引不越界,则会从头开始遍历数据,
                每循环一遍,游标 +1,得到游标所在节点的数据,
                当游标达到指定索引时,返回这个数据,并退出循环
             */
            Node cursor = head;
            for (int i = 1; i <= index; i++) { // 遍历数据,每执行一步获取一个节点,当个数达到索引值时,返回这个节点的数据
                cursor = cursor.next;
            }
            return (T) cursor.getData();
        }
    }

    /**
     * 在指定位置插入数据
     * @param index 数据要插入的位置
     * @param data 要插入的数据
     * @return 成功返回 true,否则返回 false
     * @throws Exception
     */
    public boolean insert(int index, T data) throws Exception {
        if (index < 1 || index > length) {
            throw new Exception("下标越界");
        } else if (index == length) { // 如果在最末尾插入数据
            Node node = new Node(data);
            last.next = node;
            last = node;
        } else if (index == 1) { // 如果放在第一个节点
            Node node = new Node(data);
            node.next = head.next;
            head.next = node;
        } else { // 如果不在最末尾插入数据
            Node node = new Node(data);
            Node cursor = head;
            for (int i = 1; i < index; i++) { // 通过 cursor 得到这个索引的前一个数据
                cursor = cursor.next;
            }
            node.next = cursor.next;
            cursor.next = node;
        }
        length ++;
        return true;
    }

    public boolean delete(int index) throws Exception {
        if (index < 1 || index > length) {
            throw new Exception("下标越界");
        } else if (index == 1) { // 如果删除的为第 1 个节点
            head.next = head.next.next;
        } else if (index == length - 1) { // 如果删除的是最后一个节点
            Node cursor = head;
            for (int i = 1; i < length; i++) { // 找到要删的节点的前一个节点
                cursor = cursor.next;
            }
            cursor.next = null;
        } else { // 如果删除其它节点
            Node cursor = head;
            for (int i = 1; i < index; i++) { // 找到要删的节点的前一个节点
                cursor = cursor.next;
            }
            cursor.next = cursor.next.next;
        }
        length--;
        return true;
    }

    @Override
    public String toString() {
        StringBuffer res = new StringBuffer();
        Node cursor = head;
        for (int i = 1; i < length; i++) {
            cursor = cursor.next;
            res.append(cursor.getData() + ", ");
        }
        return res.toString() + " length: " + (length - 1);
    }
}

class Node<N> {
    protected Node next; // 指向下一个 Node
    //    private Node pre; // 指向上一个 Node
    private N data; // 用来存储数据

    protected Node() {
    } // 仅用于创建头节点

    public Node(N data) { // 用于添加后续数据节点
        this.data = data;
    }

    public N getData() {
        return data;
    }
}
测试类
package top.gldwolf.java.datastructure.linkedtable;

/**
 * @author: Gldwolf
 * @email: ZengqiangZhao@sina.com
 * @date: 2020/4/15 12:40
 */

public class LinkedTableDriver {
    public static void main(String[] args) throws Exception {
        LinkedTable<String> table = new LinkedTable<>();
        table.add("2");
        table.add("3");
        table.add("4");
        table.add("5");
        table.add("6");
        String s = table.get(5);
        System.out.println(s);
        table.insert(1, "1");
        System.out.println(table.get(1));
        System.out.println(table.get(5));
        table.insert(4, "9");
        System.out.println(table.get(4));
        System.out.println(table.toString());
        table.delete(4);
        System.out.println(table);
        table.delete(1);
        System.out.println(table);
        table.delete(5);
        System.out.println(table);
        table.add("6");
        System.out.println(table);
        System.out.println(table.get(5));
        System.out.println(table.get(3));
        table.delete(5);
        table.delete(3);
        System.out.println(table);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值