Java【链表操作】

建立单列表节点类

package org.article.node;

import lombok.Data;

/**
 * @Author: yangyang7
 * @Description: 单链表节点
 * @Date: Created in 09:28 2019-11-25
 */

@Data
public class Node {

    /**
     * 数据域
     */
    private int data;

    /**
     * 指针域
     */
    private Node next;

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

建立单链表类

package org.article.node;

import lombok.Data;

/**
 * @Author: yangyang7
 * @Description: 建立简单的单链表
 * @Date: Created in 09:30 2019-11-25
 */

@Data
public class SLinkList {

    private Node head;

    /**
     * 节点的长度
     */
    private int length = 0;

    public SLinkList() {
        this.head = null;
    }

    /**
     * 在链表头部添加节点
     */
    public void addHead(int data) {
        Node newNode = new Node(data);
        if (head == null) {
            head = newNode;
        } else {
            newNode.setNext(head);
            head = newNode;
        }
        length++;
    }

    /**
     * 在链表头部删除节点
     */
    public void deleteHead() {
        if (head == null) {
            System.out.println("链表为空");
            return;
        }
        Node curNode = head;
        head = curNode.getNext();
        length--;
    }

    /**
     * 在链表尾部添加节点
     */
    public void addTail(int data) {
        Node newNode = new Node(data);
        if (head == null) {
            head = newNode;
        } else {
            Node preNode = head;
            int count = 1;
            while (count < length) {
                preNode = preNode.getNext();
                count++;
            }
            Node curNode = preNode.getNext();
            newNode.setNext(curNode);
            preNode.setNext(newNode);
            length++;
        }
    }

    /**
     * 在链表尾部删除节点
     */
    public void deleteTail() {
        if (head == null) {
            System.out.println("链表为空");
            return;
        }
        Node preNode = head;
        int count = 1;
        while (count < length - 1) {
            preNode = preNode.getNext();
            count++;
        }
        Node curNode = preNode;
        preNode.setNext(curNode.getNext());
        length--;
    }

    /**
     * 在链表指定位置插入节点
     */
    public void insertList(int index, int data) {
        Node newNode = new Node(data);
        if (head == null) {
            head = newNode;
        }
        if (index < length || index < 0) {
            System.out.println("没有指定节点位置");
            return;
        }
        if (index == 1) {
            // 在头部插入
            head.setNext(head);
            head = newNode;
        } else {
            Node preNode = head;
            int count = 1;
            while (count < index - 1) {
                preNode = newNode.getNext();
                count++;
            }
            Node curNode = preNode.getNext();
            preNode.setNext(newNode);
            newNode.setNext(curNode);
        }
        length++;
    }

    /**
     * 在链表指定位置删除节点
     */
    public void deleteList(int index) {
        if (head == null) {
            System.out.println("链表为空");
            return;
        }
        if (index == 1) {
            // 删除头部
            Node curNode = head;
            head = curNode.getNext();
            length--;
            return;
        } else {
            Node preNode = head;
            int count = 1;
            while (count < index - 1) {
                preNode = preNode.getNext();
                count++;
            }
            Node curNode = preNode.getNext();
            preNode.setNext(curNode.getNext());
            length--;
        }
    }


    /**
     * 获取指定位置的节点
     *
     * @param index
     * @return
     */
    public Node getIndexData(int index) {
        if (head == null) {
            System.out.println("空表");
            return null;
        }
        if (index > length || index < 1) {
            System.out.println("节点位置不存在,可查询的位置为1到" + length);
            return null;
        }
        Node preNode = head;
        int count = 1;
        while (count != index) {
            preNode = preNode.getNext();
            count++;
        }
        System.out.println(preNode);
        return preNode;
    }

    /**
     * 修改指定位置节点数据
     *
     * @param index
     * @param data
     */
    public void updateIndexData(int index, int data) {
        if (head == null) {
            System.out.println("空表");
        }
        if (index > length || index < 1) {
            System.out.println("节点位置不存在,可查询的位置为1到" + length);
        }
        Node preNode = head;
        int count = 1;
        while (count != index) {
            preNode = preNode.getNext();
            count++;
        }
        preNode.setData(data);
    }
}

建立双向列表节点类

package org.article.node;

import lombok.Data;

/**
 * @Author: yangyang7
 * @Description:
 * @Date: Created in 16:47 2019-11-25
 */

@Data
public class DNode {

    /**
     * 数据域
     */
    private int data;

    /**
     * 后驱指针域
     */
    private DNode next;

    /**
     * 前驱指针域
     */
    private DNode previous;

