数据结构(java版)SinglyList(单链表)

SinglyList(单链表)

代码部分:


class Node<T> {

    public T data; // 数据域
    public Node<T> next; // 地址域,指向下一个节点地址

    public Node(T data, Node<T> next) { // 构造函数,提供数据域和地址域
        this.data = data;
        this.next = next;
    }

    public Node() { // 构造函数,建造空节点
        this(null, null); // 调用上面的构造函数,调用自身
    }

    public Node(T data) {
        this.data = data;
    }

    public String toString() {
        return this.data.toString();
    }

}

/*
 * 带头结点的单链表
 */
public class SinglyList<T> {

    public Node<T> head; // 创建头结点
    public int n; // 链表长度,包括头结点

    //构造函数1,只有一个头结点

    public SinglyList() {
        this.head = new Node<T>();
    }

    // 构造函数2,依次将values中的值填入单链表中
    public SinglyList(T[] values) {
        this(); // 构造一个空的单链表
        n = n + values.length; // 确定链表长度
        Node<T> rear = this.head; // 为空的单链表设置头结点
        for (int i = 0; i < values.length; i++) {
            rear.next = new Node<T>(values[i], null); // 尾插入节点
            rear = rear.next; // 尾指针指向尾节点
        }
    }

    // 判断单链表是否为空
    public boolean isEmpty() {

        return this.head.next == null; // 带头结点的单链表判断为空的条件是头结点以后为空
    }

    public T get(int i) {
        Node<T> cur = this.head.next; // 用cur指向当其节点
        for (int j = 0; cur != null && j < i; j++) {
            cur = cur.next;
        }
        return (i >= 0 && cur != null) ? cur.data : null; // 当i(小于零,或者链表为空)都返回空,否则返回对应元素
    }

    public void set(int i, T x) {
        if (x == null)
            throw new NullPointerException("x==null"); // 插入的元素为空,抛出空异常
        Node<T> front = this.head;

        for (int j = 0; front.next != null && j < i; j++) // 寻找到第i个节点
            front = front.next;
        front.data = x;

    }

    public int size() {

        int length = 1; // 头结点算是第一个节点
        Node<T> p = head.next;
        while (p != null) {
            length++;
            p = p.next;
        }
        return length;

    }

    //深拷贝单链表
    public SinglyList(SinglyList<T> list) {

        this.head.data = list.head.data;
        Node<T> cur1 = list.head; // 沿着list链表遍历
        Node<T> cur2 = this.head; // 沿着当前链表开始创建节点,然后拷贝
        while (cur1.next != null) {
            cur1 = cur1.next;
            Node<T> p = new Node<T>();
            p.data = cur1.data;
            cur2.next = cur2;
        }

    }

    public String toString() {
        String str = "(";
        for (Node<T> p = this.head.next; p != null; p = p.next) { // 遍历单链表
            str += p.data.toString();
            if (p.next != null)
                str += ",";

        }
        return str + ")";
    }

    //在指定位置进行插入,将x插入到第i个元素之后(第一个元素是head)
    public Node<T> insert(int i, T x) {
        if (x == null)
            throw new NullPointerException("x==null");
        Node<T> front = this.head;
        for (int j = 0; front.next != null && j < i; j++) // 寻找到第i个节点
            front = front.next;
        front.next = new Node<T>(x, front.next);//三合一
        n++; // 长度加1
        return front.next; // 返回插入结点
    }

    public Node<T> insert(T x) {
        return insert(Integer.MAX_VALUE, x);
    }

    // 删除第i位结点
    public T remove(int i) {
        Node<T> front = this.head;
        for (int j = 0; front.next != null && j < i; j++)
            front = front.next; // 遍历并找到第i位元素,或者是最后一位(当i大于链表长度时)
        if (i >= 0 && front.next != null) { //如果i小于0,跳过上面的for语句
            T old = front.next.data;
            front.next = front.next.next; // 删除操作
            n--;
            return old;
        }
        return null; // 说明i小于0,或者大于链表长度
    }


    // 根据关键字进行删除
    public T remove(T key) {
        Node<T> p = this.head;
        if (head.data == key) {
            head = head.next;
            n--;
            return key;
        }
        while (!p.next.data.equals(key) && p.next.next != null) {
            p = p.next;
        }
        if (p.next.data.equals(key)) {
            p.next = p.next.next;
            n--;
            return key;
        }
        return null;

    }

    public Node<T> search(T key) {

        Node<T> p = this.head;
        p = p.next;
        while (!p.data.equals(key) && p.next != null) {
            p = p.next; // 当前节点数据域不等于key,并且p的下一个节点不为空

        }
        if (p.data.equals(key))
            return p;

        return null;
    }

    public void clear() {
        this.head.next = null;
        n = 1;
    }
}

写在最后:

仅为学习阶段记录写过的代码,方便自己查找,初出茅庐,有不完善的地方烦请指正,如有继承类可查阅数据结构其他博文。

  • 4
    点赞
  • 35
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AntyRia

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值