From b5c5aa8a2f3a435e5b44aac326807e0ce2474285 Mon Sep 17 00:00:00 2001 From: Ts-Pytham <43942761+Ts-Pytham@users.noreply.github.com> Date: Tue, 27 Dec 2022 15:33:56 -0500 Subject: [PATCH] TryPop and TryPeek methods created --- GenericCollectionsExtension/Stack/IStack.cs | 23 ++++++++++++++ .../Stack/PriorityStack/IPriorityStack.cs | 1 + .../Stack/PriorityStack/PriorityStack.cs | 30 ++++++++++++++++++- 3 files changed, 53 insertions(+), 1 deletion(-) diff --git a/GenericCollectionsExtension/Stack/IStack.cs b/GenericCollectionsExtension/Stack/IStack.cs index 7d88e69..8a0b3f8 100644 --- a/GenericCollectionsExtension/Stack/IStack.cs +++ b/GenericCollectionsExtension/Stack/IStack.cs @@ -16,6 +16,11 @@ public interface IStack : ICollection /// int Capacity { get; } + /// + /// Gets a value indicating whether the priority stack is empty. + /// + bool IsEmpty { get; } + /// /// Returns the element at the top of the stack without removing it. /// @@ -23,9 +28,27 @@ public interface IStack : ICollection T Peek(); /// + /// Returns a value that indicates whether there is an object at the top of the + /// , and if one is present, copies it to the result parameter. + /// The object is not removed from the . + /// + /// If present, the object at the top of the ; otherwise, the default value of . + /// if there is an object at the top of the ; if the is empty. + bool TryPeek(out T result); + /// Removes and returns the element at the top of the stack. /// /// The element at the top of the stack. T Pop(); + + /// + /// Returns a value that indicates whether there is an object at the top of the + /// , and if one is present, copies it to the result parameter, + /// and removes it from the . + /// + /// If present, the object at the top of the ; otherwise, the default value of . + /// if there is an object at the top of the ; if the is empty. + bool TryPop(out T result); + } } diff --git a/GenericCollectionsExtension/Stack/PriorityStack/IPriorityStack.cs b/GenericCollectionsExtension/Stack/PriorityStack/IPriorityStack.cs index b8f10c7..dfabdd2 100644 --- a/GenericCollectionsExtension/Stack/PriorityStack/IPriorityStack.cs +++ b/GenericCollectionsExtension/Stack/PriorityStack/IPriorityStack.cs @@ -18,5 +18,6 @@ public interface IPriorityStack : IStack /// The element to add to the stack. /// The priority of the element. void Push(T item, int priority); + } } diff --git a/GenericCollectionsExtension/Stack/PriorityStack/PriorityStack.cs b/GenericCollectionsExtension/Stack/PriorityStack/PriorityStack.cs index c703a95..546f761 100644 --- a/GenericCollectionsExtension/Stack/PriorityStack/PriorityStack.cs +++ b/GenericCollectionsExtension/Stack/PriorityStack/PriorityStack.cs @@ -24,6 +24,9 @@ public class PriorityStack : IPriorityStack, ICollection, IEnumerable false; + /// class. /// @@ -91,10 +94,23 @@ public T Peek() return _stack[Count - 1].Value; } + /// + public bool TryPeek(out T result) + { + if (!IsEmpty) + { + result = _stack[Count - 1].Value; + return true; + } + + result = default; + return false; + } + /// public T Pop() { - if (Count == 0) + if (IsEmpty) throw new IndexOutOfRangeException(); var value = _stack[Count - 1].Value; @@ -102,6 +118,18 @@ public T Pop() return value; } + /// + public bool TryPop(out T result) + { + if (!IsEmpty) + { + result = Pop(); + return true; + } + result = default; + return false; + } + /// public void Push(T item, int priority) {