Guest User

Untitled

a guest
Jan 19th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.87 KB | None | 0 0
  1.         public void Add(T item, uint index)
  2.         {
  3.             Assert.That(Invariant());
  4.             if (index < 0 || index > Length) throw new ArgumentException("Please use an index value within the following range: 0 <= index <= Length");
  5.             if (item == null) throw new ArgumentNullException("It is not permitted to add null elements to this sequence.");
  6.  
  7.             uint oldLength = Length;
  8.  
  9.             if (index == Length)
  10.             {
  11.                 Add(item);
  12.                 Assert.That(Length == oldLength + 1);
  13.                 return;
  14.             }
  15.  
  16.             Stack<T> temp = new Stack<T>();
  17.  
  18.             uint i = Length-1;
  19.  
  20.             while (i > index)
  21.             {
  22.                 temp.Push(_elements.Pop());
  23.                 i--;
  24.             }
  25.  
  26.             _elements.Pop(); // Discard the current element at index.
  27.  
  28.             _elements.Push(item); // And insert the new element instead.
  29.  
  30.             while (!temp.IsEmpty()) { _elements.Push(temp.Pop()); }
  31.  
  32.             Assert.That(Length == oldLength);
  33.             Assert.That(Invariant());
  34.         }
Add Comment
Please, Sign In to add comment