-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack.cs
More file actions
91 lines (80 loc) · 2.52 KB
/
Copy pathStack.cs
File metadata and controls
91 lines (80 loc) · 2.52 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
using System;
namespace Stack.List
{
/// <summary>
/// A Last In First Out (LIFO) collection implemented as a linked list.
/// </summary>
/// <typeparam name="T">The type of item contained in the stack</typeparam>
public class Stack<T> : System.Collections.Generic.IEnumerable<T>
{
private System.Collections.Generic.LinkedList<T> _list =
new System.Collections.Generic.LinkedList<T>();
/// <summary>
/// Adds the specified item to the stack
/// </summary>
/// <param name="item">The item</param>
public void Push(T item)
{
_list.AddFirst(item);
}
/// <summary>
/// Removes and returns the top item from the stack
/// </summary>
/// <returns>The top-most item in the stack</returns>
public T Pop()
{
if (_list.Count == 0)
{
throw new InvalidOperationException("The stack is empty");
}
T value = _list.First.Value;
_list.RemoveFirst();
return value;
}
/// <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 (_list.Count == 0)
{
throw new InvalidOperationException("The stack is empty");
}
return _list.First.Value;
}
/// <summary>
/// The current number of items in the stack
/// </summary>
public int Count
{
get
{
return _list.Count;
}
}
/// <summary>
/// Removes all items from the stack
/// </summary>
public void Clear()
{
_list.Clear();
}
/// <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()
{
return _list.GetEnumerator();
}
/// <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 _list.GetEnumerator();
}
}
}