Created
August 15, 2019 22:11
-
-
Save oshea00/e067caf6b16bce80110ccb5f7f7e19f9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void Main() | |
{ | |
var l = new LinkedList<string>(); | |
l.Push("A"); | |
l.Push("B"); | |
l.Push("C"); | |
l.Push("D"); | |
l.Push("E"); | |
String.Join(", ",l.ToList()).Dump(); | |
l.Reverse(); | |
String.Join(", ", l.ToList()).Dump(); | |
} | |
// Define other methods and classes here | |
class Node<T> { | |
public T Value; | |
public Node<T> Next; | |
} | |
class LinkedList<T> { | |
Node<T> Head; | |
int count = 0; | |
public int Count => count; | |
public LinkedList() | |
{ | |
Head = new Node<T>(); | |
} | |
public List<T> ToList() { | |
var listvals = new List<T>(); | |
var curr = Head.Next; | |
while (curr != null) { | |
listvals.Add(curr.Value); | |
curr = curr.Next; | |
} | |
return listvals; | |
} | |
public T Pop() { | |
if (Count == 0) | |
throw new Exception("Empty List"); | |
var top = Head.Next; | |
Head.Next = top.Next; | |
count--; | |
return top.Value; | |
} | |
public void Push(T value) | |
{ | |
var node = new Node<T> { Value = value }; | |
node.Next = Head.Next; | |
Head.Next = node; | |
count++; | |
} | |
public void Reverse() { | |
var prev = (Node<T>) null; | |
var curr = Head.Next; | |
var next = (Node<T>) null; | |
while (curr != null) { | |
next = curr.Next; | |
curr.Next = prev; | |
prev = curr; | |
curr = next; | |
} | |
Head.Next = prev; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment