| | 1 | | // -------------------------------------------------------------------------------- |
| | 2 | | // Copyright (C) 2025 Eugene Eremeev (also known as Yevhenii Yeriemeieiv). |
| | 3 | | // All Rights Reserved. |
| | 4 | | // -------------------------------------------------------------------------------- |
| | 5 | | // This software is the confidential and proprietary information of Eugene Eremeev |
| | 6 | | // (also known as Yevhenii Yeriemeieiv) ("Confidential Information"). You shall not |
| | 7 | | // disclose such Confidential Information and shall use it only in accordance with |
| | 8 | | // the terms of the license agreement you entered into with Eugene Eremeev (also |
| | 9 | | // known as Yevhenii Yeriemeieiv). |
| | 10 | | // -------------------------------------------------------------------------------- |
| | 11 | |
|
| | 12 | | namespace LeetCode.Algorithms.DesignCircularDeque; |
| | 13 | |
|
| | 14 | | /// <inheritdoc /> |
| | 15 | | public class DesignCircularDequeLinkedList : IDesignCircularDeque |
| | 16 | | { |
| | 17 | | private readonly int _k; |
| | 18 | | private int _count; |
| | 19 | | private DequeNode? _frontNode; |
| | 20 | | private DequeNode? _rearNode; |
| | 21 | |
|
| 1 | 22 | | public DesignCircularDequeLinkedList(int k) |
| 1 | 23 | | { |
| 1 | 24 | | _k = k; |
| 1 | 25 | | } |
| | 26 | |
|
| | 27 | | /// <summary> |
| | 28 | | /// Time complexity - O(1) |
| | 29 | | /// Space complexity - O(1) |
| | 30 | | /// </summary> |
| | 31 | | /// <param name="value"></param> |
| | 32 | | /// <returns></returns> |
| | 33 | | public bool InsertFront(int value) |
| 3 | 34 | | { |
| 3 | 35 | | if (IsFull()) |
| 1 | 36 | | { |
| 1 | 37 | | return false; |
| | 38 | | } |
| | 39 | |
|
| 2 | 40 | | if (_frontNode == null) |
| 0 | 41 | | { |
| 0 | 42 | | _frontNode = new DequeNode(value); |
| | 43 | |
|
| 0 | 44 | | _rearNode = _frontNode; |
| 0 | 45 | | } |
| | 46 | | else |
| 2 | 47 | | { |
| 2 | 48 | | _frontNode.Previous = new DequeNode(value) { Next = _frontNode }; |
| | 49 | |
|
| 2 | 50 | | _frontNode = _frontNode.Previous; |
| 2 | 51 | | } |
| | 52 | |
|
| 2 | 53 | | _count++; |
| | 54 | |
|
| 2 | 55 | | return true; |
| 3 | 56 | | } |
| | 57 | |
|
| | 58 | | /// <summary> |
| | 59 | | /// Time complexity - O(1) |
| | 60 | | /// Space complexity - O(1) |
| | 61 | | /// </summary> |
| | 62 | | /// <param name="value"></param> |
| | 63 | | /// <returns></returns> |
| | 64 | | public bool InsertLast(int value) |
| 2 | 65 | | { |
| 2 | 66 | | if (IsFull()) |
| 0 | 67 | | { |
| 0 | 68 | | return false; |
| | 69 | | } |
| | 70 | |
|
| 2 | 71 | | if (_rearNode == null) |
| 1 | 72 | | { |
| 1 | 73 | | _rearNode = new DequeNode(value); |
| | 74 | |
|
| 1 | 75 | | _frontNode = _rearNode; |
| 1 | 76 | | } |
| | 77 | | else |
| 1 | 78 | | { |
| 1 | 79 | | _rearNode.Next = new DequeNode(value) { Previous = _rearNode }; |
| | 80 | |
|
| 1 | 81 | | _rearNode = _rearNode.Next; |
| 1 | 82 | | } |
| | 83 | |
|
| 2 | 84 | | _count++; |
| | 85 | |
|
| 2 | 86 | | return true; |
| 2 | 87 | | } |
| | 88 | |
|
| | 89 | | /// <summary> |
| | 90 | | /// Time complexity - O(1) |
| | 91 | | /// Space complexity - O(1) |
| | 92 | | /// </summary> |
| | 93 | | /// <returns></returns> |
| | 94 | | public bool DeleteFront() |
| 0 | 95 | | { |
| 0 | 96 | | if (IsEmpty()) |
| 0 | 97 | | { |
| 0 | 98 | | return false; |
| | 99 | | } |
| | 100 | |
|
| 0 | 101 | | if (_count == 1) |
| 0 | 102 | | { |
| 0 | 103 | | _frontNode = null; |
| 0 | 104 | | _rearNode = null; |
| 0 | 105 | | } |
| | 106 | | else |
| 0 | 107 | | { |
| 0 | 108 | | _frontNode = _frontNode?.Next; |
| | 109 | |
|
| 0 | 110 | | if (_frontNode != null) |
| 0 | 111 | | { |
| 0 | 112 | | _frontNode.Previous = null; |
| 0 | 113 | | } |
| 0 | 114 | | } |
| | 115 | |
|
| 0 | 116 | | _count--; |
| | 117 | |
|
| 0 | 118 | | return true; |
| 0 | 119 | | } |
| | 120 | |
|
| | 121 | | /// <summary> |
| | 122 | | /// Time complexity - O(1) |
| | 123 | | /// Space complexity - O(1) |
| | 124 | | /// </summary> |
| | 125 | | /// <returns></returns> |
| | 126 | | public bool DeleteLast() |
| 1 | 127 | | { |
| 1 | 128 | | if (IsEmpty()) |
| 0 | 129 | | { |
| 0 | 130 | | return false; |
| | 131 | | } |
| | 132 | |
|
| 1 | 133 | | if (_count == 1) |
| 0 | 134 | | { |
| 0 | 135 | | _frontNode = null; |
| 0 | 136 | | _rearNode = null; |
| 0 | 137 | | } |
| | 138 | | else |
| 1 | 139 | | { |
| 1 | 140 | | _rearNode = _rearNode?.Previous; |
| | 141 | |
|
| 1 | 142 | | if (_rearNode != null) |
| 1 | 143 | | { |
| 1 | 144 | | _rearNode.Next = null; |
| 1 | 145 | | } |
| 1 | 146 | | } |
| | 147 | |
|
| 1 | 148 | | _count--; |
| | 149 | |
|
| 1 | 150 | | return true; |
| 1 | 151 | | } |
| | 152 | |
|
| | 153 | | /// <summary> |
| | 154 | | /// Time complexity - O(1) |
| | 155 | | /// Space complexity - O(1) |
| | 156 | | /// </summary> |
| | 157 | | /// <returns></returns> |
| | 158 | | public int GetFront() |
| 1 | 159 | | { |
| 1 | 160 | | if (_frontNode == null) |
| 0 | 161 | | { |
| 0 | 162 | | return -1; |
| | 163 | | } |
| | 164 | |
|
| 1 | 165 | | return _frontNode.Value; |
| 1 | 166 | | } |
| | 167 | |
|
| | 168 | | /// <summary> |
| | 169 | | /// Time complexity - O(1) |
| | 170 | | /// Space complexity - O(1) |
| | 171 | | /// </summary> |
| | 172 | | /// <returns></returns> |
| | 173 | | public int GetRear() |
| 1 | 174 | | { |
| 1 | 175 | | if (_rearNode == null) |
| 0 | 176 | | { |
| 0 | 177 | | return -1; |
| | 178 | | } |
| | 179 | |
|
| 1 | 180 | | return _rearNode.Value; |
| 1 | 181 | | } |
| | 182 | |
|
| | 183 | | /// <summary> |
| | 184 | | /// Time complexity - O(1) |
| | 185 | | /// Space complexity - O(1) |
| | 186 | | /// </summary> |
| | 187 | | /// <returns></returns> |
| | 188 | | public bool IsEmpty() |
| 1 | 189 | | { |
| 1 | 190 | | return _count == 0; |
| 1 | 191 | | } |
| | 192 | |
|
| | 193 | | /// <summary> |
| | 194 | | /// Time complexity - O(1) |
| | 195 | | /// Space complexity - O(1) |
| | 196 | | /// </summary> |
| | 197 | | /// <returns></returns> |
| | 198 | | public bool IsFull() |
| 6 | 199 | | { |
| 6 | 200 | | return _count == _k; |
| 6 | 201 | | } |
| | 202 | |
|
| 4 | 203 | | private class DequeNode(int value) |
| | 204 | | { |
| 6 | 205 | | public DequeNode? Previous { get; set; } |
| | 206 | |
|
| 5 | 207 | | public DequeNode? Next { get; set; } |
| | 208 | |
|
| 6 | 209 | | public int Value { get; } = value; |
| | 210 | | } |
| | 211 | | } |