-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathLinkedListStack.cs
More file actions
43 lines (35 loc) · 863 Bytes
/
LinkedListStack.cs
File metadata and controls
43 lines (35 loc) · 863 Bytes
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
using System;
namespace AlgorithmsAndDataStructures.DataStructures.Stack;
public class LinkedListStack<T>
{
private LinkedListStackNode<T> head;
public LinkedListStack()
{
head = null;
}
public bool IsEmpty => head == null;
public void Push(T value)
{
if (head == null)
{
head = new LinkedListStackNode<T> { Value = value };
}
else
{
var node = new LinkedListStackNode<T> { Value = value, Next = head };
head = node;
}
}
public T Pop()
{
if (IsEmpty) throw new ArgumentException("Stack is Empty");
var value = head.Value;
head = head.Next;
return value;
}
public T Peak()
{
if (IsEmpty) throw new ArgumentException("Stack is Empty");
return head.Value;
}
}