//display list public void Show() { Generalized <T> cur = head; while (cur != tail.Next) { Console.WriteLine(cur.Data.ToString()); cur = cur.Next; } }
IEnumerator <T> IEnumerable <T> .GetEnumerator() { Generalized <T> cur = head; while (cur != null) { yield return(cur.Data); cur = cur.Next; } }
//add begin public void AppendFirst(T data) { Generalized <T> gen = new Generalized <T>(data); gen.Next = head; head = gen; if (count == 0) { tail = head; } count++; }
//is element in list? public bool Constrain(T data) { Generalized <T> cur = head; while (cur != null) { if (cur.Data.Equals(data)) { return(true); } cur = cur.Next; } return(false); }
int count; //count elements in list //add element public void Add(T data) { Generalized <T> gen = new Generalized <T>(data); if (head == null) { head = gen; } else { tail.Next = gen; } tail = gen; count++; }
//delete element public bool Remove(T data) { Generalized <T> cur = head; Generalized <T> prev = null; while (cur != null) { if (cur.Data.Equals(data)) { //if data inside or end of list if (prev != null) { //remove cur, prev ref on car.Next prev.Next = cur.Next; //if cur.Next = null, than prev = tail if (cur.Next == null) { tail = prev; } } //if remove first el, update value 'head' else { head = head.Next; if (head == null) { tail = null; } } count--; return(true); } prev = cur; cur = cur.Next; } return(false); }
//clear list public void Clear() { head = null; tail = null; count = 0; }