Example #1
0
    private void RemoveNode(DoubleListNode node)
    {
        var before = node.prev;

        before.next    = node.next;
        node.next.prev = before;
    }
        private static DoubleListNode Insert(DoubleListNode head, int data)
        {
            var newNode = new DoubleListNode {
                Data = data
            };
            var pointer = head;

            if (head.Data >= data)
            {
                newNode.Next = head;
                head.Prev    = newNode;
                head         = newNode;
                return(head);
            }

            while (head.Next != null && head.Next.Data < data)
            {
                head = head.Next;
            }

            newNode.Next = head.Next;
            newNode.Prev = head;
            head.Next    = newNode;

            if (head.Next != null && newNode.Next != null)
            {
                newNode.Next.Prev = newNode;
            }

            return(pointer);
        }
Example #3
0
 public LRUCache(int capacity)
 {
     _capacity = capacity;
     _dict     = new Dictionary <int, DoubleListNode>();
     _preHead  = new DoubleListNode(0, 0);
     _tail     = _preHead;
 }
Example #4
0
 public void Set(int key, int value)
 {
     if (!_dict.ContainsKey(key))
     {
         _count++;
         var nn = new DoubleListNode(key, value);
         _dict[key] = nn;
         _tail.next = nn;
         nn.prev    = _tail;
         _tail      = nn;
     }
     else
     {
         var node = _dict[key];
         node.value = value;
         PutToTail(node);
     }
     if (_count > _capacity)
     {
         _count--;
         // remove the node in head
         _dict.Remove(_preHead.next.key);
         _preHead.next      = _preHead.next.next;
         _preHead.next.prev = _preHead;
     }
 }
Example #5
0
            public void AddNodeToFront(int data)                           //adds at the beginning
            {
                DoubleListNode node = new DoubleListNode(data);

                node.next = head;
                head      = node;
                count++;
            }
Example #6
0
    private void AddToCache(int key, int value)
    {
        //add to the end of the list
        var newNode = new DoubleListNode(key, value);

        AddToEnd(newNode);
        //add to the dictionary
        dict.Add(key, newNode);
    }
Example #7
0
 public LRUCache(int capacity)
 {
     this.storage   = new Dictionary <int, DoubleListNode>(capacity);
     this.capacity  = capacity;
     this.head      = new DoubleListNode(int.MinValue, int.MinValue);
     this.tail      = new DoubleListNode(int.MaxValue, int.MaxValue);
     this.head.next = this.tail;
     this.tail.prev = this.head;
 }
Example #8
0
    private void AddToEnd(DoubleListNode node)
    {
        var beforeTail = tail.prev;

        node.prev       = beforeTail;
        beforeTail.next = node;
        tail.prev       = node;
        node.next       = tail;
    }
Example #9
0
        /** Append a node of value val to the last element of the linked list. */
        public void AddAtTail(int val)
        {
            DoubleListNode last = tail.prev;
            DoubleListNode temp = new DoubleListNode(val);

            last.next = temp;
            temp.prev = last;
            temp.next = tail;
            tail.prev = temp;
        }
Example #10
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)
        {
            DoubleListNode temp = new DoubleListNode(val);
            DoubleListNode next = head.next;

            head.next = temp;
            temp.next = next;
            temp.prev = head;
            next.prev = temp;
        }
Example #11
0
    private int currentLength;  //the deque's current length

    /** Initialize your data structure here. Set the size of the deque to be k. */
    public MyCircularDeque(int capacity)
    {
        head = new DoubleListNode(-1);
        tail = new DoubleListNode(-1);

        head.pre      = tail;
        tail.next     = head;
        this.capacity = capacity;
        currentLength = 0;
    }
Example #12
0
        /** Delete the index-th node in the linked list, if the index is valid. */
        public void DeleteAtIndex(int index)
        {
            DoubleListNode node = GetIndex(index);

            if (node.val != -1)
            {
                DoubleListNode pre = node.prev;
                pre.next       = node.next;
                node.next.prev = pre;
            }
        }
Example #13
0
 public LRUCache(int capacity)
 {
     this.capacity = capacity;
     this.count    = 0;
     dict          = new Dictionary <int, DoubleListNode>();
     //fake and head tail to be safe
     head      = new DoubleListNode(0, 0);
     tail      = new DoubleListNode(0, 0);
     head.next = tail;
     tail.prev = head;
 }
Example #14
0
 private void PutToTail(DoubleListNode node)
 {
     if (node != _tail)
     {
         node.prev.next = node.next;
         node.next.prev = node.prev;
         _tail.next     = node;
         node.next      = null;
         node.prev      = _tail;
         _tail          = node;
     }
 }
Example #15
0
            public void PrintList()
            {
                DoubleListNode runner = head;
                DoubleListNode prev   = null;

                while (runner != null)
                {
                    Console.WriteLine(runner.data);
                    prev            = runner;
                    runner.previous = prev;
                    runner          = runner.next;
                }
            }
