Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class CircularList<T> : IEnumerable<T>
- {
- private class Node
- {
- public T item;
- public Node next;
- public Node previous;
- }
- Node current;
- int count;
- public int Count
- {
- get { return count; }
- set { }
- }
- public CircularList()
- {
- }
- public void Add(T item)
- {
- if (count == 0)
- {
- current = new Node();
- current.item = item;
- current.next = current;
- current.previous = current;
- count++;
- return;
- }
- Node nodeNew = new Node();
- nodeNew.item = item;
- nodeNew.previous = current.previous;
- nodeNew.next = current;
- current.previous.next = nodeNew;
- current.previous = nodeNew;
- count++;
- }
- public T GetCurrent()
- {
- if (count < 1)
- {
- throw new IndexOutOfRangeException("List is empty.");
- }
- return current.item;
- }
- public void Next()
- {
- if (count < 1)
- {
- throw new IndexOutOfRangeException("List is empty.");
- }
- current = current.next;
- }
- public void Previous()
- {
- if (count < 1)
- {
- throw new IndexOutOfRangeException("List is empty.");
- }
- current = current.previous;
- }
- public void Remove()
- {
- if (count < 1)
- {
- throw new IndexOutOfRangeException("List is empty.");
- }
- current.previous.next = current.next;
- current.next.previous = current.previous;
- current = current.next;
- count--;
- }
- public IEnumerator<T> GetEnumerator()
- {
- Node start = this.current;
- do
- {
- yield return current.item;
- current = current.next;
- }
- while (current != start);
- }
- IEnumerator IEnumerable.GetEnumerator()
- {
- return GetEnumerator();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment