-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStack.cs
86 lines (71 loc) · 1.36 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
class Stack<T>(int size)
{
public int head = 0;
private readonly T[] stack = new T[size];
public void Push(T el)
{
stack[head] = el;
head++;
}
public T Pop()
{
head--;
T popped = stack[head];
return popped;
}
public void Clear()
{
head = 0;
}
public bool IsEmpty()
{
return head == 0;
}
public void StackLogger(int n)
{
int count = 0;
foreach (var item in stack)
{
Console.Write(item + " ");
count++;
}
while (count < n)
{
Console.Write(0 + " ");
count++;
}
}
public T ElementAt(int index)
{
return stack[index];
}
public int Length()
{
return size;
}
public int Count()
{
return head;
}
public T Peek()
{
return stack[0];
}
public T Pook()
{
if (head == 0)
{
return stack[0];
}
return stack[head - 1];
}
public Stack<S> Map<S>(Func<T, S> Mapper)
{
Stack<S> result = new(head);
for (int i = 0; i < head; i++)
{
result.Push(Mapper(stack[i]));
}
return result;
}
}