Skip to content

Commit

Permalink
Make ListBasedStack use an actual linked list. (TheAlgorithms#151)
Browse files Browse the repository at this point in the history
Co-authored-by: Andrii Siriak <[email protected]>
  • Loading branch information
afoix and siriak authored Sep 1, 2020
1 parent 4a0c073 commit b5817f4
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions DataStructures/ListBasedStack/ListBasedStack.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@
namespace DataStructures.ListBasedStack
{
/// <summary>
/// Implementation of a list based stack. FIFO style.
/// Implementation of a list based stack. FILO style.
/// </summary>
/// <typeparam name="T">Generic Type.</typeparam>
public class ListBasedStack<T>
{
/// <summary>
/// <see cref="List{T}"/> based stack.
/// </summary>
private readonly List<T> stack;
private readonly LinkedList<T> stack;

/// <summary>
/// Initializes a new instance of the <see cref="ListBasedStack{T}"/> class.
/// </summary>
public ListBasedStack() => stack = new List<T>();
public ListBasedStack() => stack = new LinkedList<T>();

/// <summary>
/// Initializes a new instance of the <see cref="ListBasedStack{T}"/> class.
Expand Down Expand Up @@ -59,23 +59,23 @@ public ListBasedStack(IEnumerable<T> items)
/// Returns the item at the top of the <see cref="ListBasedStack{T}"/> without removing it.
/// </summary>
/// <returns>The item at the top of the <see cref="ListBasedStack{T}"/>.</returns>
public T Peek() => stack[^1];
public T Peek() => stack.First.Value;

/// <summary>
/// Removes and returns the item at the top of the <see cref="ListBasedStack{T}"/>.
/// </summary>
/// <returns>The item removed from the top of the <see cref="ListBasedStack{T}"/>.</returns>
public T Pop()
{
var item = stack[^1];
stack.RemoveAt(stack.Count - 1);
var item = stack.First.Value;
stack.RemoveFirst();
return item;
}

/// <summary>
/// Inserts an item at the top of the <see cref="ListBasedStack{T}"/>.
/// </summary>
/// <param name="item">The item to push onto the <see cref="ListBasedStack{T}"/>.</param>
public void Push(T item) => stack.Add(item);
public void Push(T item) => stack.AddFirst(item);
}
}

0 comments on commit b5817f4

Please sign in to comment.