| | 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.DesignCircularQueue; |
| | 13 | |
|
| | 14 | | /// <inheritdoc /> |
| 1 | 15 | | public class DesignCircularQueueLinkedList(int k) : IDesignCircularQueue |
| | 16 | | { |
| | 17 | | private int _count; |
| | 18 | | private Node? _front; |
| | 19 | | private Node? _rear; |
| | 20 | |
|
| | 21 | | /// <summary> |
| | 22 | | /// Time complexity - O(1) |
| | 23 | | /// Space complexity - O(1) |
| | 24 | | /// </summary> |
| | 25 | | /// <param name="value"></param> |
| | 26 | | /// <returns></returns> |
| | 27 | | public bool EnQueue(int value) |
| 5 | 28 | | { |
| 5 | 29 | | if (IsFull()) |
| 1 | 30 | | { |
| 1 | 31 | | return false; |
| | 32 | | } |
| | 33 | |
|
| 4 | 34 | | if (_rear == null) |
| 1 | 35 | | { |
| 1 | 36 | | _rear = new Node(value); |
| 1 | 37 | | _front = _rear; |
| 1 | 38 | | } |
| | 39 | | else |
| 3 | 40 | | { |
| 3 | 41 | | var node = new Node(value); |
| | 42 | |
|
| 3 | 43 | | _rear.Next = node; |
| | 44 | |
|
| 3 | 45 | | _rear = node; |
| 3 | 46 | | } |
| | 47 | |
|
| 4 | 48 | | _count++; |
| | 49 | |
|
| 4 | 50 | | return true; |
| 5 | 51 | | } |
| | 52 | |
|
| | 53 | | /// <summary> |
| | 54 | | /// Time complexity - O(1) |
| | 55 | | /// Space complexity - O(1) |
| | 56 | | /// </summary> |
| | 57 | | /// <returns></returns> |
| | 58 | | public bool DeQueue() |
| 1 | 59 | | { |
| 1 | 60 | | if (IsEmpty()) |
| 0 | 61 | | { |
| 0 | 62 | | return false; |
| | 63 | | } |
| | 64 | |
|
| 1 | 65 | | if (_front == null) |
| 0 | 66 | | { |
| 0 | 67 | | return false; |
| | 68 | | } |
| | 69 | |
|
| 1 | 70 | | _front = _front.Next; |
| | 71 | |
|
| 1 | 72 | | _count--; |
| | 73 | |
|
| 1 | 74 | | if (IsEmpty()) |
| 0 | 75 | | { |
| 0 | 76 | | _rear = null; |
| 0 | 77 | | } |
| | 78 | |
|
| 1 | 79 | | return true; |
| 1 | 80 | | } |
| | 81 | |
|
| | 82 | | /// <summary> |
| | 83 | | /// Time complexity - O(1) |
| | 84 | | /// Space complexity - O(1) |
| | 85 | | /// </summary> |
| | 86 | | /// <returns></returns> |
| | 87 | | public int Front() |
| 0 | 88 | | { |
| 0 | 89 | | if (_front == null) |
| 0 | 90 | | { |
| 0 | 91 | | return -1; |
| | 92 | | } |
| | 93 | |
|
| 0 | 94 | | return _front.Value; |
| 0 | 95 | | } |
| | 96 | |
|
| | 97 | | /// <summary> |
| | 98 | | /// Time complexity - O(1) |
| | 99 | | /// Space complexity - O(1) |
| | 100 | | /// </summary> |
| | 101 | | /// <returns></returns> |
| | 102 | | public int Rear() |
| 2 | 103 | | { |
| 2 | 104 | | if (_rear == null) |
| 0 | 105 | | { |
| 0 | 106 | | return -1; |
| | 107 | | } |
| | 108 | |
|
| 2 | 109 | | return _rear.Value; |
| 2 | 110 | | } |
| | 111 | |
|
| | 112 | | /// <summary> |
| | 113 | | /// Time complexity - O(1) |
| | 114 | | /// Space complexity - O(1) |
| | 115 | | /// </summary> |
| | 116 | | /// <returns></returns> |
| | 117 | | public bool IsEmpty() |
| 2 | 118 | | { |
| 2 | 119 | | return _count == 0; |
| 2 | 120 | | } |
| | 121 | |
|
| | 122 | | /// <summary> |
| | 123 | | /// Time complexity - O(1) |
| | 124 | | /// Space complexity - O(1) |
| | 125 | | /// </summary> |
| | 126 | | /// <returns></returns> |
| | 127 | | public bool IsFull() |
| 6 | 128 | | { |
| 6 | 129 | | return _count == k; |
| 6 | 130 | | } |
| | 131 | |
|
| 4 | 132 | | private class Node(int value) |
| | 133 | | { |
| 4 | 134 | | public Node? Next { get; set; } |
| | 135 | |
|
| 6 | 136 | | public int Value { get; } = value; |
| | 137 | | } |
| | 138 | | } |