コード例 #1
0
ファイル: LRUCache.cs プロジェクト: rvats/MainDSA
        public void Put(int key, int value)
        {
            DoublyListNode node;

            myDictionary.TryGetValue(key, out node);
            if (node == null)
            {
                if (this.count == this.capacity)
                {
                    // remove the last element
                    myDictionary.Remove(head.Previous.KeyValue.Key);
                    head.Previous      = head.Previous.Previous;
                    head.Previous.Next = head;

                    count--;
                }

                // create new node and add to dictionary
                var newNode = new DoublyListNode(key, value);
                myDictionary[key] = newNode;

                this.InsertAfterTheHead(newNode);

                // increase count
                count++;
            }
            else
            {
                node.KeyValue = new KeyValuePair <int, int>(key, value);
                this.MoveItToFirstElementAfterHead(node);
            }
        }
コード例 #2
0
        public DoublyLinkedList <T> Remove(DoublyListNode <T> node)
        {
            if (node != null)
            {
                if (node.prev != null)
                {
                    node.prev.next = node.next;
                }
                if (node.next != null)
                {
                    node.next.prev = node.prev;
                }

                if (node == head)
                {
                    head = head.next;
                }

                if (node == tail)
                {
                    tail = tail.prev;
                }
            }

            return(this);
        }
コード例 #3
0
    /** Get the value of the index-th node in the linked list. If the index is invalid, return -1. */
    public int Get(int index)
    {
        if (index < 0 || index >= size)
        {
            return(-1);
        }

        // 获取伪头结点
        DoublyListNode curr = head;

        // 判断从前往后遍历还是从后往前遍历
        if (index * 2 < size - 1)
        {
            for (int i = 0; i < index + 1; ++i)
            {
                curr = curr.next;
            }
        }
        else
        {
            curr = tail;
            for (int i = 0; i < size - index; ++i)
            {
                curr = curr.prev;
            }
        }

        return(curr.val);
    }
コード例 #4
0
        public void Add(T value)
        {
            var node = new DoublyListNode <T>(value);

            map.Add(value, node);
            AddToHead(node);
        }
コード例 #5
0
 public MyLinkedList()
 {
     size      = 0;
     head      = new DoublyListNode(0);
     tail      = new DoublyListNode(0);
     head.next = tail;
     tail.prev = head;
 }
コード例 #6
0
            private void RemoveFromList(DoublyListNode target)
            {
                var pre  = target.pre;
                var next = target.next;

                next.pre = target.pre;
                pre.next = target.next;
            }
コード例 #7
0
 private void MoveToHead(DoublyListNode <T> node)
 {
     node.Next.Prev = node.Prev;
     node.Prev.Next = node.Next;
     node.Next      = head.Next;
     node.Prev      = null;
     head.Next      = node;
 }
コード例 #8
0
ファイル: LRUCache.cs プロジェクト: rvats/MainDSA
 private void InsertAfterTheHead(DoublyListNode node)
 {
     // insert after the head
     node.Next               = this.head.Next;
     node.Previous           = this.head;
     this.head.Next.Previous = node;
     this.head.Next          = node;
 }
コード例 #9
0
ファイル: LRUCache.cs プロジェクト: rvats/MainDSA
 public LRUCache(int capacity)
 {
     head          = new DoublyListNode(-1, -1);
     head.Next     = head;
     head.Previous = head;
     this.capacity = capacity;
     myDictionary  = new Dictionary <int, DoublyListNode>();
 }
コード例 #10
0
        private DoublyListNode GetTail()
        {
            DoublyListNode cur = _head;

            while (cur?.Next != null)
            {
                cur = cur.Next;
            }
            return(cur);
        }
コード例 #11
0
        private DoublyListNode GetNode(int index)
        {
            DoublyListNode cur = _head;

            for (var i = 0; i < index && cur != null; i++)
            {
                cur = cur.Next;
            }
            return(cur);
        }
コード例 #12
0
            private void InsertAtHead(DoublyListNode target)
            {
                var pre  = dummyHead;
                var next = dummyHead.next;

                next.pre    = target;
                target.next = next;
                pre.next    = target;
                target.pre  = pre;
            }
