Java无头单向链表

16 篇文章 0 订阅
链表:

是一种物理存储结构上非连续存储结构,数据元素的逻辑顺序是通过链表中的引用链接次序实现的 。

实际中链表的结构非常多样,以下情况组合起来就有8种链表结构:
单向、双向
带头、不带头
循环、非循环
对这六种排列组合即可得到八种链表的名称;

我们今天实现的是 无头单向链表


```java
 // 1、无头单向非循环链表实现 
 public class SingleLinkedList {     
 //头插法     
 public void addFirst(int data);     
 //尾插法     
 public void addLast(int data);     
 //任意位置插入,第一个数据节点为0号下标     
 public boolean addIndex(int index,int data);     
 //查找是否包含关键字key是否在单链表当中     
 public boolean contains(int key);     
 //删除第一次出现关键字为key的节点     
 public void remove(int key);     
 //删除所有值为key的节点     
 public void removeAllKey(int key);     
 //得到单链表的长度     
 public int size();    
 public void display();    
  public void clear(); }

`


```java

```java
class LinkedNode{
    public int data;   
    public LinkedNode next = null;

    public LinkedNode(int data) {    //构造方法
        this.data = data;
    }
}
public class LinkedList {  //头插
    private LinkedNode head = null;

    //创建第一个结点
    public void addFirst(int elem) {
        LinkedNode node = new LinkedNode(elem);
        if (head == null) {    //空链表
            this.head = node;
            return;
        } else {
            node.next = head;
            this.head = node;
            return;
        }
    }

    //显示链表
    public void display() {
        LinkedNode prv;
        if (this.head == null) {
            System.out.println("空链表!");
            return;
        }
        System.out.print("[");
        for (prv = this.head; prv != null; prv = prv.next) {
            if (prv.next != null) {
                System.out.print(prv.data + ",");
            } else System.out.print(prv.data);
        }
        System.out.print("]");
    }

    //尾插
    public void addLast(int elem) {
        LinkedNode node = new LinkedNode(elem);
        if (this.head == null) {  //空链表
            this.head = node;
            return;
        }
        LinkedNode cur = this.head;
        while (cur.next != null) {
            cur = cur.next;
        }
        cur.next = node;
    }

    //任意添加结点
    public void addIndex(int index, int elem) {
        LinkedNode node = new LinkedNode(elem);
        int len = size();
        if (index < 0 || index > len) {
            return;
        }
        if (index == 0) {  //头插
            addFirst(elem);
            return;
        }
        if (index == len) {//尾插
            addLast(elem);
            return;
        }
        //中间插入
        LinkedNode cur = this.head;
        for (int i = 0; i < index - 1; i++) {
            cur = cur.next;
        }
        node.next = cur.next;
        cur.next = node;
        return;
    }

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

    //判断元素是否存在
    public boolean contains(int toFind) {
        // 直接遍历链表, 依次比较每个元素就行了
        if(this.head==null){
            return false;
        }
        LinkedNode cur=this.head;
        for(cur=this.head;cur!=null;cur=cur.next){
            if(cur.data==toFind){
                return true;
            }
        }
        return false;
    }
    //删除指定值
    public void remove(int toRemove) {
        if(this.head==null){  //空链表情况
            System.out.println("空链表!");
            return ;
        }
        //头删除
        if(toRemove==this.head.data){
            this.head=head.next;
            return;
        }
        //别的地方删除,先找前面结点
        LinkedNode prv=getPlace(toRemove);
        prv.next=prv.next.next;
    }
    private LinkedNode getPlace(int toRemove){
        LinkedNode cur=this.head;
        for(;cur!=null;cur=cur.next){
            if(cur.next.data==toRemove){
                return cur;
            }
        }
        return null;
    }
    //删除所有key的值
    public void removeAllKey(int elem){
        if(this.head==null){
            return;
        }
        LinkedNode prev=head;
        LinkedNode cur=head.next;
        while(cur!=null){
            if(cur.data==elem){
            prev.next=cur.next;
            cur=prev.next;
            }
            else {
                prev=cur;
                cur=cur.next;
            }
        }

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

    }

    public void clear(){
        this.head=null;
    }
}

``

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当我们需要在单向无头链表中查找某个元素时,我们需要遍历整个链表,直到找到目标元素或者链表结束。下面是一个示例的单向无头链表查找代码: ```c++ #include <iostream> // 定义链表节点结构 struct Node { int data; // 节点数据 Node* next; // 指向下一个节点的指针 }; // 在链表尾部插入新节点 void insert(Node** head, int data) { Node* newNode = new Node(); // 创建新节点 newNode->data = data; newNode->next = nullptr; if (*head == nullptr) { *head = newNode; } else { Node* currNode = *head; while (currNode->next != nullptr) { currNode = currNode->next; } currNode->next = newNode; } } // 在链表中查找目标元素 bool search(Node* head, int target) { Node* currNode = head; while (currNode != nullptr) { if (currNode->data == target) { return true; } currNode = currNode->next; } return false; } // 打印链表元素 void display(Node* head) { Node* currNode = head; while (currNode != nullptr) { std::cout << currNode->data << " "; currNode = currNode->next; } std::cout << std::endl; } int main() { Node* head = nullptr; // 插入节点 insert(&head, 1); insert(&head, 2); insert(&head, 3); insert(&head, 4); // 打印链表 display(head); // 查找元素 int target = 3; if (search(head, target)) { std::cout << target << " found in the list." << std::endl; } else { std::cout << target << " not found in the list." << std::endl; } return 0; } ``` 在上面的代码中,我们定义了一个 `Node` 结构表示链表的节点,其中 `data` 存储节点的数据,`next` 指向下一个节点。`insert` 函数用于在链表尾部插入新节点。`search` 函数用于在链表中查找目标元素,并返回是否找到。`display` 函数用于打印链表的所有元素。 在 `main` 函数中,我们创建一个空链表,并插入一些节点。然后,我们调用 `display` 函数打印链表,再调用 `search` 函数查找目标元素是否存在。最后输出结果。 注意:这只是一个简单的示例代码,实际应用中可能需要处理更多的边界情况和错误处理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值