-
Notifications
You must be signed in to change notification settings - Fork 7
/
ArrayedStack.cs
87 lines (79 loc) · 2.5 KB
/
ArrayedStack.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
/*******************************************************
* ArrayedStack.cs
* Created by Stephen Hall on 9/25/17.
* Copyright (c) 2017 Stephen Hall. All rights reserved.
* A Arrayed Stack implementation in C#
********************************************************/
namespace DataStructures.Stacks.ArrayedStack
{
/// <summary>
/// Arrayed Stack Class
/// </summary>
/// <typeparam name="T"> Generic type</typeparam>
public class ArrayedStack<T>
{
/// <summary>
/// Private Members
/// </summary>
private T[] _array;
private int _count;
private int _size;
/// <summary>
/// Default Constructor
/// </summary>
public ArrayedStack()
{
_array = new T[_size = 10];
_count = 0;
}
/// <summary>
/// Arrayed Stack Constructor
/// </summary>
/// <param name="size">Size to initialize the stack to</param>
public ArrayedStack(int size)
{
_array = new T[_size = size];
_count = 0;
}
/// <summary>
/// Pushes given data onto the stack if space is available
/// </summary>
/// <param name="data">Data to be added to the stack</param>
/// <returns>Item added to the stack</returns>
public T Push(T data)
{
if (!IsFull())
{
_array[_count] = data;
_count++;
return Top();
}
return default(T);
}
/// <summary>
/// Pops item off the stack
/// </summary>
/// <returns>Item popped off of the stack</returns>
public T Pop()
{
T data = _array[_count - 1];
_count--;
return data;
}
/// <summary>
/// Gets the Item onto of the stack
/// </summary>
/// <returns>Item on top of the stack</returns>
public T Top() => _array[_count - 1];
/// <summary>
/// Returns a value indicating if the stack is empty
/// </summary>
/// <returns>true if empty, false if not</returns>
public bool IsEmpty() => _count == 0;
/// <summary>
/// Returns a value indicating if the stack is full
/// </summary>
/// <returns>True if full, false if not</returns>
public bool IsFull() => _count == _size;
}
}