Пример #1
0
        public override T Remove(T value)
        {
            Assertion.Pre(value != null);

            SinglyLinkedListElement <T> finger   = this.head;
            SinglyLinkedListElement <T> previous = null;

            while (finger != null && !value.Equals(finger.GetValue()))
            {
                previous = finger;
                finger   = finger.GetNext();
            }

            if (finger == null)
            {
                return(default(T));
            }

            if (previous == null)
            {
                this.head = finger.GetNext();
            }
            else
            {
                previous.SetNext(finger.GetNext());
            }

            this.count--;

            return(finger.GetValue());
        }
Пример #2
0
        public override T RemoveFromTail()
        {
            Assertion.Pre(this.tail != null);

            SinglyLinkedListElement <T> finger  = this.tail;
            SinglyLinkedListElement <T> oldTail = this.tail;

            while (finger.GetNext() != this.tail)
            {
                finger = finger.GetNext();
            }

            if (finger == this.tail)
            {
                this.tail = null;
            }
            else
            {
                finger.SetNext(this.tail.GetNext());
                this.tail = finger;
            }

            this.count--;
            return(oldTail.GetValue());
        }
Пример #3
0
        public override T RemoveFromTail()
        {
            Assertion.Pre(this.head != null);

            SinglyLinkedListElement <T> previous = null;
            SinglyLinkedListElement <T> finger   = this.head;

            while (finger.GetNext() != null)
            {
                previous = finger;
                finger   = finger.GetNext();
            }

            if (previous == null)
            {
                this.head = null;
            }
            else
            {
                previous.SetNext(finger.GetNext());
            }

            this.count--;

            return(finger.GetValue());
        }
Пример #4
0
        public void TestSetGetNext()
        {
            Assert.AreEqual(null, element.GetNext());
            SinglyLinkedListElement <int> e = new SinglyLinkedListElement <int>(2, element);

            Assert.AreEqual(element, e.GetNext());
            SinglyLinkedListElement <int> e2 = new SinglyLinkedListElement <int>(3, null);

            element.SetNext(e2);
            Assert.AreEqual(e2, element.GetNext());
        }
Пример #5
0
        public override T RemoveFromHead()
        {
            Assertion.Pre(this.head != null);

            SinglyLinkedListElement <T> oldHead = this.head;

            this.head = this.head.GetNext();

            this.count--;

            return(oldHead.GetValue());
        }
Пример #6
0
        public override T PeekTail()
        {
            Assertion.Pre(this.head != null);

            SinglyLinkedListElement <T> finger = this.head;

            while (finger.GetNext() != null)
            {
                finger = finger.GetNext();
            }

            return(finger.GetValue());
        }
Пример #7
0
        public override bool Contains(T value)
        {
            Assertion.Pre(value != null);

            SinglyLinkedListElement <T> finger = this.head;

            while (finger != null && !value.Equals(finger.GetValue()))
            {
                finger = finger.GetNext();
            }

            return(finger != null);
        }
Пример #8
0
        public override void AddToHead(T value)
        {
            SinglyLinkedListElement <T> newHead = new SinglyLinkedListElement <T>(value);

            if (this.tail == null)
            {
                this.tail = newHead;
            }
            else
            {
                SinglyLinkedListElement <T> head = this.tail.GetNext();
                newHead.SetNext(head);
            }

            this.tail.SetNext(newHead);
            this.count++;
        }
Пример #9
0
        public override T RemoveFromHead()
        {
            Assertion.Pre(this.tail != null);

            SinglyLinkedListElement <T> head = this.tail.GetNext();

            if (this.tail == head)
            {
                this.tail = null;
            }
            else
            {
                this.tail.SetNext(head.GetNext());
            }

            this.count--;

            return(head.GetValue());
        }
Пример #10
0
        public override void AddToTail(T value)
        {
            SinglyLinkedListElement <T> newTail = new SinglyLinkedListElement <T>(value);

            if (this.head == null)
            {
                this.head = newTail;
            }
            else
            {
                SinglyLinkedListElement <T> finger = this.head;

                while (finger.GetNext() != null)
                {
                    finger = finger.GetNext();
                }

                finger.SetNext(newTail);
            }
            this.count++;
        }
Пример #11
0
        public override bool Contains(T value)
        {
            Assertion.Pre(value != null);

            SinglyLinkedListElement <T> finger = this.tail;

            if (finger == null)
            {
                return(false);
            }

            do
            {
                if (value.Equals(finger.GetValue()))
                {
                    return(true);
                }

                finger = finger.GetNext();
            } while (finger != this.tail);

            return(false);
        }
Пример #12
0
 public override void AddToTail(T value)
 {
     AddToHead(value);
     this.tail = this.tail.GetNext();
 }
Пример #13
0
 public void SetNext(SinglyLinkedListElement <T> next)
 {
     this.next = next;
 }
Пример #14
0
 public void TestSetup()
 {
     element = new SinglyLinkedListElement <int>(1);
 }
Пример #15
0
 public SinglyLinkedListElement(T value, SinglyLinkedListElement <T> next)
 {
     this.value = value;
     this.next  = next;
 }
Пример #16
0
 public override void AddToHead(T value)
 {
     this.head = new SinglyLinkedListElement <T>(value, this.head);
     this.count++;
 }
Пример #17
0
 public override void Clear()
 {
     this.head  = null;
     this.count = 0;
 }
Пример #18
0
 public override void Clear()
 {
     this.tail  = null;
     this.count = 0;
 }