开发者社区> 问答> 正文

如果要你自己实现一个链表,实现数据的增删改查,需要怎么做?

如果要你自己实现一个链表,实现数据的增删改查,需要怎么做?

展开
收起
游客pklijor6gytpx 2019-12-02 16:18:01 350 0
1 条回答
写回答
取消 提交回答
  • class Node {
        private Object obj;
        Node next;
        public Node(Object obj) {
            this.obj = obj;
        }
        public Object value() {
            return obj;
        }
    }
    public class LinkedList {
        private Node head;
        private Node tail;
        private int size;
        public void add(Object obj) {
            Node node = new Node(obj);
            if (null == tail) {
                head = node;
                tail = node;
            } else {
                tail.next = node;
                tail = node;
            }
            size ++;
        }
        public Object get(int index) {
            if (null == head) return null;
            Node current = head;
            for (int i = 0; i <= index; i ++) {
                if (null == current) return null;
                if (i == index) {
                    return current.value();
                }
                current = current.next;
            }
            return null;
        }
        public int size() {
            return size;
        }
    }
    
    2019-12-02 16:18:14
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载