Example #16
0
        private DoubleListNode GetIndex(int index)
        {
            DoubleListNode cur = head.next;

            for (int i = 0; i < index; i++)
            {
                if (cur != tail)
                {
                    cur = cur.next;
                }
            }
            return(cur);
        }
Example #17
0
    /** Adds an item at the rear of Deque. Return true if the operation is successful. */
    public bool InsertLast(int value)
    {
        if (IsFull())
        {
            return(false);
        }

        DoubleListNode node = new DoubleListNode(value);

        node.next     = tail.next;
        node.pre      = tail;
        tail.next.pre = node;
        tail.next     = node;

        currentLength++;
        return(true);
    }
Example #18
0
        public bool IsPalindrome(ListNode head)
        {
            DoubleListNode dl = null;

            ListNode node = new ListNode(-1);

            node.next = head;
            while (node != null)
            {
                dl.pre  = node;
                dl.val  = node.val;
                dl.next = node.next;

                node = node.next;
            }
            return(false);
        }
Example #19
0
            public void AddNodeToEnd(int data)                             //adds to the end
            {
                DoubleListNode node   = new DoubleListNode(data);
                DoubleListNode runner = head;

                while (runner != null)
                {
                    if (runner.next == null)
                    {
                        runner.next   = node;
                        node.previous = runner;
                        node.next     = null;
                        break;
                    }
                    runner = runner.next;
                }
                count++;
            }
Example #20
0
        public void Put(int key, int value)
        {
            // if the key exists update the value.
            if (this.storage.ContainsKey(key))
            {
                // update the value.
                this.storage[key].value = value;

                // if this value isn't head.next, move it to be head.next.
                if (this.storage[key].key != head.next.key)
                {
                    // bypass previous to next, and next to previous skipping current.
                    this.storage[key].prev.next = this.storage[key].next;
                    this.storage[key].next.prev = this.storage[key].prev;

                    this.storage[key].prev = this.head;
                    this.storage[key].next = this.head.next;

                    // move current to between head and head.next.
                    this.head.next.prev = this.storage[key];
                    this.head.next      = this.storage[key];
                }
            }
            else
            {
                // we must remove the oldest element, because we are inserting new element.
                if (this.storage.Count + 1 > capacity)
                {
                    evictEldest();
                }

                // create new node and attach it to head and head.next
                var node = new DoubleListNode(key, value);
                node.prev = head;
                node.next = head.next;

                // target head and head.next to new node.
                head.next.prev = node;
                head.next      = node;

                this.storage.Add(key, node);
            }
        }
        public static void Execute()
        {
            var head       = new DoubleListNode(1);
            var secondNode = new DoubleListNode(2);
            var thirdNode  = new DoubleListNode(4);
            var fourthNode = new DoubleListNode(10);

            head.Next = secondNode;

            secondNode.Next = thirdNode;
            secondNode.Prev = head;

            thirdNode.Next = fourthNode;
            thirdNode.Prev = secondNode;

            fourthNode.Prev = thirdNode;

            var res1 = Insert(head, 11);
        }
Example #22
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 == 0)
            {
                AddAtHead(val);
                return;
            }
            DoubleListNode node = GetIndex(index - 1);

            if (node.val != -1)
            {
                DoubleListNode next = node.next;
                DoubleListNode temp = new DoubleListNode(val);
                node.next = temp;
                temp.next = next;
                temp.prev = node;
                next.prev = temp;
            }
        }
Example #23
0
    /** Adds an item at the front of Deque. Return true if the operation is successful. */
    public bool InsertFront(int value)
    {
        if (IsFull())
        {
            return(false);
        }

        DoubleListNode node = new DoubleListNode(value);

        node.next     = head;
        node.pre      = head.pre;
        head.pre.next = node;
        head.pre      = node;
        // node.next = head;
        // head.next.pre = node;
        // node.pre = head.next;
        // head.next.next = node;
        currentLength++;

        return(true);
    }
        private static DoubleListNode Reverse(DoubleListNode head)
        {
            var            currentNode = head;
            DoubleListNode temp        = null;

            while (currentNode != null)
            {
                temp             = currentNode.Prev;
                currentNode.Prev = currentNode.Next;
                currentNode.Next = temp;

                currentNode = currentNode.Prev;
            }

            if (temp != null)
            {
                head = temp.Prev;
            }

            return(head);
        }
Example #25
0
 private void RemoveFromCache(DoubleListNode node)
 {
     RemoveNode(node);
     dict.Remove(node.key);
     count--;
 }
Example #26
0
 private void UpdateUsedNode(DoubleListNode node)
 {
     RemoveNode(node);
     AddToEnd(node);
 }
Example #27
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)
        {
            DoubleListNode cur = GetIndex(index);

            return(cur.val);
        }
Example #28
0
 public DoubleListNode(int x)
 {
     data     = x;
     previous = null;
     next     = null;
 }
Example #29
0
 public void DeleteNodeFromBeginning()
 {
     head = head.next;
 }
Example #30
0
            DoubleListNode head = null;                                   //beginning of the lish

            public DoubleList()
            {
                head  = null;
                count = 0;
            }