コード例 #13
0
 public LRUCache_Version1(int capacity)
 {
     this.capacity  = capacity;
     dummyHead      = new DoublyListNode(0);
     dummyTail      = new DoublyListNode(0);
     dummyHead.next = dummyTail;
     dummyTail.pre  = dummyHead;
     dict           = new Dictionary <int, DoublyListNode>();
     dictReverse    = new Dictionary <DoublyListNode, int>();
 }
コード例 #14
0
        public void Print()
        {
            DoublyListNode <T> ptr = head.Next;

            while (ptr != null)
            {
                Console.Write($"{ptr.Value}, ");
                ptr = ptr.Next;
            }
            Console.WriteLine();
        }
コード例 #15
0
 public DoublyLinkedList <T> RemoveHead()
 {
     if (this.head != null)
     {
         this.head = this.head.next;
         if (this.head == null)
         {
             this.tail = null;
         }
     }
     return(this);
 }
コード例 #16
0
        public void AddAtTail(int val)
        {
            if (_head == null)
            {
                AddAtHead(val);
                return;
            }
            DoublyListNode prev = GetTail();
            DoublyListNode cur  = new DoublyListNode(val);

            prev.Next = cur;
            cur.Prev  = prev;
        }
コード例 #17
0
 private void AddToHead(DoublyListNode <T> node)
 {
     if (head.Next == null)
     {
         // First node
         head.Next = node;
     }
     else
     {
         node.Next      = head.Next;
         head.Next.Prev = node;
         head.Next      = node;
     }
 }
コード例 #18
0
        public void AddAtHead(int val)
        {
            DoublyListNode cur = new DoublyListNode(val)
            {
                Next = _head
            };

            if (_head != null)
            {
                _head.Prev = cur;
            }
            _head = cur;
            return;
        }
コード例 #19
0
    /** Append a node of value val to the last element of the linked list. */
    public void AddAtTail(int val)
    {
        // succ tail是伪尾节点
        // pred是最后一个节点
        DoublyListNode succ = tail, pred = tail.prev;

        ++size;
        DoublyListNode toAdd = new DoublyListNode(val);

        toAdd.prev = pred;
        toAdd.next = succ;
        pred.next  = toAdd;
        succ.prev  = toAdd;
    }
コード例 #20
0
        public T GetRandom()
        {
            Random             rand  = new Random();
            int                max   = rand.Next(map.Keys.Count);
            int                count = 0;
            DoublyListNode <T> ptr   = head.Next;

            while (count < max && ptr != null)
            {
                ptr = ptr.Next;
                count++;
            }

            return(ptr.Value);
        }
コード例 #21
0
        public DoublyLinkedList <T> InsertBefore(DoublyListNode <T> node1, DoublyListNode <T> node2)
        {
            if (node1.prev != null)
            {
                node1.prev.next = node1.next;
            }
            if (node1.next != null)
            {
                node1.next.prev = node1.prev;
            }

            node1.next = node2;
            node1.prev = node2.prev;
            node2.prev = node1;
            return(this);
        }
コード例 #22
0
    /** Add a node of value val before the first element of the linked list. After the insertion, the new node will be the first node of the linked list. */
    public void AddAtHead(int val)
    {
        // 伪头结点
        // succ 第一个节点
        DoublyListNode pred = head, succ = head.next;

        ++size;
        // 新加的头结点
        DoublyListNode toAdd = new DoublyListNode(val);

        // 头结点上一个是哨兵伪头结点
        toAdd.prev = pred;
        // 下一个是之前的第一个节点
        toAdd.next = succ;
        pred.next  = toAdd;
        succ.prev  = toAdd;
    }
コード例 #23
0
        public DoublyLinkedList <T> InsertAtHead(DoublyListNode <T> node)
        {
            if (node.prev != null)
            {
                node.prev.next = node.next;
            }
            if (node.next != null)
            {
                node.next.prev = node.prev;
            }

            node.next = this.head;
            node.prev = null;
            this.head = node;
            this.tail ??= this.head;
            return(this);
        }
