- Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathStack.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
usingSystem;
usingSystem.Collections.Generic;
namespaceDataStructures.Lists
{
/// <summary>
/// The Stack (LIFO) Data Structure.
/// </summary>
/// <typeparam name="T">Type</typeparam>
publicclassStack<T>:IEnumerable<T>whereT:IComparable<T>
{
/// <summary>
/// Instance variables.
/// _collection: Array-Based List.
/// Count: Public Getter for returning the number of elements.
/// </summary>
privateArrayList<T>_collection{get;set;}
publicintCount{get{return_collection.Count;}}
/// <summary>
/// CONSTRUCTORS
/// </summary>
publicStack()
{
// The internal collection is implemented as an array-based list.
// See the ArrayList.cs for the list implementation.
_collection=newArrayList<T>();
}
publicStack(intinitialCapacity)
{
if(initialCapacity<0)
{
thrownewArgumentOutOfRangeException();
}
// The internal collection is implemented as an array-based list.
// See the ArrayList.cs for the list implementation.
_collection=newArrayList<T>(initialCapacity);
}
/// <summary>
/// Checks whether the stack is empty.
/// </summary>
/// <returns>True if stack is empty, false otherwise.</returns>
publicboolIsEmpty
{
get
{
return_collection.IsEmpty;
}
}
/// <summary>
/// Returns the top element in the stack.
/// </summary>
publicTTop
{
get
{
try
{
return_collection[_collection.Count-1];
}
catch(Exception)
{
thrownewException("Stack is empty.");
}
}
}
/// <summary>
/// Inserts an element at the top of the stack.
/// </summary>
/// <param name="dataItem">Element to be inserted.</param>
publicvoidPush(TdataItem)
{
_collection.Add(dataItem);
}
/// <summary>
/// Removes the top element from stack.
/// </summary>
publicTPop()
{
if(Count>0)
{
vartop=Top;
_collection.RemoveAt(_collection.Count-1);
returntop;
}
thrownewException("Stack is empty.");
}
/// <summary>
/// Returns an array version of this stack.
/// </summary>
/// <returns>System.Array.</returns>
publicT[]ToArray()
{
return_collection.ToArray();
}
/// <summary>
/// Returns a human-readable, multi-line, print-out (string) of this stack.
/// </summary>
/// <returns>String.</returns>
publicstringToHumanReadable()
{
return_collection.ToHumanReadable();
}
/********************************************************************************/
publicIEnumerator<T>GetEnumerator()
{
for(inti=_collection.Count-1;i>=0;--i)
yieldreturn_collection[i];
}
System.Collections.IEnumeratorSystem.Collections.IEnumerable.GetEnumerator()
{
returnthis.GetEnumerator();
}
}
}