Example #1
0
        public void Should_Remove_Last()
        {
            //arrange
            var node1 = new MyDoublyLinkedListNode <int>(2);
            var node2 = new MyDoublyLinkedListNode <int>(4);
            var node3 = new MyDoublyLinkedListNode <int>(3);
            var node4 = new MyDoublyLinkedListNode <int>(5);

            node1.Next = node2;
            node2.Next = node3;
            node3.Next = node4;

            node2.Prev = node1;
            node3.Prev = node2;
            node4.Prev = node3;

            var list = new MyDoublyLinkedList <int>(node1);

            //act
            list.RemoveLast();

            //assert
            list.Count.ShouldBeEquivalentTo(3);
            list.Head.ShouldBeEquivalentTo(node1);
            list.Head.Next.ShouldBeEquivalentTo(node2);
            list.Head.Next.Next.ShouldBeEquivalentTo(node3);
        }
Example #2
0
        public void Should_RemoveLast_Throw_If_Empty()
        {
            //arrange
            var list = new MyDoublyLinkedList <int>();

            //act
            Action act = () => list.RemoveLast();

            //assert
            act.ShouldThrow <InvalidOperationException>();
        }
Example #3
0
        public Cat DequeueCat()
        {
            if (_cats.Count == 0)
            {
                throw new InvalidOperationException();
            }

            var cat = _cats.Tail.Data;

            _cats.RemoveLast();
            return(cat);
        }
Example #4
0
        public Dog DequeueDog()
        {
            if (_dogs.Count == 0)
            {
                throw new InvalidOperationException();
            }

            var dog = _dogs.Tail.Data;

            _dogs.RemoveLast();
            return(dog);
        }
Example #5
0
        public void Should_Remove_Last_When_Length_One()
        {
            //arrange
            var node1 = new MyDoublyLinkedListNode <int>(2);
            var list  = new MyDoublyLinkedList <int>(node1);

            //act
            list.RemoveLast();

            //assert
            list.Count.ShouldBeEquivalentTo(0);
            list.Head.Should().BeNull();
            list.Tail.Should().BeNull();
        }
Example #6
0
        private void ArrangeAndActUponLinkedLists(LinkedList <int> originalLinkedList, MyDoublyLinkedList <int> myLinkedList)
        {
            LinkedListNode <int> fiveHundredListNode = new LinkedListNode <int>(500);
            LinkedListNode <int> tenListNode         = new LinkedListNode <int>(10);
            LinkedListNode <int> last = new LinkedListNode <int>(666);

            MyLinkedListNode <int> myFiveHundredListNode = new MyLinkedListNode <int>(500);
            MyLinkedListNode <int> myTenListNode         = new MyLinkedListNode <int>(10);
            MyLinkedListNode <int> myLast = new MyLinkedListNode <int>(666);

            originalLinkedList.AddFirst(tenListNode);
            originalLinkedList.AddBefore(tenListNode, 9);
            originalLinkedList.AddAfter(tenListNode, fiveHundredListNode);
            originalLinkedList.AddLast(last);
            originalLinkedList.Remove(10);
            originalLinkedList.AddAfter(last, 777);
            originalLinkedList.Remove(fiveHundredListNode);
            originalLinkedList.RemoveFirst();
            originalLinkedList.RemoveLast();
            originalLinkedList.Clear();
            originalLinkedList.AddLast(5);
            originalLinkedList.RemoveFirst();

            myLinkedList.AddFirst(myTenListNode);
            myLinkedList.AddBefore(myTenListNode, 9);
            myLinkedList.AddAfter(myTenListNode, myFiveHundredListNode);
            myLinkedList.AddLast(myLast);
            myLinkedList.Remove(10);
            myLinkedList.AddAfter(myLast, 777);
            myLinkedList.Remove(myFiveHundredListNode);
            myLinkedList.RemoveFirst();
            myLinkedList.RemoveLast();
            myLinkedList.Clear();
            myLinkedList.AddLast(5);
            myLinkedList.RemoveFirst();
        }