public CustomDictionary <T, U> Remove(T key) { CustomNode <KeyValuePair <T, U> > node = this.head; for (int i = 0; i < this.Count; i++) { if (node.Data.Key.Equals(key)) { if (node.Previous == null) { this.head = node.Next; } else { node.Previous.Next = node.Next; if (node.Next != null) { node.Next.Previous = node.Previous; } } this.Count--; } node = node.Next; } return(this); }
public T[] ToArray() { T[] result = new T[this.Count]; CustomNode <T> node = this.head; for (int i = 0; i < this.Count; i++) { result[i] = node.Data; node = node.Next; } return(result); }
public CustomQueue <T> Enqueue(T element) { CustomNode <T> node = new CustomNode <T>(element); if (this.tail != null) { this.tail.Next = node; } node.Previous = this.tail; this.tail = node; this.Count++; return(this); }
public CustomStack <T> Push(T element) { CustomNode <T> node = new CustomNode <T>(element); node.Next = this.head; if (this.head != null) { this.head.Previous = node; } this.head = node; this.Count++; return(this); }
private void CheckKeyExists(T key) { CustomNode <KeyValuePair <T, U> > node = this.head; for (int i = 0; i < this.Count; i++) { if (node.Data.Key.Equals(key)) { throw new ArgumentException("This key already exists in the dictionary"); } node = node.Next; } }
public T Dequeue() { CustomNode <T> result = this.head; if (head == null) { throw new NullReferenceException("Nothing to dequeue"); } this.head = this.head.Next; if (this.head != null) { this.head.Previous = null; } this.Count--; return(result.Data); }
public T Pop() { CustomNode <T> result = this.head; if (this.head == null) { throw new NullReferenceException("No elements to pop"); } this.head = this.head.Next; if (this.head != null) { this.head.Previous = null; } this.Count--; return(result.Data); }
public override string ToString() { string s = "[head:"; CustomNode <T> node = this.head; for (int i = 0; i < this.Count - 1; i++) { s += String.Format("{0}->", node.Data.ToString()); node = node.Next; } if (this.Count != 0) { s += node.Data.ToString(); } s += ":tail]"; return(s); }
public override string ToString() { string s = "["; CustomNode <KeyValuePair <T, U> > node = this.head; for (int i = 0; i < this.Count - 1; i++) { s += String.Format(" {0};", node.Data.ToString()); node = node.Next; } if (this.Count != 0) { s += " " + node.Data.ToString(); } s += " ]"; return(s); }
public CustomDictionary <T, U> Add(KeyValuePair <T, U> element) { CheckKeyExists(element.Key); if (tail != null) { tail.Next = new CustomNode <KeyValuePair <T, U> >(element) { Previous = tail }; } else { tail = new CustomNode <KeyValuePair <T, U> >(element); head = tail; } tail = tail.Next; Count++; return(this); }
public U this[T key] { get { CustomNode <KeyValuePair <T, U> > node = this.head; for (int i = 0; i < this.Count; i++) { if (node.Data.Key.Equals(key)) { return(node.Data.Value); } node = node.Next; } throw new KeyNotFoundException(); } set { CheckKeyExists(key); this.Add(new KeyValuePair <T, U>(key, value)); } }
public CustomQueue(T head) { this.head = new CustomNode <T>(head); this.tail = this.head; this.Count = 1; }
public CustomStack(T head) { this.head = new CustomNode <T>(head); //this.tail = this.head; this.Count = 1; }
public CustomDictionary(KeyValuePair <T, U> firstElement) { head = new CustomNode <KeyValuePair <T, U> >(firstElement); tail = head; Count++; }
public CustomNode(T data, CustomNode <T> previous = null, CustomNode <T> next = null) { this.Data = data; this.Previous = previous; this.Next = next; }