【数据结构】单向链表

package 链表.单向链表;

import java.util.NoSuchElementException;

/**
 * @author 芜情
 * 带头结点的单向链表
 */
public class SLinkedList{

    //定义链表节点
    private static class Node{
        private int data;
        private Node next;
        public int getData() {
            return data;
        }
        public void setData(int data) {
            this.data = data;
        }
        public Node getNext() {
            return next;
        }
        public void setNext(Node next) {
            this.next = next;
        }

        Node(){
        }

        Node(int e){
            data = e;
        }
    }

    //头结点
    private Node head;

    //链表长度
    private int size;

    //初始化单向链表
    public SLinkedList() {
        head = new Node();
        head.setNext(null);
        size = 0;
    }

    //判断链表是否为空
    public boolean isEmpty(){
        return head.getNext() == null;
    }

    //清空链表
    public void clear(){
        head.setNext(null);
    }

    //链表长度
    public int size(){
        return size;
    }
    /*============================================*/
    /*内部方法,为了好理解,从名字能看出来代码要干什么*/

    //判断下标是不是有效
    private boolean isValidIndex(int index) {
        return index >= 0 && index < size;
    }

    //判断某个操作的位置是否有效,如执行插入操作时
    private boolean isValidPosition(int position) {
        return position >= 0 && position <= size;
    }

    //定义报错信息
    private String outOfBoundsMsg(int index) {
        return "Index: "+index+", Size: "+size;
    }

    private void checkIndex(int index) {
        if (!isValidIndex(index))
            throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
    }

    private void checkPosition(int position) {
        if (!isValidPosition(position))
            throw new IndexOutOfBoundsException(outOfBoundsMsg(position));
    }

    //返回指定下标节点
    private Node node(int index){
        Node currentNode = head;
        int i = 0;
        while(i <= index){
            currentNode = currentNode.getNext();
            ++i;
        }
        return currentNode;
    }
    /*============================================*/

    /**
     * 增加节点
     */

    //链接首节点
    private void linkFirst(Node node) {
        final Node temp = head.getNext();
        head.setNext(node);
        node.setNext(temp);
        ++size;
    }

    //由于单向链表,得知道要插入位置节点的前节点,进行链接
    private void link(Node prev, Node newNode){
        newNode.setNext(prev.getNext());
        prev.setNext(newNode);
        ++size;
    }

    //在中间插入节点
    public void add(int position, int e){
        checkPosition(position);
        Node newNode = new Node(e);
        if(position == 0){
            linkFirst(newNode);
        }else{
            link(node(position - 1), newNode);
        }
    }

    //插入首节点
    public void addFirst(int e){
        Node newNode = new Node(e);
        linkFirst(newNode);
    }

    //插入尾节点
    public void addLast(int e){
        Node newNode = new Node(e);
        link(node(size - 1), newNode);
    }

    public void add(int e){
        addLast(e);
    }

    /**
     * 删除节点
     */

    //解除首节点链接
    private int unlinkFirst(Node f) {
        final int element = f.getData();
        head.setNext(f.getNext());
        --size;
        return element;
    }

    //解除中间结点链接
    private int unlink(Node prev, Node node) {
        final int element = node.getData();
        prev.setNext(node.getNext());
        --size;
        return element;
    }

    //删除中间的一个节点
    public int remove(int index){
        checkIndex(index);
        if(index == 0){
            return unlinkFirst(node(0));
        }else{
            return unlink(node(index - 1),node(index));
        }
    }

    //删除首节点,返回被删除的元素
    public int removeFirst(){
        final Node f = head.getNext();
        if(f == null){
            throw new NoSuchElementException();
        }
        return unlinkFirst(f);
    }

    public int remove(){
        return removeFirst();
    }

    //删除尾节点
    public int removeLast(){
        final Node f = head.getNext();
        if(f == null){
            throw new NoSuchElementException();
        }
        if(f.getNext() == null){
            return unlinkFirst(f);
        }else{
            return unlink(node(size - 2), node(size - 1));
        }
    }

    /*链表主要以增删节点的操作比较重要,改和查就不写了*/

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值