Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public void Add(T item, uint index)
- {
- Assert.That(Invariant());
- if (index < 0 || index > Length) throw new ArgumentException("Please use an index value within the following range: 0 <= index <= Length");
- if (item == null) throw new ArgumentNullException("It is not permitted to add null elements to this sequence.");
- uint oldLength = Length;
- if (index == Length)
- {
- Add(item);
- Assert.That(Length == oldLength + 1);
- return;
- }
- Stack<T> temp = new Stack<T>();
- uint i = Length-1;
- while (i > index)
- {
- temp.Push(_elements.Pop());
- i--;
- }
- _elements.Pop(); // Discard the current element at index.
- _elements.Push(item); // And insert the new element instead.
- while (!temp.IsEmpty()) { _elements.Push(temp.Pop()); }
- Assert.That(Length == oldLength);
- Assert.That(Invariant());
- }
Add Comment
Please, Sign In to add comment