java实现双向链表的增删改查

实现思路:

代码:

 

public class DoubleLinkedList {
    private Person head = new Person(0,"");
    // 获取头结点
    public Person getHead(){
        return head;
    }

    // 添加-- 直接添加,没有按顺序添加
    public void add(Person headNode,Person node){
        if(headNode.next == null){// 只有头结点的时候
            headNode.next = node;
            node.pre = headNode;
            return;
        }
        Person temp = headNode;
        while(true){// 寻找最后一个节点
            if(temp.next == null){// 最后一个节点
                temp.next = node;
                node.pre = temp;
                break;
            }
            temp = temp.next;
        }
    }
    // 按顺序添加节点
    public void addByOrder(Person headNode,Person node){
        /**
         * 1.链表为空的第一个节点插入
         * 2.在中间插入
         * 3.在尾部插入
         */
        boolean flag = true;
        Person temp = headNode.next;
        while(true){
            if(temp == null){// 首节点插入
                headNode.next = node;
                node.pre = headNode;
                flag = false;
                break;
            }
            if(node.id < temp.id){// 中间插入
                temp.pre.next = node;
                node.pre = temp.pre;
                node.next = temp;
                temp.pre =node;
                flag = false;
                break;
            }
            if(node.id == temp.id){ // 已经存在
                System.out.println("节点已存在。。");
                flag = false;
                break;
            }
            if(temp.next == null){// 如果已经没有节点了,就退出
                break;
            }
            temp = temp.next;
        }

        if(flag){// 尾部插入的节点
            temp.next = node;
            node.pre = temp;
        }
    }

    // 修改
    public void update(Person updateNode){
        boolean flag = true;
        Person temp = head.next;
        while(true){
            if(head.next == null){
                throw new RuntimeException("链表为空。。。");
            }

            if(temp.id == updateNode.id){
//                System.out.println(updateNode);
                temp.name = updateNode.name;
                flag = false;
                break;
            }
            temp = temp.next;
        }
        if(flag){
            System.out.println("该节点不存在。。");
        }
    }

    // 删除
    public void delete(int id){
        boolean flag = true;
        Person temp = head.next;
        if(head.next == null){
            throw new RuntimeException("链表为空。。");
        }
        while(temp != null){
            if(temp.id == id){
               if(temp.next == null){// 如果删除的是最后一个的话
                   temp.pre.next = null;
                   temp.pre = null;
                   flag = false;
                   break;
               }else{
                   temp.pre.next = temp.next;
                   temp.pre = temp.next.pre;
                   flag = false;
                   break;
               }
            }
            temp = temp.next;
        }
        if(flag){
            throw new RuntimeException("链表的该节点不存在。。。");
        }
    }

    // 显示
    public void show(Person headNode){
        if(headNode.next == null){
            throw new RuntimeException("运行时异常。。");
        }
        Person temp = headNode.next;
        while( temp != null){
            System.out.println(temp);
            temp = temp.next;
        }
    }

}

节点

public class Person {
    public int id;
    public String name;
    public Person next;
    public Person pre;

    public Person(int id, String name) {
        this.id = id;
        this.name = name;
    }

