コード例 #1
0
ファイル: Program.cs プロジェクト: aracen74/RefreshMyMemory
        public static void WhatHappens_ToNodeNextsReferences_WhenYouSetTailToNull()
        {
            var linkedList = new LinkedList<int>();

            linkedList.AddToFront(new LinkedListNode<int>(3));
            linkedList.AddToFront(new LinkedListNode<int>(5));
            linkedList.AddToFront(new LinkedListNode<int>(7));

            var node = linkedList.Head;
            while(node != linkedList.Tail)
            {
                node = node.Next;
                var result = object.ReferenceEquals(node, linkedList.Tail);

                if (result)
                {
                    node.Value = 14;
                    result = object.ReferenceEquals(node, linkedList.Tail);

                    node = null; // will this delete it? No you can change properties but you can't delete another reference type by setting another reference type to null
                    result = object.ReferenceEquals(node, linkedList.Tail);
                }
            }

            

            linkedList.RemoveLast();
        }
コード例 #2
0
 public static float[] BucketSort(float[] input)
 {
     LinkedList<float>[] lists = new LinkedList<float>[input.Length];
     for (int i = 0; i < input.Length; i++)
     {
         lists[i] = new LinkedList<float>();
     }
     for (int i = 0; i < input.Length; i++)
     {
         lists[(int)(input.Length * input[i])].AddFirst(input[i]);
     }
     for (int i = 0; i < input.Length; i++)
     {
         float[] sorted = InsertionSortClass.InsertionSort(lists[i].ToArray());
         lists[i] = new LinkedList<float>(sorted);
     }
     float[] result = new float[input.Length];
     int index = 0;
     for (int i = 0; i < input.Length; i++)
     {
         lists[i].CopyTo(result, index);
         index += lists[i].Count;
     }
     return result;
 }
コード例 #3
0
ファイル: LinkedList.cs プロジェクト: baio/algos
        static void removeDups2(LinkedList head) {
            LinkedList prev = null;
            LinkedList n = head;            
            var list = new List<int>();

            while (n != null)
            {
                LinkedList k = head;

                while (k != n) {

                    if (n.data == k.data) {

                        prev.next = n.next;

                        break;

                    }

                    k = k.next;
                }

                if (k == n) {
                    prev = n;
                }

                n = n.next;
            }
        }
コード例 #4
0
ファイル: SortingClass.cs プロジェクト: neolee11/Algorithm
        public static void BucketSort(int[] array)
        {
            int numOfBuckets = 5;
            LinkedList[] buckets = new LinkedList[numOfBuckets];
            for (int i = 0; i < numOfBuckets; i++)
                buckets[i] = new LinkedList();

            int n = array.Length;

            for (int i = 0; i < n; i++)
            {
                int bucketIdx = array[i] / 10;
                buckets[bucketIdx].Insert(array[i]);
            }

            for (int i = 0; i < numOfBuckets; i++)
                buckets[i].Sort();

            int next = 0;
            for (int i = 0; i < numOfBuckets; i++)
            {
                ListNode curr = buckets[i].Head;

                while (curr != null)
                {
                    array[next] = curr.Value;
                    curr = curr.Next;
                    next++;
                }
            }
        }
コード例 #5
0
ファイル: LinkedList.cs プロジェクト: baio/algos
 void appendToTail(int data) {
     LinkedList node = new LinkedList(data);
     this.next = node;
     LinkedList n = this;
     while (n.next != null) {
         n = n.next;
     }
     n.next = node;
 }
コード例 #6
0
ファイル: LinkedList.cs プロジェクト: baio/algos
        static LinkedList delete(LinkedList head, int data) {
            if (head.data == data) {
                return head.next;
            }
            LinkedList n = head;
            while (n.next != null)
            {
                if (n.next.data == data) {
                    n.next = n.next.next;
                    return head;
                }

                n = n.next;
            }
            return head;
        }
コード例 #7
0
ファイル: LinkedList.cs プロジェクト: baio/algos
        // 2.1 Write code to remove duplicates from an unsorted linked list.
        // How would you solve this problem if a temporary buffer is not allowed?

        static void removeDups(LinkedList head)
        {
            LinkedList prev = null;
            LinkedList n = head;
            var list = new List<int>();
            
            while (n != null){
                if (list.Exists(i => i == n.data))
                {
                    prev.next = n.next;
                }
                else {
                    prev = n;
                    list.Add(n.data);
                }
                n = n.next;
            }
        }
コード例 #8
0
ファイル: LinkedListForm.cs プロジェクト: neolee11/Algorithm
        public LinkedListForm()
        {
            InitializeComponent();

            mLinkedList = new LinkedList();
        }
コード例 #9
0
ファイル: LinkedList.cs プロジェクト: baio/algos
        // 2.2 Implement an algorithm to find the nth to last element of a singly linked list.

        static LinkedList nthToLast(LinkedList root, int idx)
        {
            LinkedList n = root;
            for (int i = 0; i < idx; i++)
            {
                if (n.next == null) {
                    return null;
                }

                n = n.next;
            }

            return n;
        }
