不带头非循环单链表的实现

单链表的Java实现

1. 什么是链表

用一组地址任意的存储单元存放线性表中的数据元素,即逻辑上连续,物理上不一定连续

在顺序表的链式存储结构中,每个存储的节点不仅包含数据元素本身(称之为数据域),而且还包括此节点的后继节点的地址信息(称之为指针域)。一般地,每个节点可以有一个或多个这样的指针域,如果某个节点中的某个指针域不再需要指向任何节点,则它的值为null

2. 什么是单链表

在每个节点中除了包含数据域 (data域) 外,又设置一个指针域 (next域),用以指向其后继节点,即每个节点的构成:元素(数据元素的映像) + 指针(指向后继元素的存储位置)。

3. 不带头非循环单链表

这种单链表必须有一个头指针(假设起名叫head),用以指向单链表中的第一个节点,否则单链表会在内存中丢失。
在这里插入图片描述
在这里插入图片描述

  • 以下是不带头非循环单链表的Java语言实现:
package com.Linked;

public interface ILinked {

    //头插法
    void addFirst(int data);
    //尾插法
    void addLast(int data);
    //任意位置插入,第一个数据节点为0号下标
    boolean addIndex(int index, int data);
    //查找是否包含关键字key是否在单链表当中
    boolean contains(int key);
    //删除第一次出现关键字为key的节点
    int remove(int key);
    //删除所有值为key的节点
    void removeAllKey(int key);
    //得到单链表的长度
    int getLength();
    //打印单链表
    void display();
    //清空单链表以防内存泄漏
    void clear();
}
package com.Linked;

public class MySingleListImpl implements ILinked {

	//节点类
    class Node {

        private int data;
        private Node next;

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

    private Node head;
    public MySingleListImpl() {
    	//head初始化为0,因为不知道它到底指向哪儿
        this.head = null;
    }

    @Override
    public void addFirst(int data) {

        Node node = new Node(data);
        //如果单链表为空,只需要让head指向node
        if (this.head == null) {
            this.head = node;
        }else {
        	//链表不为空
            node.next = this.head;
            this.head = node;
        }
    }

    @Override
    public void addLast(int data) {

        Node node = new Node(data);
        //链表为空时同头插法
        if (this.head == null) {
            this.head = node;
        }else {
            Node cur = this.head;
            while (cur.next != null) {//找最后一个节点
                cur = cur.next;
            }
            cur.next = node;
        }
    }

	//找index-1位置的节点
    public Node searchIndex(int index) {

        if (index < 0 || index > getLength()) {
            throw new UnsupportedOperationException();
        }

        int i = 0;
        Node cur = this.head;
        while (i < index-1) {
            cur = cur.next;
            i++;
        }
        return cur;
    }

    @Override
    public boolean addIndex(int index, int data) {

        if (index == 0) {
            addFirst(data);
            return true;
        }
        if (index == getLength()) {
            addLast(data);
            return true;
        }
        
        Node node = new Node(data);
        Node pre = searchIndex(index);
        node.next = pre.next;
        pre.next = node;
        return true;
    }