    @Override
    public String toString() {
        return "Person{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }
}

测试

public class TestMain {
    public static void main(String[] args) {
        Person person1 = new Person(1,"zs");
        Person person3 = new Person(6,"ww");
        Person person2 = new Person(3,"ls");


       DoubleLinkedList linkedList = new DoubleLinkedList();
       linkedList.addByOrder(linkedList.getHead(),person1);
       linkedList.addByOrder(linkedList.getHead(),person3);
       linkedList.addByOrder(linkedList.getHead(),person2);
       System.out.println("添加以后。。。。");
       linkedList.show(linkedList.getHead());

        System.out.println("修改以后。。。。");
       Person person4 = new Person(3,"zss");
       linkedList.update(person4);
       linkedList.show(linkedList.getHead());
//
       System.out.println("删除以后。。。。");
       linkedList.delete(3);
       linkedList.show(linkedList.getHead());
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 可以使用java中的LinkedHashMap类来实现LRU缓存淘汰策略。LinkedHashMap是一种特殊的HashMap,它不仅保存了键值对,还维护了一个双向链表来记录插入顺序,当缓存满后,它会将最先插入的元素淘汰掉。 ### 回答2: LRU(最近最少使用)缓存淘汰策略是指在缓存满时,优先淘汰最近最少使用的数据。实现LRU缓存淘汰策略可以使用链表和哈希表的组合。 首先,我们可以创建一个双向链表的节点类,包含键值对的信息,以及前后指针。链表的头部表示最近访问的节点,尾部表示最久未使用的节点。 我们还需要一个哈希表,用于快速查找节点。哈希表的键是缓存的键,值是对应的节点。 接下来,我们需要实现缓存淘汰策略的几个操作: 1. 获取操作:当尝试获取缓存中的键值对时,如果哈希表中存在该键,则将对应的节点移动到链表头部,并返回该值。如果哈希表中不存在该键,则返回空。 2. 添加操作:当添加新的键值对时,如果该键已存在于缓存中,则将该节点移到链表头部,更新值。否则,创建新节点,并将其插入到链表头部和哈希表中。如果缓存已满,删除链表尾部的节点,并在哈希表中删除对应的键。 3. 删除操作:当从缓存中删除某个键值对时,找到对应的节点,从链表中删除该节点,并在哈希表中删除该键。 以上就是使用Java链表实现LRU缓存淘汰策略的基本思路。通过使用双向链表和哈希表的组合,能够快速实现缓存的增删改查操作,并保持对节点访问的顺序,以便实现LRU策略。 ### 回答3: LRU(Least Recently Used,最近最少使用)是一种常用的缓存淘汰策略,当缓存满时,会将最长时间未被使用的数据从缓存中淘汰。实现LRU缓存淘汰策略可以使用Java中的链表来实现Java中已经提供了LinkedList类,可以直接用于实现链表数据结构。接下来是一个简单的实现LRU缓存的示例代码: ``` import java.util.LinkedList; public class LRUCache<K, V> { private int capacity; private LinkedList<Entry<K, V>> cache; public LRUCache(int capacity) { this.capacity = capacity; this.cache = new LinkedList<>(); } public V get(K key) { Entry<K, V> entry = findEntryByKey(key); if (entry != null) { // 将使用的数据移动到链表头部 cache.remove(entry); cache.addFirst(entry); return entry.value; } return null; } public void put(K key, V value) { Entry<K, V> entry = findEntryByKey(key); if (entry != null) { // 如果key已存在,更新其value,并将数据移动到链表头部 entry.value = value; cache.remove(entry); cache.addFirst(entry); } else { // 如果key不存在,先判断容量是否已满,如果已满则移除最久未使用的数据 if (cache.size() >= capacity) { cache.removeLast(); } // 将新数据插入到链表头部 Entry<K, V> newEntry = new Entry<>(key, value); cache.addFirst(newEntry); } } private Entry<K, V> findEntryByKey(K key) { for (Entry<K, V> entry : cache) { if (entry.key.equals(key)) { return entry; } } return null; } private static class Entry<K, V> { private K key; private V value; public Entry(K key, V value) { this.key = key; this.value = value; } } } ``` 使用示例: ``` LRUCache<String, Integer> cache = new LRUCache<>(3); cache.put("a", 1); cache.put("b", 2); cache.put("c", 3); System.out.println(cache.get("a")); // 输出1 cache.put("d", 4); System.out.println(cache.get("b")); // 输出null,因为b是最久未使用的数据,已被淘汰 ``` 在LRUCache类中,使用LinkedList作为缓存的数据存储结构。缓存的最近使用的数据总是位于链表的头部,当需要访问或更新数据时,将其移动到链表头部。当缓存已满时,移除链表尾部的最久未使用的数据。 这种实现方式可以在O(1)的时间复杂度内实现get和put操作,符合LRU缓存淘汰策略的特性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值