Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Collections;
- using System.Linq;
- namespace DataTypes
- {
- class MyList<T> : IEnumerable<T>
- {
- public delegate void ProcessDelegate(T item);
- Node first;
- Node last;
- int n = 0;
- public int N
- {
- get { return n; }
- }
- private class Node
- {
- public T item;
- public Node previous;
- public Node next;
- }
- public bool IsEmpty { get { return first == null; } }
- public void Add(T item)
- {
- Node oldLast = last;
- last = new Node();
- last.item = item;
- last.previous = oldLast;
- last.next = null;
- if (IsEmpty)
- first = last;
- else
- oldLast.next = last;
- n++;
- }
- public T Get(int index)
- {
- int i = 0;
- foreach (T item in this)
- {
- if (i == index)
- {
- return item;
- }
- i++;
- }
- return default(T);
- }
- public void Set(int index, T itemIn)
- {
- int i = 0;
- Node current = first;
- while (current != null)
- {
- if (i == index)
- {
- current.item = itemIn;
- break;
- }
- current = current.next;
- i++;
- }
- }
- public int IndexOf(T item)
- {
- int i = 0;
- foreach (T it in this)
- {
- if (item.Equals(it))
- {
- return i;
- }
- i++;
- }
- return -1;
- }
- public void Remove(T item)
- {
- if (this.IsEmpty) return;
- for (Node node = first; node != null; node = node.next)
- {
- if (item.Equals(node.item))
- {
- if (node.next != null && node.previous != null)
- {
- node.previous.next = node.next;
- node.next.previous = node.previous;
- }
- else
- {
- if (node.next == null)
- {
- node.previous.next = null;
- last = node.previous;
- }
- else
- {
- node.next.previous = null;
- first = node.next;
- }
- }
- break;
- }
- }
- n--;
- }
- public void AddRange(MyList<T> that)
- {
- if (!that.IsEmpty && !this.IsEmpty)
- {
- this.last.next = that.first;
- that.first.previous = this.last;
- this.last = that.last;
- }
- else if (this.IsEmpty && !that.IsEmpty)
- {
- this.first = that.first;
- }
- n += that.n;
- }
- public static MyList<T> operator + (MyList<T> listIn1, MyList<T> listIn2)
- {
- MyList<T> listOut = new MyList<T>();
- foreach (T item in listIn1)
- {
- listOut.Add(item);
- }
- foreach (T item in listIn2)
- {
- listOut.Add(item);
- }
- return listOut;
- }
- public static MyList<T> operator + (MyList<T> listIn, T item)
- {
- listIn.Add(item);
- return listIn;
- }
- public T this[int index]
- {
- get { return this.Get(index); }
- set { this.Set(index, value); }
- }
- public void each(ProcessDelegate function)
- {
- foreach (T item in this)
- {
- function(item);
- }
- }
- public IEnumerator<T> GetEnumerator()
- {
- Node current = first;
- while (current != null)
- {
- yield return current.item;
- current = current.next;
- }
- }
- IEnumerator IEnumerable.GetEnumerator()
- {
- return GetEnumerator();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment