Created
April 6, 2016 20:28
-
-
Save philippdolder/8d248d01ef65e9f277e6c49da5421329 to your computer and use it in GitHub Desktop.
linked list
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
namespace LinkedList | |
{ | |
public class List<T> : IEnumerable<T>, IEnumerator<T> | |
{ | |
public IEnumerator<T> GetEnumerator() | |
{ | |
this.current = null; | |
return this; | |
} | |
public bool MoveNext() | |
{ | |
if (this.current == null) | |
{ | |
this.current = this.head; | |
} | |
else | |
{ | |
this.Next(); | |
} | |
return this.current.next != null; | |
} | |
public void Reset() | |
{ | |
this.current = null; | |
} | |
public T Current | |
{ | |
get { return this.GetElement(); } | |
} | |
private Node<T> head; | |
private Node<T> current; | |
private int numberOfElements; | |
public List() { current = head = null; numberOfElements = 0; } | |
~List() | |
{ | |
while (head.next != null) | |
{ | |
current = head; | |
head = head.next; | |
current = null; | |
} | |
head = null; | |
numberOfElements = 0; | |
} | |
public void First() { current = head; } | |
public void Next() { current = current.next; } | |
public bool ThisIsNotNull() | |
{ | |
if (current == null) return false; | |
return true; | |
} | |
public virtual void Add(T data) | |
{ | |
if (head == null) AddLast(data); | |
else if (current.next == null) AddLast(data); | |
else | |
{ | |
Node<T> toAdd = new Node<T>(); | |
toAdd.data = data; | |
toAdd.next = current.next; | |
current.next = toAdd; | |
numberOfElements++; | |
} | |
} | |
public void AddFirst(T data) | |
{ | |
Node<T> toAdd = new Node<T>(); | |
toAdd.data = data; | |
toAdd.next = head; | |
head = toAdd; | |
numberOfElements++; | |
} | |
public void AddLast(T data) | |
{ | |
if (head == null) | |
{ | |
head = new Node<T>(); | |
head.data = data; | |
head.next = null; | |
numberOfElements++; | |
} | |
else | |
{ | |
Node<T> toAdd = new Node<T>(); | |
toAdd.data = data; | |
current = head; | |
while (current.next != null) | |
{ | |
current = current.next; | |
} | |
current.next = toAdd; | |
numberOfElements++; | |
} | |
} | |
public void Delete() | |
{ | |
if (head != null) | |
{ | |
if (current == head && current.next == null) | |
{ | |
current = head = null; | |
numberOfElements = 0; | |
} | |
else | |
{ | |
if (current == head && current.next != null) | |
{ | |
head = head.next; | |
current = null; | |
numberOfElements--; | |
} | |
else | |
{ | |
if (current.next != null) | |
{ | |
Node<T> deletable = current; | |
current = head; | |
while (current.next != deletable) current = current.next; | |
current.next = current.next.next; | |
deletable = null; | |
numberOfElements--; | |
} | |
else | |
{ | |
if (current.next == null) | |
{ | |
Node<T> deletable = head; | |
while (deletable.next != current) deletable = deletable.next; | |
current = null; | |
current = deletable; | |
numberOfElements--; | |
} | |
} | |
} | |
} | |
} | |
} | |
public T GetElement() | |
{ | |
return current.data; | |
} | |
public int Size() | |
{ | |
return numberOfElements; | |
} | |
public bool Contains(T data) | |
{ | |
current = head; | |
while (current != null) | |
{ | |
if (current.data.Equals(data)) | |
return true; | |
current = current.next; | |
} | |
return false; | |
} | |
} | |
class Node<T> | |
{ | |
public T data; | |
public Node<T> next; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment