Java实现单项无头链表

1. 链表

1.1 链表的概念及结构

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

实际中链表的结构非常多样,以下情况组合起来就有8种链表结构:
单向、双向

带头、不带头

循环、非循环 

无头单向非循环链表:结构简单,一般不会单独用来存数据。实际中更多是作为其他数据结构的子结构,如哈 希桶、图的邻接表等等。另外这种结构在笔试面试中出现很多。

无头双向链表:在Java的集合框架库中LinkedList底层实现就是无头双向循环链表。 

1.2 无头单项非循环链表实现

代码如下

class Node {
    public int data;
    public Node next;
    public Node() {

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

 public class LinkedList {
    public Node head;

    //头插法
    public void addFirst(int data){
        Node node = new Node(data);
        if(this.head == null) {
            this.head = node;
            return;
        }
        node.next =this.head;
        this.head = node;
    }
    //尾插法
    public void addLast(int data) {
        Node node = new Node(data);
        if(this.head == null) {
            this.head = node;
            return;
        }
        Node cur = this.head;
        while (cur.next != null) {
            cur = cur.next;
        }
        cur.next = node;
    }
    //任意位置插入数据
     public void addIndex(int index, int elem) {
         Node node = new Node(elem);
         int len = size();
         if (index < 0 || index > len) {
             return;
         }
         //1.头插
         if (index == 0) {
             addFirst(elem);
             return;
         }
         Node cur = this.head;
         for (int i = 0; i < index - 1; i++) {
             cur = cur.next;
         }
         Node prev = cur;
         //先连右边后连左边
         node.next = prev.next;
         prev.next = node;
     }
     //删除关键字key
     public void remove(int key) {
         //头节点
         if (head == null) {
             return;
         }
         if (head.data == key) {
             this.head = this.head.next;
             return;
         }
             Node prev = searchPrev(key);
         if(prev != null) {
             prev.next = prev.next.next;
         }
     }
    //删除所有的key
     public void removeAllkey(int key) {
         if (head == null) {
             return;
         }
         Node prev = this.head;
         Node cur = this.head.next;
         while (cur != null) {
             if (cur.data == key) {
                 prev.next = cur.next;
                 cur = prev.next;
             }else {
                 prev = cur;
                 cur = cur.next;
             }
         }
        if (head.data == key) {
            head = head.next;
        }
     }
     //查找关键字的前驱
     private Node searchPrev(int toRemove) {
         Node prev = this.head;
         while (prev.next != null) {
             if (prev.next.data == toRemove) {
                 return prev;
             }
             prev = prev.next;
         }
         return null;
     }
     //打印链表
     public void display() {
         System.out.print("[");
         for (Node node = this.head; node != null;
              node = node.next) {
             System.out.print(node.data);
             if (node.next != null) {
                 System.out.print(", ");
             }
         }
         System.out.println("]");
     }
     //链表长度
     public int size() {
        int size = 0;
        for(Node cur = this.head;cur != null;
            cur = cur.next)  {
            size++;
        }
        return  size;
     }
     //清空链表
     public void clear() {
        this.head = null;
     }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是Java语言实现一个无头链表的代码: ```java public class ListNode { int val; ListNode next; public ListNode(int val) { this.val = val; this.next = null; } } public class LinkedList { ListNode head; public LinkedList() { this.head = null; } public void add(int val) { ListNode newNode = new ListNode(val); if (head == null) { head = newNode; } else { ListNode cur = head; while (cur.next != null) { cur = cur.next; } cur.next = newNode; } } public void printList() { ListNode cur = head; while (cur != null) { System.out.print(cur.val + " "); cur = cur.next; } System.out.println(); } } ``` 代码解释: 首先定义一个 ListNode 类,表示链表中的每个节点,包含一个整数值 val 和指向下一个节点的指针 next。 然后定义一个 LinkedList 类,表示无头链表,包含一个头结点 head,初始值为 null。 在 LinkedList 类中,定义一个 add 方法,用于在链表尾部添加新元素。如果链表为空,直接将新元素作为头结点;否则,遍历链表,找到最后一个节点并将新元素插入其 next 指针所指的位置。 最后,定义一个 printList 方法,用于遍历链表并打印每个节点的值。 使用示例: ```java LinkedList list = new LinkedList(); list.add(1); list.add(2); list.add(3); list.printList(); // 输出:1 2 3 ``` 注意,这是一个简单的无头链表实现,只包含了添加元素和遍历链表的基本操作。在实际应用中,需要根据具体需求进行扩展和优化。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值