-
Notifications
You must be signed in to change notification settings - Fork 294
/
Copy pathStack.cs
82 lines (70 loc) · 1.62 KB
/
Stack.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
using System.Collections;
using System.Collections.Generic;
namespace Advanced.Algorithms.DataStructures.Foundation;
/// <summary>
/// A stack implementation.
/// </summary>
public class Stack<T> : IEnumerable<T>
{
private readonly IStack<T> stack;
/// <param name="type">The stack type to use.</param>
public Stack(StackType type = StackType.Array)
{
if (type == StackType.Array)
stack = new ArrayStack<T>();
else
stack = new LinkedListStack<T>();
}
/// <summary>
/// The total number of items in this stack.
/// </summary>
public int Count => stack.Count;
public IEnumerator<T> GetEnumerator()
{
return GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return stack.GetEnumerator();
}
/// <summary>
/// Time complexity:O(1).
/// </summary>
/// <returns>The item popped.</returns>
public T Pop()
{
return stack.Pop();
}
/// <summary>
/// Time complexity:O(1).
/// </summary>
/// <param name="item">The item to push.</param>
public void Push(T item)
{
stack.Push(item);
}
/// <summary>
/// Peek from stack.
/// Time complexity:O(1).
/// </summary>
/// <returns>The item peeked.</returns>
public T Peek()
{
return stack.Peek();
}
}
internal interface IStack<T> : IEnumerable<T>
{
int Count { get; }
T Pop();
void Push(T item);
T Peek();
}
/// <summary>
/// The stack implementation types.
/// </summary>
public enum StackType
{
Array = 0,
LinkedList = 1
}