- Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathArrayBasedStack.cs
133 lines (116 loc) · 3.94 KB
/
ArrayBasedStack.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
usingSystem;
usingSystem.Collections.Generic;
namespaceDataStructures.Stack
{
/// <summary>
/// Implementation of an array based stack. FIFO style.
/// </summary>
/// <typeparam name="T">Generic Type.</typeparam>
publicclassArrayBasedStack<T>
{
privateconstintDefaultCapacity=10;
privateconststringStackEmptyErrorMessage="Stack is empty";
/// <summary>
/// <see cref="Array"/> based stack.
/// </summary>
privateT[]stack;
/// <summary>
/// How many items are in the stack right now.
/// </summary>
privateinttop;
/// <summary>
/// Initializes a new instance of the <see cref="ArrayBasedStack{T}"/> class.
/// </summary>
publicArrayBasedStack()
{
stack=newT[DefaultCapacity];
top=-1;
}
/// <summary>
/// Initializes a new instance of the <see cref="ArrayBasedStack{T}"/> class.
/// </summary>
/// <param name="item">Item to push onto the <see cref="ArrayBasedStack{T}"/>.</param>
publicArrayBasedStack(Titem)
:this()=>Push(item);
/// <summary>
/// Initializes a new instance of the <see cref="ArrayBasedStack{T}"/> class.
/// </summary>
/// <param name="items">Items to push onto the <see cref="ArrayBasedStack{T}"/>.</param>
publicArrayBasedStack(IEnumerable<T>items)
:this()
{
foreach(variteminitems)
{
Push(item);
}
}
/// <summary>
/// Gets the number of elements on the <see cref="ArrayBasedStack{T}"/>.
/// </summary>
publicintTop=>top;
/// <summary>
/// Gets or sets the Capacity of the <see cref="ArrayBasedStack{T}"/>.
/// </summary>
publicintCapacity
{
get
{
returnstack.Length;
}
set
{
Array.Resize(refstack,value);
}
}
/// <summary>
/// Removes all items from the <see cref="ArrayBasedStack{T}"/>.
/// </summary>
publicvoidClear()
{
top=-1;
Capacity=DefaultCapacity;
}
/// <summary>
/// Determines whether an element is in the <see cref="ArrayBasedStack{T}"/>.
/// </summary>
/// <param name="item">The item to locate in the <see cref="ArrayBasedStack{T}"/>.</param>
/// <returns>True, if the item is in the stack.</returns>
publicboolContains(Titem)=>Array.IndexOf(stack,item,0,top+1)>-1;
/// <summary>
/// Returns the item at the top of the <see cref="ArrayBasedStack{T}"/> without removing it.
/// </summary>
/// <returns>The item at the top of the <see cref="ArrayBasedStack{T}"/>.</returns>
publicTPeek()
{
if(top==-1)
{
thrownewInvalidOperationException(StackEmptyErrorMessage);
}
returnstack[top];
}
/// <summary>
/// Removes and returns the item at the top of the <see cref="ArrayBasedStack{T}"/>.
/// </summary>
/// <returns>The item removed from the top of the <see cref="ArrayBasedStack{T}"/>.</returns>
publicTPop()
{
if(top==-1)
{
thrownewInvalidOperationException(StackEmptyErrorMessage);
}
returnstack[top--];
}
/// <summary>
/// Inserts an item at the top of the <see cref="ArrayBasedStack{T}"/>.
/// </summary>
/// <param name="item">The item to push onto the <see cref="ArrayBasedStack{T}"/>.</param>
publicvoidPush(Titem)
{
if(top==Capacity-1)
{
Capacity*=2;
}
stack[++top]=item;
}
}
}