コード例 #24
0
    /** Add a node of value val before the index-th node in the linked list. If index equals to the length of linked list, the node will be appended to the end of linked list. If index is greater than the length, the node will not be inserted. */
    public void AddAtIndex(int index, int val)
    {
        if (index > size)
        {
            return;
        }

        if (index < 0)
        {
            index = 0;
        }

        // find predecessor and successor
        // 前任, 继承者
        DoublyListNode pred, succ;

        // 判断从前往后方便还是从后往前方便
        if (index < size - index)
        {
            pred = head;
            for (int i = 0; i < index; ++i)
            {
                pred = pred.next;
            }
            succ = pred.next;
        }
        else
        {
            succ = tail;
            for (int i = 0; i < size - index; ++i)
            {
                succ = succ.prev;
            }
            pred = succ.prev;
        }

        // insertion itself
        ++size;
        DoublyListNode toAdd = new DoublyListNode(val);

        toAdd.prev = pred;
        toAdd.next = succ;
        pred.next  = toAdd;
        succ.prev  = toAdd;
    }
コード例 #25
0
            public void Put(int key,
                            int value)
            {
                if (Get(key) != -1)
                {
                    dict[key].val = value;
                    return;
                }

                var target = new DoublyListNode(value);

                dict.Add(key,
                         target);
                dictReverse.Add(target,
                                key);

                InsertAtHead(target);
                RemoveLeastRecentlyUsed();
            }
コード例 #26
0
        public DoublyLinkedList <T> InsertAtTail(DoublyListNode <T> node)
        {
            if (node.prev != null)
            {
                node.prev.next = node.next;
            }
            if (node.next != null)
            {
                node.next.prev = node.prev;
            }

            node.prev = this.tail;
            if (this.tail != null)
            {
                this.tail.next = node;
            }
            node.next = null;
            this.tail = node;
            this.head ??= this.tail;
            return(this);
        }
コード例 #27
0
        private DoublyListNode DfsInOrder(TreeNode root)
        {
            Stack <TreeNode> stack = new Stack <TreeNode>();
            TreeNode         curr  = root;
            DoublyListNode   head  = null;
            DoublyListNode   prev  = null;

            while (curr != null || stack.Any())
            {
                while (curr != null)
                {
                    stack.Push(curr);
                    curr = curr.left;
                }

                curr = stack.Pop();
                // Link curr to prev.
                DoublyListNode node = new DoublyListNode(curr.val)
                {
                    prev = prev
                };
                if (head == null)
                {
                    head = node;
                }

                // Link prev to its next.
                if (prev != null)
                {
                    prev.next = node;
                }
                prev = node;

                // Continue dfs in-order
                curr = curr.right;
            }

            return(head);
        }
コード例 #28
0
 private void RemoveFromList(DoublyListNode <T> node)
 {
     if (node.Prev == null)
     {
         // First node
         head.Next      = node.Next;
         node.Next.Prev = null;
         node           = null;
     }
     else if (node.Next == null)
     {
         // Last node
         node.Prev.Next = null;
         node.Prev      = null;
         node           = null;
     }
     else
     {
         node.Prev.Next = node.Next;
         node.Next.Prev = node.Prev;
         node           = null;
     }
 }
コード例 #29
0
        public void DeleteAtIndex(int index)
        {
            DoublyListNode cur = GetNode(index);

            if (cur == null)
            {
                return;
            }
            DoublyListNode prev = cur.Prev;
            DoublyListNode next = cur.Next;

            if (prev != null)
            {
                prev.Next = next;
            }
            else
            {
                _head = next;
            }
            if (next != null)
            {
                next.Prev = prev;
            }
        }
コード例 #30
0
        public void AddAtIndex(int index, int val)
        {
            if (index == 0)
            {
                AddAtHead(val);
                return;
            }
            var prev = GetNode(index - 1);

            if (prev == null)
            {
                return;
            }
            DoublyListNode cur  = new DoublyListNode(val);
            DoublyListNode next = prev.Next;

            cur.Prev  = prev;
            cur.Next  = next;
            prev.Next = cur;
            if (next != null)
            {
                next.Prev = cur;
            }
        }