Пример #1
0
        public bool Remove(T item)
        {
            Current = Head;
            for (var i = 0; i < Count; i++)
            {
                if (!Current.Item.Equals(item))
                {
                    continue;
                }
                if (Current == Head)
                {
                    Head = Head.Next;
                }
                else if (Current == Tail)
                {
                    Tail = Tail.Prev;
                }
                else
                {
                    Current.Prev.Next = Current.Next;
                    Current.Next.Prev = Current.Prev;
                    Current           = Current.Next;
                }
                Count--;
                return(true);
            }

            return(false);
        }
Пример #2
0
        public void RemoveAt(int index)
        {
            if (index < 0 || index >= Count)
            {
                throw new IndexOutOfRangeException();
            }
            if (index == 0)
            {
                Head = Head.Next;
            }
            else if (index == Count - 1)
            {
                Tail = Tail.Prev;
            }
            else
            {
                var temp = Current;
                MoveToPosition(index);
                if (temp == Current)        //falls der momentane Knoten gelöscht wird
                {
                    temp = Current.Next;
                }
                Current.Prev.Next = Current.Next;
                Current.Next.Prev = Current.Prev;
                Current           = temp;
            }

            Count--;
        }
Пример #3
0
 public void Clear()
 {
     Current = null;
     Head    = null;
     Tail    = null;
     Count   = 0;
 }
Пример #4
0
 private void Init(T item)
 {
     Current = new SickLinkedNode <T>(item);
     Head    = Current;
     Tail    = Current;
     Count   = 1;
 }
Пример #5
0
        public void Sort()
        {
            var temp = this.OrderBy(e => e).ToSickLinkedList();

            Head    = temp.Head;
            Tail    = temp.Tail;
            Current = Head;
        }
Пример #6
0
        public void InsertAt(int index, T value, Insertion insertion = Insertion.Before)
        {
            if (index < 0 || index > Count)
            {
                throw new IndexOutOfRangeException();
            }
            if (index == Count)
            {
                Add(value);
                return;
            }
            if (index == 0)
            {
                Head = new SickLinkedNode <T>(
                    item: value,
                    n: Head
                    );
                Head.Next.Prev = Head;
            }
            else if (index == Count - 1)
            {
                Tail = new SickLinkedNode <T>(
                    item:  value,
                    p: Tail
                    );
                Tail.Prev.Next = Tail;
            }
            else
            {
                var temp = Current;
                MoveToPosition(index);
                switch (insertion)
                {
                case Insertion.InFront:
                    Current.Next = new SickLinkedNode <T>(
                        item: value,
                        n: Current.Next,
                        p: Current
                        );
                    Current.Next.Next.Prev = Current.Next;
                    break;

                case Insertion.Before:
                    Current.Prev = new SickLinkedNode <T>(
                        item: value,
                        n: Current,
                        p: Current.Prev
                        );
                    Current.Prev.Prev.Next = Current.Prev;
                    break;
                }
                Current = temp;
            }

            Count++;
        }
Пример #7
0
 public T this[int index]
 {
     get
     {
         MoveToPosition(index);
         return(Current.Item);
     }
     set
     {
         if (index < 0)
         {
             throw new IndexOutOfRangeException();
         }
         if (index >= Count)
         {
             if (Count == 0)
             {
                 if (index == 0)
                 {
                     Init(value);
                     return;
                 }
                 else
                 {
                     Init(default(T));
                 }
             }
             Current = Tail;
             for (var i = 0; i < index - Count; i++)
             {
                 Current.Next = new SickLinkedNode <T>(
                     item: default(T),
                     p: Current
                     );
                 Current = Current.Next;
             }
             Current.Next = new SickLinkedNode <T>(
                 item: value,
                 p: Current
                 );
             Current = Current.Next;
             Tail    = Current;
             Count   = index + 1;
         }
         else
         {
             MoveToPosition(index);
             Current.Item = value;
         }
     }
 }
Пример #8
0
 public void Add(T item)
 {
     if (Count == 0)
     {
         Init(item);
     }
     else
     {
         Tail.Next = new SickLinkedNode <T>(
             item: item,
             p: Tail
             );
         Tail = Tail.Next;
         Count++;
     }
 }
Пример #9
0
 public SickLinkedList(IEnumerable <T> en)
 {
     using (var e = en.GetEnumerator())
     {
         e.MoveNext();
         Current = new SickLinkedNode <T>(e.Current);
         Head    = Current;
         Count   = 1;
         while (e.MoveNext())
         {
             Current.Next = new SickLinkedNode <T>(
                 item: e.Current,
                 p: Current
                 );
             Current = Current.Next;
             Count++;
         }
         Tail = Current;
     }
 }