-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack.cs
More file actions
111 lines (98 loc) · 3.18 KB
/
Copy pathStack.cs
File metadata and controls
111 lines (98 loc) · 3.18 KB
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
using System;
namespace Stack.Array
{
/// <summary>
/// A Last In First Out (LIFO) collection implemented as an array.
/// </summary>
/// <typeparam name="T">The type of item contained in the stack</typeparam>
public class Stack<T> : System.Collections.Generic.IEnumerable<T>
{
// The array of items contained in the stack. Initialized to 0 length,
// will grow as needed during Push
T[] _items = new T[0];
// The current number of items in the stack.
int _size;
/// <summary>
/// Adds the specified item to the stack
/// </summary>
/// <param name="item">The item</param>
public void Push(T item)
{
// _size = 0 ... first push
// _size == length ... growth boundary
if (_size == _items.Length)
{
// initial size of 4, otherwise double the current length
int newLength = _size == 0 ? 4 : _size * 2;
// allocate, copy and assign the new array
T[] newArray = new T[newLength];
_items.CopyTo(newArray, 0);
_items = newArray;
}
// add the item to the stack array and increase the size
_items[_size] = item;
_size++;
}
/// <summary>
/// Removes and returns the top item from the stack
/// </summary>
/// <returns>The top-most item in the stack</returns>
public T Pop()
{
if (_size == 0)
{
throw new InvalidOperationException("The stack is empty");
}
_size--;
return _items[_size];
}
/// <summary>
/// Returns the top item from the stack without removing it from the stack
/// </summary>
/// <returns>The top-most item in the stack</returns>
public T Peek()
{
if (_size == 0)
{
throw new InvalidOperationException("The stack is empty");
}
return _items[_size - 1];
}
/// <summary>
/// The current number of items in the stack
/// </summary>
public int Count
{
get
{
return _size;
}
}
/// <summary>
/// Removes all items from the stack
/// </summary>
public void Clear()
{
_size = 0;
}
/// <summary>
/// Enumerates each item in the stack in LIFO order. The stack remains unaltered.
/// </summary>
/// <returns>The LIFO enumerator</returns>
public System.Collections.Generic.IEnumerator<T> GetEnumerator()
{
for (int i = _size-1; i >= 0; i--)
{
yield return _items[i];
}
}
/// <summary>
/// Enumerates each item in the stack in LIFO order. The stack remains unaltered.
/// </summary>
/// <returns>The LIFO enumerator</returns>
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
}