forked from farQtech/Data-Structures
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack.cs
More file actions
82 lines (59 loc) · 1.52 KB
/
Stack.cs
File metadata and controls
82 lines (59 loc) · 1.52 KB
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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
namespace DataStructSandBox.DataStructures
{
public interface IStack<T>{
int Size();
bool IsEmpty();
T Pop();
void Push(T data);
T Top();
}
class Stack<T> : IStack<T>, IEnumerable
{
private LinkedList<T> ll = new LinkedList<T>();
public Stack()
{
}
public int Size() => this.ll.Count;
public bool IsEmpty() => this.ll.Count == 0;
public T Pop()
{
if (this.IsEmpty())
throw new NullReferenceException();
T data = this.ll.First.Value;
this.ll.RemoveFirst();
return data;
}
public void Push(T data)
{
this.ll.AddFirst(data);
}
public T Top()
{
return this.ll.First.Value;
}
public IEnumerator GetEnumerator() => this.ll.GetEnumerator();
}
// driver
public class Program
{
public static int Main()
{
Stack<int> st = new Stack<int>();
st.Push(1);
st.Push(8);
st.Push(6);
st.Push(7);
foreach (int i in st)
{
Console.Write("{0} ", i);
}
// output: 7 6 8 1
// last value is at the top
return 0;
}
}
}