Java模拟实现无头节点的单链表

/**
 *  模拟没有头结点的单向链表 直接将首元节点作为头结点
 *
 */

public class SingleLinkedList {


    private class Node {
        int val;
        Node next;

        public Node() {
        }

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

        public Node(int val, Node next) {
            this.val = val;
            this.next = next;
        }
    }

    public Node headNode = null;

    public SingleLinkedList() {
    }

    //头插法
    public void addFirst(int data) {
        if(headNode == null){
            Node node = new Node(data);
            headNode = node;
            return;
        }

        Node newNode = new Node(data);
        newNode.next = headNode;
        headNode= newNode;

    }

  //尾插法
    public void addLast(int data){
        Node node = headNode;
        while(node.next != null){
            node = node.next;
        }
        Node newNode = new Node(data);
        node.next = newNode;
    }

    //任意位置插入,第一个数据节点为0号下标
    //要在0号数据节点插入 用户输入的是index = 1
    public boolean addIndex(int index, int data){
        index = index -1;

        if(index < 0){
            return false;
        }
        if(headNode == null){
            headNode = new Node(data);
            return true;
        }

        //第一个节点直接头插
        if(index == 0){
            addFirst(data);
            return true;
        }

        //插入找前驱
        Node curPre = headNode;
        Node cur = headNode;
        for (int i = 0; i < index; i++) {
            if(cur == null){
                return false;
            }
            curPre = cur;
            cur = cur.next;
        }
        Node newNode = new Node(data);
        newNode.next = cur;
        curPre.next = newNode;
        return true;
    }



    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key){
        if(headNode == null){
            return false;
        }
        Node cur = headNode;
        while(cur != null){
           return ((cur.val == key) ? true:false);
        }
        return false;
    }

    //删除第一次出现关键字为key的节点
    public void remove(int key){
        if(headNode == null){
            return;
        }
        //headNode为要删除的节点 直接删除
        if(headNode.val == key){
            headNode = headNode.next;
            return;
        }

        Node cur = headNode;
        Node curPre = headNode;
        //删除找前驱
        while(cur != null){
            curPre = cur;
            cur = cur.next;
            if(cur.val == key){
                curPre.next = cur.next;
                return;
            }
        }
    }

    //删除所有值为key的节点
    public void removeAllKey(int key){
        if(headNode == null){
            return;
        }
        //从第二个节点开始判断是否满足删除条件
        Node cur = headNode.next;
        Node curPre = headNode;
        while(cur != null){
            if(cur.val == key){
                curPre.next = cur.next;
            }else{
                curPre = cur;
            }
            cur = cur.next;
        }

        //再判断第一个节点是否满足删除条件
        if(headNode.val == key){
            headNode = headNode.next;
        }
    }


    //得到单链表的长度

    public int size(){
        int count = 0;
        Node cur = headNode;
        while(cur != null){
            count++;
        }
        return count;
    }

    public void display(){
        Node cur = headNode;
        while(cur != null){
            System.out.print(cur.val+" ");
            cur = cur.next;
        }
        System.out.println();
    }

    public void clear(){
        headNode = null;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
### 回答1: 以下是一个示例无头节点单链表代码,包括节点的定义、插入、删除、查找和输出操作: ```python class ListNode: def __init__(self, val=0, next=None): self.val = val self.next = next class LinkedList: def __init__(self): self.head = None def insert(self, val): new_node = ListNode(val) new_node.next = self.head self.head = new_node def delete(self, val): prev = None curr = self.head while curr is not None and curr.val != val: prev = curr curr = curr.next if curr is not None: if prev is None: self.head = curr.next else: prev.next = curr.next def search(self, val): curr = self.head while curr is not None and curr.val != val: curr = curr.next return curr def print_list(self): curr = self.head while curr is not None: print(curr.val, end=' ') curr = curr.next print() ``` 在这个代码中,节点使用 `ListNode` 类来表示,每个节点包括一个值和一个指向下一个节点的指针。链表使用 `LinkedList` 类来表示,包括一个头节点 `head`,初始化为 `None`。 `insert` 方法用于在链表头部插入一个新节点,首先创建一个新节点,然后将它的 `next` 指针指向当前的头节点,最后将头节点指向新节点。 `delete` 方法用于从链表中删除一个值为 `val` 的节点,首先遍历整个链表找到要删除的节点,然后修改它前面节点的 `next` 指针即可。需要注意的是,如果要删除的节点是头节点,需要特殊处理。 `search` 方法用于在链表中查找一个值为 `val` 的节点,遍历整个链表即可。 `print_list` 方法用于按顺序输出整个链表中的节点值。 ### 回答2: 无头节点单链表是指链表中没有一个特定的头节点,而是通过记录链表的第一个节点来代表整个链表的首部。下面是一个示例的无头节点单链表的代码实现: ```python class ListNode: def __init__(self, value): self.value = value self.next = None class LinkedList: def __init__(self): self.head = None def is_empty(self): return self.head is None def insert(self, value): new_node = ListNode(value) if self.is_empty(): self.head = new_node else: current = self.head while current.next: current = current.next current.next = new_node def delete(self, value): if self.is_empty(): return if self.head.value == value: self.head = self.head.next return current = self.head while current.next: if current.next.value == value: current.next = current.next.next return current = current.next def search(self, value): if self.is_empty(): return False current = self.head while current: if current.value == value: return True current = current.next return False ``` 以上是无头节点单链表的基本操作代码。链表包含两个类,ListNode 表示链表节点,LinkedList 表示链表本身。其中,insert 方法用于插入节点链表尾部,delete 方法用于删除指定值的节点,search 方法用于查找链表中是否存在指定值的节点。 ### 回答3: 无头节点单链表是指链表中没有头节点,只有数据节点。在这种情况下,我们需要自己实现链表的插入、删除、查找等操作。 定义一个节点类,用来表示链表节点,该节点包含两个属性:数据和下一个节点的指针。 ```python class Node: def __init__(self, data=None): self.data = data self.next = None ``` 接下来,我们定义一个LinkedList类,该类包含一些对链表进行操作的方法。 ```python class LinkedList: def __init__(self): self.head = None def insert(self, data): new_node = Node(data) if self.head is None: self.head = new_node else: current = self.head while current.next is not None: current = current.next current.next = new_node def delete(self, data): if self.head is None: return if self.head.data == data: self.head = self.head.next return current = self.head while current.next is not None: if current.next.data == data: current.next = current.next.next return current = current.next def search(self, data): current = self.head while current is not None: if current.data == data: return True current = current.next return False def display(self): current = self.head while current is not None: print(current.data, end=" ") current = current.next print() ``` 上述代码中,我们实现了插入、删除、查找和展示链表数据的方法。我们可以通过创建一个LinkedList对象并调用这些方法来操作链表。 需要注意的是,在无头节点单链表中,我们需要特别处理链表的头部插入和删除操作。 这样,我们就完成了一个无头节点单链表实现

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

小小小小关同学

你的支持就是我的动力

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

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

打赏作者

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

抵扣说明:

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

余额充值