    @Override
    public boolean contains(int key) {

		//单链表为空
        if (this.head == null) {
            return false;
        }

        Node cur = this.head;
        while (cur != null) {
            if (cur.data == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

	//寻找要删除的节点的前驱
    public Node searchPre(int key) {

        Node cur = this.head;
        while (cur.next != null) {
            if (cur.next.data == key) {
                return cur;
            }
            cur = cur.next;
        }
        return null;
    }

    @Override
    public int remove(int key) {

        if (this.head == null) {
            throw new UnsupportedOperationException("单链表为空,不支持删除");
        }

		//因为要求返回值为int型,所以定义了一个oldData来记录要删除节点的data域
        int oldData = 0;

		//searchPre返回值有两种,要么找到了,要么没找到,分情况讨论
        Node pre = searchPre(key);

        if (pre == null) {
            throw new UnsupportedOperationException("链表中没有该节点的前驱");
        }

        oldData = pre.next.data;
        pre.next = pre.next.next;

		//如果要删除的是第一个节点,上述两种情况不能覆盖
		if (this.head.data == key) {
            oldData = this.head.data;
            this.head = this.head.next;
            return oldData;
        }

        return oldData;
    }

    @Override
    public void removeAllKey(int key) {

        if (this.head == null) {
            throw new UnsupportedOperationException("单链表为空,不支持删除");
        }

        Node pre = this.head;
        Node cur = this.head.next;

        while (cur != null) {
            if (cur.data == key) {
                pre.next = cur.next;
                cur = cur.next;
            }else {
                pre = cur;
                cur = cur.next;
            }
        }

        if (this.head.data == key) {
            this.head = this.head.next;
        }
    }

    @Override
    public int getLength() {

        if (this.head == null) {
            return 0;
        }

        int count = 0;
        Node cur = this.head;
        while (cur != null) {
            count++;
            cur = cur.next;
        }
        return count;
    }


    @Override
    public void display() {

        Node cur = this.head;
        while (cur != null) {
            System.out.print(cur.data + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    @Override
    public void clear() {

        Node cur = this.head;
        while (cur.next != null) {
            this.head = cur.next;
            cur = cur.next;
        }
        this.head = null;
    }
}
package com.Linked;

public class Test {

    public static void main(String[] args) {

        MySingleListImpl list = new MySingleListImpl();

        list.addFirst(10);
        list.addFirst(20);
        list.addFirst(30);
        list.addLast(10);
        list.addLast(20);
        list.addLast(30);
        System.out.print("原链表:");
        list.display();

        System.out.println("链表的长度:" + list.getLength());

        list.addIndex(3, 99);
        System.out.print("添加后的链表:");
        list.display();

        System.out.println("链表是否包含元素99:" + list.contains(99));
        System.out.println("链表是否包含元素100:" + list.contains(100));

        list.remove(99);
        System.out.print("删除99之后的链表:");
        list.display();

        list.removeAllKey(20);
        System.out.print("删除所有值为20的节点后的链表:");
        list.display();

        list.clear();
        System.out.print("清空后的单链表:");
        list.display();
    }
}

在这里插入图片描述

### 带头节点的循环单链表 #### 定义 带头节点的循环单链表是一种特殊的线性数据结构,在这种结构中,最后一个节点的指针域是指向`NULL`而是指向第一个节点(即头节点),形成一个闭环。由于没有额外的头节点,实际的数据存储从这个首节点开始。 #### 结构特性 - **无哨兵节点**:像带虚拟头节点版本那样存在一个仅用于标记起点而保存任何有效信息的特殊节点。 - **闭合链接**:所有节点通过`next`指针串联起来,并最终回到起始位置,构成一圈。 - **访问方式受限**:遍历时需特别注意防止无限循环;删除或查找特定元素时也较为复杂,因为无法轻易区分当前处理的是哪个部分[^1]。 #### 主要特点 - **节省空间开销**:省去了必要的辅助节点内存占用。 - **操作相对困难**:增删查改等基本操作都需要考虑特殊情况下的边界条件,比如当列表为空或是只有一个元素的时候如何正确维护环形连接关系。 - **初始化简单但后续管理成本高**:创建空表很容易实现,但是随着元素数量增加以及频繁变动,则可能带来较高的逻辑控制难度。 #### 实现方法 下面给出一段Python代码来展示带头节点的循环单链表的部分功能: ```python class Node: def __init__(self, data=None): self.data = data self.next = None def create_circular_linked_list(elements): if not elements: return None head = Node(elements[0]) current = head for element in elements[1:]: new_node = Node(element) current.next = new_node current = new_node # Make it circular by linking the last node to the first one. current.next = head return head def traverse(circular_head): if not circular_head: print("The list is empty.") return start = circular_head while True: print(start.data, end=' ') start = start.next if start == circular_head: break ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值