forked from aalhour/C-Sharp-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Stack.cs
138 lines (113 loc) · 3.51 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
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
using System;
using System.Collections.Generic;
namespace DataStructures.Lists
{
/// <summary>
/// The Stack (LIFO) Data Structure.
/// </summary>
/// <typeparam name="T">Type</typeparam>
public class Stack<T> : IEnumerable<T> where T : IComparable<T>
{
/// <summary>
/// Instance variables.
/// _collection: Array-Based List.
/// Count: Public Getter for returning the number of elements.
/// </summary>
private ArrayList<T> _collection { get; set; }
public int Count { get { return _collection.Count; } }
/// <summary>
/// CONSTRUCTORS
/// </summary>
public Stack()
{
// The internal collection is implemented as an array-based list.
// See the ArrayList.cs for the list implementation.
_collection = new ArrayList<T>();
}
public Stack(int initialCapacity)
{
if (initialCapacity < 0)
{
throw new ArgumentOutOfRangeException();
}
// The internal collection is implemented as an array-based list.
// See the ArrayList.cs for the list implementation.
_collection = new ArrayList<T>(initialCapacity);
}
/// <summary>
/// Checks whether the stack is empty.
/// </summary>
/// <returns>True if stack is empty, false otherwise.</returns>
public bool IsEmpty
{
get
{
return _collection.IsEmpty;
}
}
/// <summary>
/// Returns the top element in the stack.
/// </summary>
public T Top
{
get
{
try
{
return _collection[_collection.Count - 1];
}
catch (Exception)
{
throw new Exception("Stack is empty.");
}
}
}
/// <summary>
/// Inserts an element at the top of the stack.
/// </summary>
/// <param name="dataItem">Element to be inserted.</param>
public void Push(T dataItem)
{
_collection.Add(dataItem);
}
/// <summary>
/// Removes the top element from stack.
/// </summary>
public T Pop()
{
if (Count > 0)
{
var top = Top;
_collection.RemoveAt(_collection.Count - 1);
return top;
}
throw new Exception("Stack is empty.");
}
/// <summary>
/// Returns an array version of this stack.
/// </summary>
/// <returns>System.Array.</returns>
public T[] ToArray()
{
return _collection.ToArray();
}
/// <summary>
/// Returns a human-readable, multi-line, print-out (string) of this stack.
/// </summary>
/// <returns>String.</returns>
public string ToHumanReadable()
{
return _collection.ToHumanReadable();
}
/********************************************************************************/
public IEnumerator<T> GetEnumerator()
{
for (int i = _collection.Count - 1; i >= 0; --i)
yield return _collection[i];
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}
}
}