コード例 #10
0
ファイル: LinkedList.cs プロジェクト: baio/algos
        /*
            2.5 Given a circular linked list, implement an algorithm which returns node at the beginning of the loop.
            DEFINITION
            Circular linked list: A (corrupt) linked list in which a node’s next pointer points to an earlier node, so as to make a loop in the linked list.
            EXAMPLE
            input: A -> B -> C -> D -> E -> C [the same C as earlier]
            output: C
        */

        static LinkedList findCirc(LinkedList head)
        {
            var n = head;

            while (n != null) {

                LinkedList k = n.next;

                while (k != null)
                {
                    //wrong

                    if (k == n)
                    {
                        return k;
                    }

                    k = k.next;
                }

                n = n.next;
            }

            return null;
        }
コード例 #11
0
ファイル: LinkedList.cs プロジェクト: baio/algos
        /*
            2.4 You have two numbers represented by a linked list, 
            where each node contains a single digit. 
            The digits are stored in reverse order, such that the 1’s digit is at the head of the list. Write a function that adds the two numbers and returns the sum as a linked list.
            EXAMPLE
            Input: (3 -> 1 -> 5) + (5 -> 9 -> 2)
            Output: 8 -> 0 -> 8
        */

        static LinkedList sum(LinkedList node1, LinkedList node2, int carry = 0)
        {
            if (node1 == null && node2 == null) {
                throw new ArgumentException("Nodes can't be null");
            }

            int value = carry;

            value += node1.data + node2.data;

            var res = new LinkedList(value % 10);

            res.next = sum(node1.next, node2.next, value >= 10 ? 1 : 0);

            return res;
        }
コード例 #12
0
ファイル: LinkedList.cs プロジェクト: baio/algos
        /*
        2.3 Implement an algorithm to delete a node in the middle of a single linked list, given only access to that node.
        EXAMPLE
        Input: the node ‘c’ from the linked list a->b->c->d->e
        Result: nothing is returned, but the new linked list looks like a->b->d->e
        */

        static void deleteNode(LinkedList node)
        {
            if (node.next != null)
            {
                node.data = node.next.data;
                node.next = node.next.next;
            }
            else {
                throw new Exception("Can't remove last node");
            }

        }
コード例 #13
0
        /// <summary>
        /// Creates a double dense vector based on a string. The string can be in the following formats (without the
        /// quotes): 'n', 'n,n,..', '(n,n,..)', '[n,n,...]', where n is a double.
        /// </summary>
        /// <returns>
        /// A double dense vector containing the values specified by the given string.
        /// </returns>
        /// <param name="value">
        /// the string to parse.
        /// </param>
        /// <param name="formatProvider">
        /// An <see cref="IFormatProvider"/> that supplies culture-specific formatting information.
        /// </param>
        public static DenseVector Parse(string value, IFormatProvider formatProvider)
        {
            if (value == null)
            {
                throw new ArgumentNullException(value);
            }

            value = value.Trim();
            if (value.Length == 0)
            {
                throw new FormatException();
            }

            // strip out parens
            if (value.StartsWith("(", StringComparison.Ordinal))
            {
                if (!value.EndsWith(")", StringComparison.Ordinal))
                {
                    throw new FormatException();
                }

                value = value.Substring(1, value.Length - 2).Trim();
            }

            if (value.StartsWith("[", StringComparison.Ordinal))
            {
                if (!value.EndsWith("]", StringComparison.Ordinal))
                {
                    throw new FormatException();
                }

                value = value.Substring(1, value.Length - 2).Trim();
            }

            // keywords
            var textInfo = formatProvider.GetTextInfo();
            var keywords = new[] { textInfo.ListSeparator };

            // lexing
            var tokens = new LinkedList<string>();
            GlobalizationHelper.Tokenize(tokens.AddFirst(value), keywords, 0);
            var token = tokens.First;

            if (token == null || tokens.Count.IsEven())
            {
                throw new FormatException();
            }

            // parsing
            var data = new double[(tokens.Count + 1) >> 1];
            for (int i = 0; i < data.Length; i++)
            {
                if (token == null || token.Value == textInfo.ListSeparator)
                {
                    throw new FormatException();
                }

                data[i] = Double.Parse(token.Value, NumberStyles.Any, formatProvider);

                token = token.Next;
                if (token != null)
                {
                    token = token.Next;
                }
            }

            return new DenseVector(data);
        }
コード例 #14
0
ファイル: SlaeSolver.cs プロジェクト: fanatt20/SA_lab2
 private static LinkedList<double[]> GetRandomVectors(double[] x)
 {
     var result = new LinkedList<double[]>();
     var step = GetStep();
     for (var i = 0; i < M; i++)
     {
         var vector = new double[x.Length];
         for (var j = 0; j < x.Length; j++)
             vector[j] = GetPseudoRandomValue();
         var norma = GetVectorNorma(vector);
         for (var k = 0; k < x.Length; k++)
             vector[k] = x[k] + vector[k] * step / norma;
         result.AddLast(vector);
     }
     return result;
 }