一个固定长度活跃记录链表-数据结构(Java)

固定长度活跃记录链表

这篇博客具体的描述了一个数据结构,这个数据结构我暂且命名为固定长度活跃记录链表。

链表大致的作用机理

  1. 维系一个固定数量的活跃记录链表,最新插入的记录在表头,记录越活跃就越靠前,当记录长度超过固定长度,会将新记录插入,把尾记录丢掉,达到维持固定长度的目的。
  2. 当一个新的记录插入,链表中如果存在一个记录所含的值与新记录相同,则更新该记录的值并移动到头节点,其他的记录按顺序后移。

我的实际场景是,需要存储键值对,为了方便,我将Node当中的值定位String,以冒号将键和值隔开。
存储 数据结构为 键 + :+值
获取 输入 得到
链表是双向链表结构,即一个节点有头指针和尾指针。

public static class FixedActiveList{
        private static class Node {
            String item;
            Node next;
            Node prev;

            Node(Node prev, String element, Node next) {
                this.item = element;
                this.next = next;
                this.prev = prev;
            }
        }

        private Node head;
        private Node tail;
        private int capacity;
        private int len;

        /**
         * 初始化
         * @param capacity 固定长度
         */
        public FixedActiveList(int capacity){
            this.capacity = capacity;
        }


        /**
         * 添加元素
         * 从头开始遍历表
         * 含有元素,则调到头,其余顺序后移
         */
        public void add(String item)
        {
            String index = item.substring(0, item.indexOf(':'));
            if(head != null)
            {
                Node tmp = head;
                while (tmp != null)
                {
                    if(tmp.item.startsWith(index))
                    {//存在对应记录
                        tmp.item = item;
                        if(tmp.prev != null)
                        {//非头
                            //将旧记录活跃成头节点,其余顺序下移

                            if(tmp.next == null)//是尾
                                tail = tmp.prev;
                            else
                                tmp.next.prev = tmp.prev;

                            tmp.prev.next = tmp.next;
                            tmp.next = head;
                            head.prev = tmp;
                            head = tmp;
                            tmp.prev = null;
                        }
                        break;
                    }
                    tmp = tmp.next;
                }

                if(tmp==null)
                {//没有匹配项
                    Node node = new Node(null, item, head);
                    head.prev = node;
                    head = node;

                    if(len == capacity)
                    {//固定列表长度
                        tail = tail.prev;
                        tail.next = null;
                    }else {
                        len++;
                    }
                }
            }else
            {//第一个元素的插入
                head = new Node(null, item, null);
                tail = head;
                len++;
            }
        }

        /**
         * 查询元素
         */
        public String get(String index)
        {
            Node tmp = head;
            String result = "No cache here please clear cache";
            while (tmp != null)
            {
                if(tmp.item.startsWith(index))
                {
                    result = tmp.item.substring(tmp.item.indexOf(':')+1);
                    break;
                }
                tmp = tmp.next;
            }
            return result;
        }

    }

测试案例

    @Test
    public void test()
    {
        FixedActiveList fixedActiveList = new FixedActiveList(5);
        fixedActiveList.add("index1:1-2");
        fixedActiveList.add("index2:1-2");
        fixedActiveList.add("index3:1-2");
        fixedActiveList.add("index4:1-2");
        fixedActiveList.add("index5:1-2");
        fixedActiveList.add("index5:2-2");
        fixedActiveList.add("index1:1-21");
        fixedActiveList.add("index3:2-2");
        fixedActiveList.add("index6:1-2");
        fixedActiveList.add("index7:1-2");


        System.out.println(fixedActiveList.get("index1"));
        System.out.println(fixedActiveList.get("index2"));
        System.out.println(fixedActiveList.get("index3"));
        System.out.println(fixedActiveList.get("index4"));
        System.out.println(fixedActiveList.get("index5"));
        System.out.println(fixedActiveList.get("index6"));
        System.out.println(fixedActiveList.get("index7"));
    }

创作不易,谢谢支持!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
好的,以下是用Java定义一个链表数据结构的例子: ``` public class LinkedList { Node head; // 链表头节点 static class Node { int data; // 此节点存储的数据 Node next; // 下一个节点 Node(int d) { // 构造函数 data = d; next = null; } } // 添加新节点到链表末尾 public void append(int data) { Node newNode = new Node(data); if (head == null) { head = newNode; return; } Node last = head; while (last.next != null) { last = last.next; } last.next = newNode; } // 在指定位置插入节点 public void insert(int data, int position) { Node newNode = new Node(data); if (position == 0) { newNode.next = head; head = newNode; return; } Node previous = null; Node current = head; for (int i = 0; i < position; i++) { previous = current; current = current.next; if (current == null) { break; } } newNode.next = current; previous.next = newNode; } // 删除指定位置的节点 public void delete(int position) { if (head == null) { return; } Node current = head; if (position == 0) { head = head.next; return; } Node previous = null; for (int i = 0; i < position; i++) { previous = current; current = current.next; if (current == null) { return; } } previous.next = current.next; } // 打印链表中的所有节点 public void printList() { Node current = head; while (current != null) { System.out.print(current.data + " "); current = current.next; } System.out.println(); } } ``` 以上就是一个简单的链表数据结构的定义,可以用来存储整数型数据。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值