Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- namespace ConsoleApp1
- {
- class MyLinkedList<T> : IList<T> where T : class
- {
- class LinkedListItem
- {
- public T Value { get; set; }
- public LinkedListItem Next { get; set; }
- public override string ToString()
- {
- return new { Value, Next }.ToString();
- }
- }
- class LinkedListEnumerator : IEnumerator<T>
- {
- readonly MyLinkedList<T> _parent;
- LinkedListItem _currentItem;
- public LinkedListEnumerator(MyLinkedList<T> parent)
- {
- _parent = parent;
- }
- public T Current { get { return _currentItem?.Value; } }
- object IEnumerator.Current { get { return Current; } }
- public void Dispose()
- {
- }
- public bool MoveNext()
- {
- if (_currentItem == null)
- {
- Reset();
- return true;
- }
- _currentItem = _currentItem?.Next;
- return _currentItem != null;
- }
- public void Reset()
- {
- _currentItem = _parent._head;
- }
- }
- LinkedListItem _head;
- public int Count { get; private set; }
- public bool IsReadOnly { get; } = false;
- public T this[int index]
- {
- get
- {
- if (index < 0 || index >= Count)
- throw new IndexOutOfRangeException();
- var i = 0;
- var current = _head;
- while (i++ != index)
- current = current.Next;
- return current.Value;
- }
- set
- {
- if (index < 0 || index >= Count)
- throw new IndexOutOfRangeException();
- var i = 0;
- var current = _head;
- while (i++ != index)
- current = current.Next;
- current.Value = value;
- }
- }
- public void Add(T item)
- {
- if (_head == null)
- _head = new LinkedListItem { Value = item };
- else
- {
- var current = _head;
- while (current.Next != null)
- current = current.Next;
- current.Next = new LinkedListItem { Value = item };
- }
- Count++;
- }
- public bool Remove(T value)
- {
- var current = _head;
- var prev = default(LinkedListItem);
- while (current != null && current.Value != value)
- {
- prev = current;
- current = current.Next;
- }
- if (current == null)
- return false;
- if (prev == null)
- _head = current.Next;
- else
- prev.Next = current.Next;
- Count--;
- return true;
- }
- public int IndexOf(T item)
- {
- var current = _head;
- var index = 0;
- while (current != null && current.Value != item)
- {
- index++;
- current = current.Next;
- }
- return current == null ? -1 : index;
- }
- public void Insert(int index, T item)
- {
- if (index < 0 || index >= Count)
- throw new ArgumentException(nameof(index));
- var current = _head;
- var prev = default(LinkedListItem);
- var i = 0;
- while(i++ < index)
- {
- prev = current;
- current = current.Next;
- }
- if (prev != null)
- prev.Next = new LinkedListItem { Value = item, Next = current };
- else
- _head = new LinkedListItem { Value = item, Next = current };
- Count++;
- }
- public void RemoveAt(int index)
- {
- if (index < 0 || index >= Count)
- throw new ArgumentException(nameof(index));
- var current = _head;
- var prev = default(LinkedListItem);
- var i = 0;
- while (i++ < index)
- {
- prev = current;
- current = current.Next;
- }
- if (prev != null)
- prev.Next = current.Next;
- else
- _head = current.Next;
- Count--;
- }
- public void Clear()
- {
- _head = null;
- Count = 0;
- }
- public bool Contains(T item)
- {
- return IndexOf(item) > -1;
- }
- public void CopyTo(T[] array, int arrayIndex)
- {
- if (array.Length < arrayIndex + Count)
- throw new ArgumentException(nameof(array));
- var current = _head;
- for (var i = arrayIndex; i < arrayIndex + Count; i++)
- {
- array[i] = current.Value;
- current = current.Next;
- }
- }
- public IEnumerator<T> GetEnumerator()
- {
- return new LinkedListEnumerator(this);
- }
- IEnumerator IEnumerable.GetEnumerator()
- {
- return GetEnumerator();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement