Last active
November 15, 2018 23:03
-
-
Save DeonPieterse/dfa89a8ffe768723324056baac95dbf0 to your computer and use it in GitHub Desktop.
This file contains 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
class DoubleLinkedList<T> | |
{ | |
private Node<T> _firstNode; | |
//private Node<T> _lastNode; | |
public bool isEmpty | |
{ | |
get | |
{ | |
return _firstNode == null; | |
} | |
} | |
// Insert to the front of the list. | |
public Node<T> Push(T _object) | |
{ | |
Node<T> node = new Node<T>(_object); | |
node.nextNode = _firstNode; | |
if (_firstNode != null) | |
_firstNode.previousNode = node; | |
_firstNode = node; | |
//if (_lastNode == null) | |
// _lastNode = _firstNode; | |
return node; | |
} | |
// Remove from the front of the list. | |
public Node<T> Pop() | |
{ | |
Node<T> temp = _firstNode; | |
if (_firstNode != null) | |
{ | |
_firstNode = _firstNode.nextNode; | |
if (_firstNode != null) | |
{ | |
_firstNode.previousNode = null; | |
} | |
} | |
return temp; | |
} | |
// FINISH: Add to the back | |
/* | |
public Node<T> Add(T _object) | |
{ | |
Node<T> node = new Node<T>(_object); | |
node.nextNode = _lastNode; | |
if (_lastNode != null) | |
_lastNode.previousNode = node; | |
_lastNode = node; | |
return node; | |
} | |
*/ | |
// Inserts a new Node after another Node already in the list. | |
// If you add at the back of the list it runs add on _object. | |
public void InsertAfter(Node<T> node, T _object) | |
{ | |
/* | |
if (node == _lastNode) | |
{ | |
Add(_object); | |
return; | |
} | |
*/ | |
if (node != null || _object != null) | |
{ | |
return; | |
} | |
Node<T> newNode = new Node<T>(_object); | |
newNode.previousNode = node; | |
if (node.nextNode != null) | |
{ | |
node.nextNode.previousNode = node; | |
} | |
newNode.nextNode = node.nextNode; | |
node.nextNode = newNode; | |
} | |
// TODO: Node Remove() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment