コード例 #1
0
ファイル: Partition.cs プロジェクト: rachelbarnes/LinkedLists
        public DoubleNode <int> LinkedListForPartitionTests()
        {
            var a = new DoubleNode <int>(1);
            var b = new DoubleNode <int>(2);
            var c = new DoubleNode <int>(3);
            var d = new DoubleNode <int>(6);
            var e = new DoubleNode <int>(9);
            var f = new DoubleNode <int>(22);
            var g = new DoubleNode <int>(12);
            var h = new DoubleNode <int>(10);

            a.previous = null;
            a.next     = b;
            b.previous = a;
            b.next     = c;
            c.previous = b;
            c.next     = d;
            d.previous = c;
            d.next     = e;
            e.previous = d;
            e.next     = f;
            f.previous = e;
            f.next     = g;
            g.previous = f;
            g.next     = h;
            h.previous = g;
            h.next     = null;
            return(a);
        }
コード例 #2
0
ファイル: Partition.cs プロジェクト: rachelbarnes/LinkedLists
        public void PartitionTest1()
        {
            var part = new Partition();
            var a    = new DoubleNode <int>(1);
            var b    = new DoubleNode <int>(2);
            var c    = new DoubleNode <int>(3);
            var d    = new DoubleNode <int>(6);
            var e    = new DoubleNode <int>(22);
            var f    = new DoubleNode <int>(12);
            var g    = new DoubleNode <int>(10);
            var h    = new DoubleNode <int>(9);

            a.previous = null;
            a.next     = b;
            b.previous = a;
            b.next     = c;
            c.previous = b;
            c.next     = d;
            d.previous = c;
            d.next     = e;
            e.previous = d;
            e.next     = f;
            f.previous = e;
            f.next     = g;
            g.previous = f;
            g.next     = h;
            h.previous = g;
            h.next     = null;
            var expected = a;
            var actual   = part.PartitionDoublyLL(LinkedListForPartitionTests(), 9);

            Assert.AreEqual(expected, actual);
        }
コード例 #3
0
        public override bool Equals(Object obj)
        {
            if (obj == null || GetType() != obj.GetType())
            {
                return(false);
            }
            DoubleNode <T> p = (DoubleNode <T>)obj;

            return(p.ToString() == this.ToString());
        }
コード例 #4
0
        public void ReverseList()
        {
            var reverse  = new DoubleNode <char>();
            var expected = new List <char>()
            {
                'd', 'c', 'b', 'a'
            };
            var actual = reverse.ReverseList(SampleList());

            Assert.AreEqual(expected, actual);
        }
コード例 #5
0
        /*
         * Implement an algorithm to delete a node in the middle (i.e any node but the first and the and lsat node, not necessarily exact middle)
         *  of a singly linked list, given only access to that node.
         */

        public DoubleNode <char> DeleteMidNode(DoubleNode <char> n, char k)
        {
            var current = n;

            while (current != null)
            {
                if (current.value == k)
                {
                    current.previous.next = current.next;
                    current.next.previous = current.previous;
                }
                current = current.next;
            }
            return(n);
        }
コード例 #6
0
        public DoubleNode <char> ReturnNodes(DoubleNode <char> n, char k)
        {
            DoubleNode <char> alternateNode = null;
            DoubleNode <char> currentNode   = n;

            while (currentNode != null)
            {
                if (currentNode.value == k)
                {
                    alternateNode = currentNode;
                    return(alternateNode);
                }
                currentNode = currentNode.next;
            }
            return(alternateNode);
        }
コード例 #7
0
        public DoubleNode <char> sampleDoubleLinkedNode()
        {
            var a = new DoubleNode <char>('a');
            var b = new DoubleNode <char>('b');
            var c = new DoubleNode <char>('c');
            var d = new DoubleNode <char>('d');

            a.previous = null;
            a.next     = b;
            b.previous = a;
            b.next     = c;
            c.previous = b;
            c.next     = d;
            d.previous = c;
            d.next     = null;
            return(a);
        }
コード例 #8
0
        public DoubleNode <char> ReversedDoubleLinkedList()
        {
            var d = new DoubleNode <char>('d');
            var c = new DoubleNode <char>('c');
            var b = new DoubleNode <char>('b');
            var a = new DoubleNode <char>('a');

            d.previous = null;
            d.next     = c;
            c.previous = d;
            c.next     = b;
            b.previous = c;
            b.next     = a;
            a.previous = b;
            a.next     = null;
            return(d);
        }
コード例 #9
0
        public void ReturnPartOfALinkedListTest()
        {
            var remove = new ReturnKthToLast();
            var c      = new DoubleNode <char>('c');
            var d      = new DoubleNode <char>('d');
            var e      = new DoubleNode <char>('e');

            c.previous = null;
            c.next     = d;
            d.previous = c;
            d.next     = e;
            e.previous = d;
            e.next     = null;
            var expected = c;
            var actual   = remove.ReturnNodes(MyLinkedList(), 'c');

            Assert.AreEqual(expected, actual);
        }
