手写单链表LinkedList,以及实现了LRU算法的单链表

LinkedList

public class LinkedList<T> {
        //记录头部节点
        public Node list;
        //记录链表大小
        public int size;
        //添加节点放在头部
        public void put(T data) {
            Node headNode = list;
            Node currentNode = new Node(data, headNode);
            list = currentNode;
            size++;
        }
        //根据指定位置添加
        public void put(int index, T data) {
            //判断下标是否越界
            testingIndex(index);
            //这是头部节点
            Node headNode = list;
            Node currentNode = list;
            for (int i = 0; i < index; i++) {
                headNode = currentNode;
                currentNode = currentNode.next;
            }
            Node node = new Node(data, currentNode);
            headNode.next = node;
            size++;
        }
        //删除头部节点
        public T remove() {
            if (list != null) {
                Node node = list;
                list = list.next;
                node.next = null;
                size--;
                return list.data;
            }
            return null;
        }
        //根据下标删除节点
        public T remove(int index) {
            testingIndex(index);
            Node headNode = list;
            Node currentNode = list;
            for (int i = 0; i < index; i++) {
                headNode = currentNode;
                currentNode = currentNode.next;
            }
            headNode.next = currentNode.next;
            currentNode.next = null;
            size--;
            return currentNode.data;
        }
        //删除尾部节点
        public T removeLast() {
            if (list != null) {
                Node headNode = list;
                Node currentNode = list;
                while (currentNode.next != null) {
                    headNode = currentNode;
                    currentNode = currentNode.next;
                }
                headNode.next = null;
                size--;
                return currentNode.data;
            }
            return null;
        }
        //修改节点
        public void set(int index, T data) {
            testingIndex(index);
            Node currentNode = list;
            for (int i = 0; i < index; i++) {
                currentNode = currentNode.next;
            }
            currentNode.data = data;
        }
        //查询节点
        public T get(int index) {
            Node headNode = list;
            for (int i = 0; i < index; i++) {
                headNode = list.next;
            }
            return headNode.data;
        }
        public void testingIndex(int index) {
            if (!(index >= 0 && index <= size)) {
                throw new IndexOutOfBoundsException("index:" + index + "size:" + size);
            }
        }
        public void toStirng() {
            for (int i = 0; i < size; i++) {
                System.out.println(list.data);
                list = list.next;
            }
        }
        //节点类
        class Node {
            private T data;
            private Node next;
            public Node(T data, Node next) {
                this.data = data;
                this.next = next;
            }
        }
    }

什么是LRU算法

代码里有注释

 public class LruLinkedList<T> extends LinkedList<T> {
        /**
         * lru算法
         * 新增的数据放在头部,添加的时候超过了内存大小,就删除尾部
         * 缓存命中,即缓存数据被访问时移至头部,
         */
        public static final int defaultSize = 5;
        public int lruSize;

        public LruLinkedList() {
            this(defaultSize);
        }
        public LruLinkedList(int size) {
            this.lruSize = size;
        }
        //增
        public void lruPut(T data) {
            if (size >= lruSize) {
                removeLast();
                put(data);
            } else {
                put(data);
            }
        }
        //删
        public void lruRemove() {
            removeLast();
        }
        //改
        public void lruSet(int index, T data) {
            testingIndex(index);
            Node head = list;
            Node cur = list;
            for (int i = 0; i < index; i++) {
                head = cur;
                cur = cur.next;
            }
            head.next = cur.next;
            cur.next=list;
            cur.data=data;
            list=cur;
        }
        //查
        public void lruGet(int index) {
            testingIndex(index);
            Node head = list;
            Node cur = list;
            for (int i = 0; i < index; i++) {
                head = cur;
                cur = cur.next;
            }
            head.next = cur.next;
            cur.next=list;
            list=cur;
        }
    }

学习算法与数据结构中,作为学习笔记记录
,测试问题不大,希望有大神指点,谢谢

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值