| | 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.DesignFrontMiddleBackQueue; |
| | 13 | |
|
| | 14 | | /// <inheritdoc /> |
| | 15 | | public class DesignFrontMiddleBackQueueLinkedList : IDesignFrontMiddleBackQueue |
| | 16 | | { |
| | 17 | | private int _count; |
| | 18 | | private Node? _head; |
| | 19 | | private Node? _tail; |
| | 20 | |
|
| | 21 | | /// <summary> |
| | 22 | | /// Time complexity - O(1) |
| | 23 | | /// Space complexity - O(1) |
| | 24 | | /// </summary> |
| | 25 | | /// <param name="value"></param> |
| | 26 | | public void PushFront(int value) |
| 1 | 27 | | { |
| 1 | 28 | | if (_head == null) |
| 1 | 29 | | { |
| 1 | 30 | | var node = new Node(value); |
| | 31 | |
|
| 1 | 32 | | _head = node; |
| 1 | 33 | | _tail = node; |
| 1 | 34 | | } |
| | 35 | | else |
| 0 | 36 | | { |
| 0 | 37 | | var node = new Node(value) { Next = _head }; |
| | 38 | |
|
| 0 | 39 | | _head.Previous = node; |
| 0 | 40 | | _head = node; |
| 0 | 41 | | } |
| | 42 | |
|
| 1 | 43 | | _count++; |
| 1 | 44 | | } |
| | 45 | |
|
| | 46 | | /// <summary> |
| | 47 | | /// Time complexity - O(n) |
| | 48 | | /// Space complexity - O(1) |
| | 49 | | /// </summary> |
| | 50 | | /// <param name="value"></param> |
| | 51 | | public void PushMiddle(int value) |
| 2 | 52 | | { |
| 2 | 53 | | if (_count == 0) |
| 0 | 54 | | { |
| 0 | 55 | | var node = new Node(value); |
| | 56 | |
|
| 0 | 57 | | _head = node; |
| 0 | 58 | | _tail = node; |
| 0 | 59 | | } |
| | 60 | | else |
| 2 | 61 | | { |
| 2 | 62 | | var node = new Node(value); |
| | 63 | |
|
| 2 | 64 | | var middleIndex = _count / 2; |
| 2 | 65 | | var current = _head; |
| | 66 | |
|
| 8 | 67 | | for (var i = 0; i < middleIndex; i++) |
| 2 | 68 | | { |
| 2 | 69 | | current = current?.Next; |
| 2 | 70 | | } |
| | 71 | |
|
| 2 | 72 | | if (current != null) |
| 2 | 73 | | { |
| 2 | 74 | | node.Next = current; |
| 2 | 75 | | node.Previous = current.Previous; |
| | 76 | |
|
| 2 | 77 | | if (current.Previous != null) |
| 2 | 78 | | { |
| 2 | 79 | | current.Previous.Next = node; |
| 2 | 80 | | } |
| | 81 | | else |
| 0 | 82 | | { |
| 0 | 83 | | _head = node; |
| 0 | 84 | | } |
| | 85 | |
|
| 2 | 86 | | current.Previous = node; |
| 2 | 87 | | } |
| 2 | 88 | | } |
| | 89 | |
|
| 2 | 90 | | _count++; |
| 2 | 91 | | } |
| | 92 | |
|
| | 93 | | /// <summary> |
| | 94 | | /// Time complexity - O(1) |
| | 95 | | /// Space complexity - O(1) |
| | 96 | | /// </summary> |
| | 97 | | /// <param name="value"></param> |
| | 98 | | public void PushBack(int value) |
| 1 | 99 | | { |
| 1 | 100 | | if (_tail == null) |
| 0 | 101 | | { |
| 0 | 102 | | var node = new Node(value); |
| | 103 | |
|
| 0 | 104 | | _head = node; |
| 0 | 105 | | _tail = node; |
| 0 | 106 | | } |
| | 107 | | else |
| 1 | 108 | | { |
| 1 | 109 | | var node = new Node(value) { Previous = _tail }; |
| | 110 | |
|
| 1 | 111 | | _tail.Next = node; |
| 1 | 112 | | _tail = node; |
| 1 | 113 | | } |
| | 114 | |
|
| 1 | 115 | | _count++; |
| 1 | 116 | | } |
| | 117 | |
|
| | 118 | | /// <summary> |
| | 119 | | /// Time complexity - O(1) |
| | 120 | | /// Space complexity - O(1) |
| | 121 | | /// </summary> |
| | 122 | | /// <returns></returns> |
| | 123 | | public int PopFront() |
| 2 | 124 | | { |
| 2 | 125 | | if (_head == null) |
| 1 | 126 | | { |
| 1 | 127 | | return -1; |
| | 128 | | } |
| | 129 | |
|
| 1 | 130 | | var head = _head; |
| | 131 | |
|
| 1 | 132 | | _head = _head.Next; |
| | 133 | |
|
| 1 | 134 | | if (_head != null) |
| 1 | 135 | | { |
| 1 | 136 | | _head.Previous = null; |
| 1 | 137 | | } |
| | 138 | | else |
| 0 | 139 | | { |
| 0 | 140 | | _tail = null; |
| 0 | 141 | | } |
| | 142 | |
|
| 1 | 143 | | _count--; |
| | 144 | |
|
| 1 | 145 | | return head.Value; |
| 2 | 146 | | } |
| | 147 | |
|
| | 148 | | /// <summary> |
| | 149 | | /// Time complexity - O(n) |
| | 150 | | /// Space complexity - O(1) |
| | 151 | | /// </summary> |
| | 152 | | /// <returns></returns> |
| | 153 | | public int PopMiddle() |
| 2 | 154 | | { |
| 2 | 155 | | if (_head == null) |
| 0 | 156 | | { |
| 0 | 157 | | return -1; |
| | 158 | | } |
| | 159 | |
|
| 2 | 160 | | var middleIndex = (_count - 1) / 2; |
| 2 | 161 | | var current = _head; |
| | 162 | |
|
| 6 | 163 | | for (var i = 0; i < middleIndex; i++) |
| 1 | 164 | | { |
| 1 | 165 | | current = current?.Next; |
| 1 | 166 | | } |
| | 167 | |
|
| 2 | 168 | | if (current == null) |
| 0 | 169 | | { |
| 0 | 170 | | return -1; |
| | 171 | | } |
| | 172 | |
|
| 2 | 173 | | var middle = current; |
| | 174 | |
|
| 2 | 175 | | if (current.Previous != null) |
| 1 | 176 | | { |
| 1 | 177 | | current.Previous.Next = current.Next; |
| 1 | 178 | | } |
| | 179 | | else |
| 1 | 180 | | { |
| 1 | 181 | | _head = current.Next; |
| 1 | 182 | | } |
| | 183 | |
|
| 2 | 184 | | if (current.Next != null) |
| 2 | 185 | | { |
| 2 | 186 | | current.Next.Previous = current.Previous; |
| 2 | 187 | | } |
| | 188 | | else |
| 0 | 189 | | { |
| 0 | 190 | | _tail = current.Previous; |
| 0 | 191 | | } |
| | 192 | |
|
| 2 | 193 | | _count--; |
| | 194 | |
|
| 2 | 195 | | return middle.Value; |
| 2 | 196 | | } |
| | 197 | |
|
| | 198 | | /// <summary> |
| | 199 | | /// Time complexity - O(1) |
| | 200 | | /// Space complexity - O(1) |
| | 201 | | /// </summary> |
| | 202 | | /// <returns></returns> |
| | 203 | | public int PopBack() |
| 1 | 204 | | { |
| 1 | 205 | | if (_tail == null) |
| 0 | 206 | | { |
| 0 | 207 | | return -1; |
| | 208 | | } |
| | 209 | |
|
| 1 | 210 | | var tail = _tail; |
| | 211 | |
|
| 1 | 212 | | _tail = _tail.Previous; |
| | 213 | |
|
| 1 | 214 | | if (_tail != null) |
| 0 | 215 | | { |
| 0 | 216 | | _tail.Next = null; |
| 0 | 217 | | } |
| | 218 | | else |
| 1 | 219 | | { |
| 1 | 220 | | _head = null; |
| 1 | 221 | | } |
| | 222 | |
|
| 1 | 223 | | _count--; |
| | 224 | |
|
| 1 | 225 | | return tail.Value; |
| 1 | 226 | | } |
| | 227 | |
|
| 4 | 228 | | private class Node(int value) |
| | 229 | | { |
| 16 | 230 | | public Node? Next { get; set; } |
| 20 | 231 | | public Node? Previous { get; set; } |
| 8 | 232 | | public int Value { get; } = value; |
| | 233 | | } |
| | 234 | | } |