    public DNode(int data) {
        this.data = data;
    }
}

建立双向链表类

package org.article.node;

import lombok.Data;

/**
 * @Author: yangyang7
 * @Description: 双向链表类
 * @Date: Created in 16:50 2019-11-25
 */

@Data
public class DLinkList {

    /**
     * 表头
     */
    private DNode head;
    private int length = 0;

    public DLinkList() {
        this.head = null;
    }

    /**
     * 在链表头部添加结点
     *
     * @param data
     */
    public void addHead(int data) {
        DNode newNode = new DNode(data);
        if (head == null) {
            //如果链表为空,增加新结点
            head = newNode;
        } else {
            newNode.setNext(head);
            head.setPrevious(newNode);
            head = newNode;
        }
        length++;
    }

    /**
     * 在链表头部删除结点
     */
    public void deleteHead() {
        if (head == null) {
            System.out.println("空表,删除的结点不存在");
        } else {
            DNode curNode = head;
            head = curNode.getNext();
            head.setPrevious(null);
        }
        length--;
    }

    /**
     * 在链表尾部添加结点
     *
     * @param data
     */
    public void addTail(int data) {
        DNode newNode = new DNode(data);
        if (head == null) {
            head = newNode;
        } else {
            DNode curNode = head;
            int count = 1;
            while (count < length) {
                curNode = curNode.getNext();
                count++;
            }
            newNode.setNext(null);
            newNode.setPrevious(curNode);
            curNode.setNext(newNode);
        }
        length++;
    }

    /**
     * 在链表尾部删除结点
     */
    public void deleteTail() {
        if (head == null) {
            System.out.println("空表,删除的结点不存在");
        } else {
            DNode preNode = head;
            int count = 1;
            while (count < length - 1) {
                preNode = preNode.getNext();
                count++;
            }
            preNode.setNext(null);
        }
        length--;
    }


    /**
     * 在指定位置插入结点
     *
     * @param data
     * @param index
     */
    public void insertList(int data, int index) {
        DNode newNode = new DNode(data);
        if (head == null) {
            head = newNode;//链表为空,插入
        }
        if (index > length + 1 || index < 1) {
            System.out.println("结点插入的位置不存在,可插入的位置为1到" + (length + 1));
        }
        if (index == 1) {
            newNode.setNext(head);
            head.setPrevious(newNode);
            head = newNode;//在链表开头插入
        } else {              //在链表中间或尾部插入
            DNode preNode = head;
            int count = 1;
            while (count < index - 1) {
                preNode = preNode.getNext();
                count++;
            }
            DNode curNode = preNode.getNext();
            newNode.setNext(curNode);
            newNode.setPrevious(preNode);
            preNode.setNext(newNode);

            curNode.setPrevious(newNode);

        }
        length++;
    }

    /**
     * 在指定位置删除结点
     *
     * @param index
     */
    public void deleteList(int index) {
        if (index > length || index < 1) {
            System.out.println("结点删除的位置不存在,可删除的位置为1到" + length);
        }
        if (index == 1) {
            DNode curNode = head;
            head = curNode.getNext();
            head.setPrevious(null);
            length--;
        } else {
            DNode preNode = head;
            int count = 1;
            while (count < index - 1) {
                preNode = preNode.getNext();
                count++;
            }
            DNode curNode = preNode.getNext();
            DNode laterNode = curNode.getNext();
            preNode.setNext(laterNode);
            if (laterNode != null) {  //若被删除结点的后继结点不是null结点,那么设置其前驱结点
                laterNode.setPrevious(preNode);//指针指向被删除结点的前驱结点
            }
            length--;
        }
    }

    /**
     * 获取指定位置的数据,与单链表一样
     *
     * @param index
     */
    public void getIndexData(int index) {
        if (head == null) {
            System.out.println("空表");
        }
        if (index > length || index < 1) {
            System.out.println("结点位置不存在,可获取的位置为1到" + length);
        }
        DNode curNode = head;
        int count = 1;
        while (count != index) {
            curNode = curNode.getNext();
            count++;
        }
        System.out.println(curNode);
        System.out.println();
    }

    /**
     * 修改指定位置的结点数据,与单链表一样
     *
     * @param index
     * @param data
     */
    public void updateIndexData(int index, int data) {
        if (head == null) {
            System.out.println("空表");
        }
        if (index > length || index < 1) {
            System.out.println("结点位置不存在,可更新的位置为1到" + length);
        }
        DNode curNode = head;
        int count = 1;//while也可以用for循环方式解决
        while (count != index) {
            curNode = curNode.getNext();
            count++;
        }
        curNode.setData(data);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值