-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
ArrayBasedStack.cs
121 lines (104 loc) · 3.43 KB
/
ArrayBasedStack.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
using System;
namespace DataStructures.Stack;
/// <summary>
/// Implementation of an array-based stack. LIFO style.
/// </summary>
/// <typeparam name="T">Generic Type.</typeparam>
public class ArrayBasedStack<T>
{
private const int DefaultCapacity = 10;
private const string StackEmptyErrorMessage = "Stack is empty";
/// <summary>
/// <see cref="Array" /> based stack.
/// </summary>
private T[] stack;
/// <summary>
/// How many items are in the stack right now.
/// </summary>
private int top;
/// <summary>
/// Initializes a new instance of the <see cref="ArrayBasedStack{T}" /> class.
/// </summary>
public ArrayBasedStack()
{
stack = new T[DefaultCapacity];
top = -1;
}
/// <summary>
/// Initializes a new instance of the <see cref="ArrayBasedStack{T}" /> class.
/// </summary>
/// <param name="item">Item to push onto the <see cref="ArrayBasedStack{T}" />.</param>
public ArrayBasedStack(T item)
: this() => Push(item);
/// <summary>
/// Initializes a new instance of the <see cref="ArrayBasedStack{T}" /> class.
/// </summary>
/// <param name="items">Items to push onto the <see cref="ArrayBasedStack{T}" />.</param>
public ArrayBasedStack(T[] items)
{
stack = items;
top = items.Length - 1;
}
/// <summary>
/// Gets the number of elements on the <see cref="ArrayBasedStack{T}" />.
/// </summary>
public int Top => top;
/// <summary>
/// Gets or sets the Capacity of the <see cref="ArrayBasedStack{T}" />.
/// </summary>
public int Capacity
{
get => stack.Length;
set => Array.Resize(ref stack, value);
}
/// <summary>
/// Removes all items from the <see cref="ArrayBasedStack{T}" />.
/// </summary>
public void Clear()
{
top = -1;
Capacity = DefaultCapacity;
}
/// <summary>
/// Determines whether an element is in the <see cref="ArrayBasedStack{T}" />.
/// </summary>
/// <param name="item">The item to locate in the <see cref="ArrayBasedStack{T}" />.</param>
/// <returns>True, if the item is in the stack.</returns>
public bool Contains(T item) => Array.IndexOf(stack, item, 0, top + 1) > -1;
/// <summary>
/// Returns the item at the top of the <see cref="ArrayBasedStack{T}" /> without removing it.
/// </summary>
/// <returns>The item at the top of the <see cref="ArrayBasedStack{T}" />.</returns>
public T Peek()
{
if (top == -1)
{
throw new InvalidOperationException(StackEmptyErrorMessage);
}
return stack[top];
}
/// <summary>
/// Removes and returns the item at the top of the <see cref="ArrayBasedStack{T}" />.
/// </summary>
/// <returns>The item removed from the top of the <see cref="ArrayBasedStack{T}" />.</returns>
public T Pop()
{
if (top == -1)
{
throw new InvalidOperationException(StackEmptyErrorMessage);
}
return stack[top--];
}
/// <summary>
/// Inserts an item at the top of the <see cref="ArrayBasedStack{T}" />.
/// </summary>
/// <param name="item">The item to push onto the <see cref="ArrayBasedStack{T}" />.</param>
public void Push(T item)
{
if (top == Capacity - 1)
{
Capacity *= 2;
}
stack[++top] = item;
}
}