public void RemoveItem(int item) { L2Node current = first; while (current != null) { if (current.Value == item && first == last) { first = null; last = null; Length--; } else if (current.Value == item && current.Previous == null) { current.Next.Previous = null; first = current.Next; Length--; } else if (current.Value == item && current.Next == null) { current.Previous.Next = null; last = current.Previous; Length--; } else if (current.Value == item) { current.Previous.Next = current.Next; current.Next.Previous = current.Previous; Length--; } current = current.Next; } }
public void SwapNodes(L2Node a, L2Node b) { a.Next = b.Next; b.Next.Previous = a; a.Previous.Next = b; b.Previous = a.Previous; b.Next = a; a.Previous = b; }
public void SortUp() { L2Node tmpListFirst = null; L2Node tmpListLast = null; for (int i = 0; i < Length; i++) { L2Node current = first; L2Node min = first; while (current != null) { if (current.Value < min.Value) { min = current; } current = current.Next; } if (first == last) { first = null; last = null; } else if (min == first) { first.Next.Previous = null; first = first.Next; } else if (min == last) { last.Previous.Next = null; last = last.Previous; } else { min.Previous.Next = min.Next; min.Next.Previous = min.Previous; } if (tmpListFirst != null) { tmpListLast.Next = min; min.Previous = tmpListLast; tmpListLast = tmpListLast.Next; } else { tmpListFirst = min; tmpListLast = tmpListFirst; } } first = tmpListFirst; last = tmpListLast; }
public void RemoveOfIndex(int index, int quantity) { //for (int i = 0; i < quantity; i++) //{ // this.RemoveOfIndex(index); //} if (index == 0) { this.RemoveFromStart(quantity); } else if (index < 0 || index > Length - 1) { throw new IndexOutOfRangeException(); } else if (index == Length - quantity) { this.Remove(quantity); } else if (index <= Length / 2) { L2Node previous = first; for (int i = 0; i < index - 1; i++) { previous = previous.Next; } L2Node next = previous; for (int i = 0; i <= quantity; i++) { next = next.Next; } previous.Next = next; next.Previous = previous; Length -= quantity; } else { L2Node previous = last; for (int i = 0; i < Length - index; i++) { previous = previous.Previous; } L2Node next = previous; for (int i = 0; i <= quantity; i++) { next = next.Next; } previous.Next = next; next.Previous = previous; Length -= quantity; } }
public void Insert(int item, int index) { if (index == 0) { AddToStart(item); } else if (index < 0 || index > Length) { throw new IndexOutOfRangeException(); } else if (index == Length) { this.Add(item); } else if (index <= Length / 2) { L2Node tmp = new L2Node(item); L2Node previous = first; for (int i = 0; i < index - 1; i++) { previous = previous.Next; } tmp.Next = previous.Next; previous.Next.Previous = tmp; previous.Next = tmp; tmp.Previous = tmp; Length++; } else { L2Node tmp = new L2Node(item); L2Node previous = last; for (int i = 0; i < Length - index; i++) { previous = previous.Previous; } tmp.Next = previous.Next; previous.Next.Previous = tmp; previous.Next = tmp; tmp.Previous = tmp; Length++; } }
public int MinItem() { L2Node current = first; int min = current.Value; while (current != null) { if (current.Value < min) { min = current.Value; } current = current.Next; } return(min); }
public int MaxItem() { L2Node current = first; int max = current.Value; while (current != null) { if (current.Value > max) { max = current.Value; } current = current.Next; } return(max); }
public void RemoveFromStart() { if (first == last) { first = null; last = null; Length = 0; } else if (first != null) { first = first.Next; first.Previous = null; Length--; } }
public void Remove() { if (first == last) { first = null; last = null; Length = 0; } else if (first != null) { last.Previous.Next = null; last = last.Previous; Length--; } }
public void AddToStart(int a) { if (first != null) { first.Previous = new L2Node(a); first.Previous.Next = first; first = first.Previous; Length++; } else { first = new L2Node(a); last = first; Length = 1; } }
public int[] ReturnMassive() { int[] array = new int[Length]; if (Length != 0) { int i = 0; L2Node tmp = first; do { array[i] = tmp.Value; i++; tmp = tmp.Next; } while (tmp != null); } return(array); }
public void Add(int a) { if (first == null) { first = new L2Node(a); last = first; Length = 1; } else { last.Next = new L2Node(a); last.Next.Previous = last; last = last.Next; Length++; } }
public void Reverse() { L2Node current = first; L2Node tmp = first; first = last; last = current; while (current != null) { tmp = current.Next; current.Next = current.Previous; current.Previous = tmp; current = tmp; } }
public int MaxItemIndex() { L2Node current = first; int max = current.Value; int count = 0; int maxIndex = 0; while (current != null) { if (current.Value > max) { max = current.Value; maxIndex = count; } current = current.Next; count++; } return(maxIndex); }
public int MinItemIndex() { L2Node current = first; int min = current.Value; int count = 0; int minIndex = 0; while (current != null) { if (current.Value < min) { min = current.Value; minIndex = count; } current = current.Next; count++; } return(minIndex); }
public void RemoveOfIndex(int index) { if (index == 0) { this.RemoveFromStart(); } else if (index < 0 || index > Length - 1) { throw new IndexOutOfRangeException(); } else if (index == Length - 1) { this.Remove(); } else if (index <= Length / 2) { L2Node previous = first; for (int i = 0; i < index - 1; i++) { previous = previous.Next; } previous.Next = previous.Next.Next; previous.Next.Previous = previous; Length--; } else { L2Node previous = last; for (int i = 0; i < Length - index; i++) { previous = previous.Previous; } previous.Next = previous.Next.Next; previous.Next.Previous = previous; Length--; } }
public int IndexOfItem(int item) { L2Node current = first; int count = 0; while (current != null) { if (current.Value == item) { break; } count++; current = current.Next; } if (current == null) { return(-1); } else { return(count); } }
public L2Node() { Next = null; Previous = null; }
public L2List() { first = null; last = null; Length = 0; }
public void SortDown() { for (int i = 0; i < Length; i++) { L2Node current = first; for (int j = 0; j < Length - 1 - i; j++) { if (current.Value < current.Next.Value) { if (j == 0 && j == Length - 2) { L2Node a = current; L2Node b = current.Next; b.Next = a; a.Previous = b; a.Next = null; b.Previous = null; first = b; last = a; } else if (j == 0) { L2Node a = current; L2Node b = current.Next; a.Next = b.Next; b.Next.Previous = a; b.Previous = null; b.Next = a; a.Previous = b; first = b; } else if (j == Length - 2) { L2Node a = current; L2Node b = current.Next; b.Previous = a.Previous; a.Previous.Next = b; b.Next = a; a.Previous = b; a.Next = null; last = a; } else { SwapNodes(current, current.Next); } } else { current = current.Next; } } } //L2Node tmpListFirst = null; //L2Node tmpListLast = null; //for (int i = 0; i < Length; i++) //{ // L2Node current = first; // L2Node max = first; // while (current != null) // { // if (current.Value > max.Value) // { // max = current; // } // current = current.Next; // } // if (first == last) // { // first = null; // last = null; // } // else if (max == first) // { // first.Next.Previous = null; // first = first.Next; // } // else if (max == last) // { // last.Previous.Next = null; // last = last.Previous; // } // else // { // max.Previous.Next = max.Next; // max.Next.Previous = max.Previous; // } // if (tmpListFirst != null) // { // tmpListLast.Next = max; // max.Previous = tmpListLast; // tmpListLast = tmpListLast.Next; // } // else // { // tmpListFirst = max; // tmpListLast = tmpListFirst; // } //} //first = tmpListFirst; //last = tmpListLast; }
public L2List(int a) { first = new L2Node(a); last = first; Length = 1; }
public L2Node(int value) { Value = value; Next = null; Previous = null; }
public int this[int index] { get { if (index == Length - 1) { return(last.Value); } else if (index == 0) { return(first.Value); } else if (index < 0 || index > Length - 1) { throw new IndexOutOfRangeException(); } else if (index <= Length / 2) { L2Node current = first; for (int i = 0; i < index; i++) { current = current.Next; } return(current.Value); } else { L2Node current = last; for (int i = 0; i < Length - 1 - index; i++) { current = current.Previous; } return(current.Value); } } set { if (index == Length - 1) { last.Value = value; } else if (index == 0) { first.Value = value; // 5-9 изменение по индексу } else if (index < 0 || index > Length - 1) { throw new IndexOutOfRangeException(); } else if (index <= Length / 2) { L2Node current = first; for (int i = 0; i < index; i++) { current = current.Next; } current.Value = value; } else { L2Node current = last; for (int i = 0; i < Length - 1 - index; i++) { current = current.Previous; } current.Value = value; } } }