数据结构——单向链表

Java单向链表

定义:单向链表是链表的一种,特点是链接的方式是单向的,对链表的访问是从头部开始访问的;链表是使用指针构造的列表,又称为节点列表,因为链表是由一个个结点组装起来的;其中每个结点都有指针成员变量指向列表中的下一个结点。
结构图:
在这里插入图片描述
Java代码实现列表的数据结构:


public class SingleList<T> {
    private Node head;//头节点
	private int size;//链表元素个数
	public SingleList() {
        this.head = null;
        this.size = 0;
    }
		
    //节点类的数据结构
    public class Node<T>{
        T data;     //数据域
        Node next;  //指针域

         //构造函数
        public Node(T data) {
            this.data = data;
            next = null;
        }
        public Node(T data,Node next) {
            this.data = data;
            this.next = next;
        }
    }
}

插入节点

  1. 头部插入:

实现思路:让插入的节点成为一个新的头节点,新的头节点的next只向当前头节点。
结构图:
在这里插入图片描述
代码实现:

 /**
     * 单向链表头部插入节点
     * @param value
     */
    public void addHeadNode(T value){
        Node newNode = new Node(value);
        //头节点不存在,新节点直接成为头节点
        if (head == null){
            head = newNode;
            return;
        }
        //新节点next直接指向当前头节点
        newNode.next = head;
        //新节点成为头节点
        head = newNode;
    }
  1. 尾部插入节点
    实现思路:找到列表的尾结点,指向新插入节点,新节点的next指向NULL
    结构图:
    在这里插入图片描述
    Java代码:
    /**
     * 单向链表插入尾节点
     * @param value
     */
    public void addTailNode(T value){
     Node newNode = new Node(value);
     //找到尾节点
     Node last = head;
     while (last.next != null){
         last = last.next;
     }
     //找到尾节点之后指向新插入的节点
     last.next = newNode;
    }
}
  1. 指定位置插入节点
    实现思路:找出插入指定位置的前一个节点pre和当前节点cur,然后cur成为新节点的下一个节点,pre节点成为新节点的上一个节点。
    结构图:
    在这里插入图片描述
    Java代码:
/**
     * 中间某个位置插入节点
     * @param value
     * @param index
     */
    public void addMidNode(T value,int index){
        Node newNode = new Node(value);
        int position = 0;
        Node cur = head;//记录当前节点
        Node pre = null;//记录前置节点
        while (cur != null){
            if (cur != null){
                if (position == index){
                    newNode.next = cur;
                    pre.next = newNode;
                    return;
                }
                pre = cur;
                cur = cur.next;
                position ++;
            }
        }
    }

删除指定位置节点

实现思路:找到指定位置的节点的当前节点cur和他的前置节点pre,pre指向cur的next节点。
结构图:
在这里插入图片描述

Java代码:

 /**
     * 删除指定节点
     * @param index
     */
    public void delNode(int index){
        int position = 0;
        Node cur = head;
        Node pre = null;
        while (cur != null){
            if (position == index){
                    pre.next = cur.next;
                    //断开与链表的链接
                    cur.next = null;
                    return;
            }
             pre = cur;
            cur = cur.next;
            position ++;
        }
    }

链表反转

实现思路:在链表遍历的过程中将指针顺序置换,即每遍历一次链表就让cur.next指向pre,最后一个结点成为新的头结点。
结构图:
在这里插入图片描述

Java代码实现:

    /**
     * 链表逆置
     */
    public void reverseListNode(){
        Node cur = head;//标记当前节点
        Node pre = null;//标记当前节点的前一个节点
        Node temp;//定义一个临时节点
        
        while (cur != null){
            //保存当前结点的下一个节点
            temp = cur.next;
            //cur.next指向pre,指针顺序置换
            cur.next = pre;
            //pre、cur继续后移
            pre = cur;
            cur = temp;
        }
        //最后一个节点变成新的头节点
        head = pre;
        
    }

求倒数n个位置的节点

实现思路:倒数第k个结点就是第size() - k + 1个结点,cur结点向后移动size() - k次就是倒数第k个结点。
Java代码实现:

    public Node getLastNode(int n){
        if (n < 0 || n > this.size){
            throw new IndexOutOfBoundsException("IndexOutOfBoundsException");
        }
        Node cur = head;
        for (int i = 1 ; i < this.size - n + 1; i ++){
            cur = cur.next;
        }
        return cur;
    }

判断是否包含某个元素

Java代码:

//链表中是否包含某个元素
    public boolean contains(T t){
        Node cur = this.head;
        while(cur != null){
            if(cur.data.equals(t)){
                return true;
            }
            else cur = cur.next;
        }
        return false;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值