// delete element with value deleteNum. public void DeleteElement(ElementType deleteNum) { if (head == null) { Console.WriteLine("List is empty"); return; } ListElement temp = head; if (head.Value() == deleteNum) { head = head.Next(); } else { while (temp.Next() != null && temp.Next().Value() != deleteNum) { temp = temp.Next(); } if (temp.Next() != null) { temp.DeleteListElement(); } } }
// return true or false - exist element value in stack. public bool Exist(ElementType value) { if (head == null) { return(false); } ListElement temp = head; for (temp = head; temp.Next() != null && temp.Value() != value; temp = temp.Next()) { ; } return(temp.Value() == value); }
// add element to end with value value. public void AddElement(ElementType value) { if (head == null) { head = new ListElement(value, null); return; } ListElement temp = head; for (temp = head; temp.Next() != null; temp = temp.Next()) { ; } temp.AddListElement(value); }
/// <summary> /// Print stack. /// </summary> public void Print() { if (head == null) { Console.WriteLine("Stack is empty"); return; } ListElement pos = head; for (pos = head; pos.Next() != null; pos = pos.Next()) { Console.Write("{0} -> ", pos.Value()); } Console.Write("{0}", pos.Value()); Console.WriteLine(); }
// print stack. public void Print() { Console.WriteLine(); if (head == null) { Console.WriteLine("List is Empty"); return; } ListElement temp = head; for (temp = head; temp.Next() != null; temp = temp.Next()) { Console.Write("{0}, ", temp.Value()); } Console.WriteLine("{0}", temp.Value()); }
// add element after position pos. public void AddElementToPosition(ElementType value, Position pos) { if (head == null) { head = new ListElement(value, null); return; } if (pos == 0) { AddElementToHead(value); return; } ListElement temp = head; int i = 0; for (i = 0; temp.Next() != null && i != pos - 1; temp = temp.Next(), i++) { ; } temp.AddListElement(value); }
//return position element with vale value. public Position PositionElement(ElementType value) { ListElement temp = head; int i = 0; for (temp = head; temp != null && temp.Value() != value; temp = temp.Next(), i++) { ; } if (temp.Value() == value) { return(i); } return(-1); }