|
2 | 2 |
|
3 | 3 | class CAStack<T> |
4 | 4 | { |
| 5 | + private T[] mArray; |
5 | 6 |
|
6 | | - private T[] mArray; |
7 | | - // Contains the number of the next empty cell. |
8 | | - private int top = 0; |
| 7 | + // Contains the number of the next empty cell. |
| 8 | + private int top = 0; |
9 | 9 |
|
10 | | - // Our default stack size is 10. |
11 | | - public CAStack(int sizeP = 10) |
12 | | - { |
13 | | - if (sizeP <= 0) |
14 | | - throw new ApplicationException("Stack size must be strictly greater than 0."); |
15 | | - else |
16 | | - mArray = new T[sizeP]; |
17 | | - } |
| 10 | + // Our default stack size is 10. |
| 11 | + public CAStack(int sizeP = 10) |
| 12 | + { |
| 13 | + if (sizeP <= 0) |
| 14 | + throw new ApplicationException( |
| 15 | + "Stack size must be strictly greater than 0." |
| 16 | + ); |
| 17 | + else |
| 18 | + mArray = new T[sizeP]; |
| 19 | + } |
18 | 20 |
|
19 | | - public void Clear() |
20 | | - { |
21 | | - top = 0; |
22 | | - } |
| 21 | + public void Clear() |
| 22 | + { |
| 23 | + top = 0; |
| 24 | + } |
23 | 25 |
|
24 | | - public bool IsEmpty() |
25 | | - { |
26 | | - return top == 0; |
27 | | - } |
| 26 | + public bool IsEmpty() |
| 27 | + { |
| 28 | + return top == 0; |
| 29 | + } |
28 | 30 |
|
29 | | - public void Push(T value) |
| 31 | + public void Push(T value) |
| 32 | + { |
| 33 | + if (top < mArray.Length) |
30 | 34 | { |
31 | | - if (top < mArray.Length) |
32 | | - { |
33 | | - mArray[top] = value; |
34 | | - top++; |
35 | | - } |
36 | | - else |
37 | | - { |
38 | | - throw new ApplicationException("Stack is full."); |
39 | | - } |
| 35 | + mArray[top] = value; |
| 36 | + top++; |
40 | 37 | } |
41 | | - |
42 | | - public T Pop() |
| 38 | + else |
43 | 39 | { |
44 | | - if (IsEmpty()) |
45 | | - throw new ApplicationException( |
46 | | - "An empty stack cannot be popped." |
47 | | - ); |
48 | | - return mArray[--top]; |
| 40 | + throw new ApplicationException("Stack is full."); |
49 | 41 | } |
| 42 | + } |
50 | 43 |
|
| 44 | + public T Pop() |
| 45 | + { |
| 46 | + if (IsEmpty()) |
| 47 | + throw new ApplicationException( |
| 48 | + "An empty stack cannot be popped." |
| 49 | + ); |
| 50 | + return mArray[--top]; |
| 51 | + } |
51 | 52 |
|
52 | | - public T Peek() |
53 | | - { |
54 | | - if (IsEmpty()) |
55 | | - throw new ApplicationException( |
56 | | - "An empty stack cannot be peeked." |
57 | | - ); |
58 | | - return mArray[top - 1]; |
59 | | - } |
60 | | - |
61 | | -public int Count |
62 | | - { |
63 | | - get |
64 | | - { |
65 | | - return top; |
66 | | - } |
67 | | - } |
| 53 | + public T Peek() |
| 54 | + { |
| 55 | + if (IsEmpty()) |
| 56 | + throw new ApplicationException( |
| 57 | + "An empty stack cannot be peeked." |
| 58 | + ); |
| 59 | + return mArray[top - 1]; |
| 60 | + } |
68 | 61 |
|
| 62 | + public int Count |
| 63 | + { |
| 64 | + get { return top; } |
| 65 | + } |
69 | 66 |
|
70 | | - public override string ToString() |
| 67 | + public override string ToString() |
| 68 | + { |
| 69 | + string returned = ""; |
| 70 | + if (!IsEmpty()) |
71 | 71 | { |
72 | | - string returned = ""; |
73 | | - if (!IsEmpty()) |
74 | | - { |
75 | | - int counter = top-1; |
76 | | - while (counter >= 0) |
77 | | - { |
78 | | - if (returned.Length > 0) |
79 | | - returned += ":"; |
80 | | - returned += mArray[counter]; |
81 | | - counter--; |
82 | | - } |
83 | | - } |
84 | | - return returned; |
85 | | - |
| 72 | + int counter = top - 1; |
| 73 | + while (counter >= 0) |
| 74 | + { |
| 75 | + if (returned.Length > 0) |
| 76 | + returned += ":"; |
| 77 | + returned += mArray[counter]; |
| 78 | + counter--; |
| 79 | + } |
86 | 80 | } |
87 | | - |
| 81 | + return returned; |
| 82 | + } |
88 | 83 | } |
0 commit comments