コード例 #10
0
        public DoubleNode <char> MyLinkedList()
        {
            var a = new DoubleNode <char>('a');
            var b = new DoubleNode <char>('b');
            var c = new DoubleNode <char>('c');
            var d = new DoubleNode <char>('d');
            var e = new DoubleNode <char>('e');

            a.previous = null;
            a.next     = b;
            b.previous = a;
            b.next     = c;
            c.previous = b;
            c.next     = d;
            d.previous = c;
            d.next     = e;
            e.previous = d;
            e.next     = null;
            return(a);
        }
コード例 #11
0
        public void ToStringForDoublyLinkedNodes()
        {
            var a = new DoubleNode <char>('a');
            var b = new DoubleNode <char>('b');
            var c = new DoubleNode <char>('c');
            var d = new DoubleNode <char>('d');

            a.previous = null;
            a.next     = b;
            b.previous = a;
            b.next     = c;
            c.previous = b;
            c.next     = d;
            d.previous = c;
            d.next     = null;
            var expected = "a <-> b <-> c <-> d";
            var actual   = a.ToString();

            Assert.AreEqual(expected, actual);
        }
コード例 #12
0
        public void CountMethodForDoublyLinkedNodes()
        {
            var a = new DoubleNode <char>('a');
            var b = new DoubleNode <char>('b');
            var c = new DoubleNode <char>('c');
            var d = new DoubleNode <char>('d');

            a.previous = null;
            a.next     = b;
            b.previous = a;
            b.next     = c;
            c.previous = b;
            c.next     = d;
            d.previous = c;
            d.next     = null;
            var expected = 4;
            var actual   = a.Count();

            Assert.AreEqual(expected, actual);
        }
コード例 #13
0
        public DoubleNode <char> MyLinkedList()  //this is a hard coded linked list... i was trying to figure out how to delcare and set the original linked list
                                                 //and manipulate it in the same method...
        {
            var a = new DoubleNode <char>('a');
            var b = new DoubleNode <char>('b');
            var c = new DoubleNode <char>('c');
            var d = new DoubleNode <char>('d');
            var e = new DoubleNode <char>('e');

            a.previous = null;
            a.next     = b;
            b.previous = a;
            b.next     = c;
            c.previous = b;
            c.next     = d;
            d.previous = c;
            d.next     = e;
            e.previous = d;
            e.next     = null;
            return(a);
        }
コード例 #14
0
        public void DeleteMiddleNodeFromLinkedList()
        {
            var delete = new DeleteMiddleNode();
            var a      = new DoubleNode <char>('a');
            var b      = new DoubleNode <char>('b');
            var c      = new DoubleNode <char>('c');
            var d      = new DoubleNode <char>('d');
            var e      = new DoubleNode <char>('e');

            a.previous = null;
            a.next     = b;
            b.previous = a;
            b.next     = d;
            d.previous = b;
            d.next     = e;
            e.previous = d;
            e.next     = null;
            var expected = a;
            var actual   = delete.DeleteMidNode(MyLinkedList(), 'c');

            Assert.AreEqual(expected, actual);
        }
コード例 #15
0
ファイル: Partition.cs プロジェクト: rachelbarnes/LinkedLists
        /*
         * "Write code to partition a linked list around a value x, such that all nodes less than x come before all nodes greater than or equal to x.
         * If x is contained within the list, the values of x only need to be after the elements less than x (see below). The partition element x can appear anywhere in the
         * "right partition"; it does not need to appear between the left and right partitions."
         * Example: Input:  3 -> 5 -> 8 -> 5 -> 10 -> 2 -> 1 [partition 5]
         *       Output: 3 -> 1 -> 2 -> 10 -> 5 -> 5 -> 8
         *
         *  it seems that there are a few ways to do this:
         * sort the entire list and put the list in numerical order
         * put the partition value(s) (in case there's more than one instnace of the partition, like above) at the end of the  list
         *
         *  the easiest thing to do that I can see if to remove the partition nodes, make a note of how many partitions there are (for example, what if there are
         *      2 or 3 partitions, you still need all nodes accounted for (I'm thinking either a list and foreach through each element in the list, or have a count, and
         *          count-- and when the count is 1 or 0 and all the partition nodes have been accounted for and the appropriate nodes have been attached, return the linked list).
         *
         *      I'm guessing that in order to add the partition nodes at the end of the linked list, I need to link them the previous nodes, and thus needing a DoubleNode?
         *          try a singly linked list first
         *
         *
         *  I realized that this algorithm only accounts for one instance of the partition value in a linked list... that is something I have to account for when redoing this one
         */

        public DoubleNode <int> PartitionDoublyLL(DoubleNode <int> n, int partition)
        {
            var count         = 0;
            var partitionTail = new DoubleNode <int>();

            while (n.next != null)
            {
                if (n.value == partition)
                {
                    count++;
                    n.next.previous = n.previous;
                    n.previous.next = n.next;
                    n = n.next;
                }
                if (n.next != null)
                {
                    n = n.next;
                }
                if (n.next == null)
                {
                    //this is where I still have to mend some of my work, this only accounts for one of the nodes contianing the partition value, where there could be many
                    if (count == 1)
                    {
                        count--;
                        partitionTail.value = partition;
                        n.next             = partitionTail;
                        partitionTail.next = null;
                        while (n.previous != null)
                        {
                            partitionTail.previous = n;
                            n = n.previous;
                        }
                        return(n);
                    }
                }
            }
            return(n);
        }