public static SingleLinkedListNode <int> CreateSinglyLinkedList(int length, int startData, int endData)
        {
            // Error checking
            if (length <= 0)
            {
                return(null);
            }
            SingleLinkedListNode <int> head        = null;
            SingleLinkedListNode <int> currentNode = null;
            Random rnd = new Random();

            while (length > 0)
            {
                if (head == null)
                {
                    head        = new SingleLinkedListNode <int>(rnd.Next(startData, endData));
                    currentNode = head;
                }
                else
                {
                    currentNode.NextNode = new SingleLinkedListNode <int>(rnd.Next(startData, endData));
                    currentNode          = currentNode.NextNode;
                }

                length--;
            }

            return(head);
        }
Esempio n. 2
0
        public void AppendNext(T data)
        {
            SingleLinkedListNode <T> nextNode  = this.NextNode;
            SingleLinkedListNode <T> nodeToAdd = new SingleLinkedListNode <T>(data);

            nodeToAdd.NextNode = nextNode;
            this.NextNode      = nodeToAdd;
        }
 public static void PrintSinglyLinkedList(SingleLinkedListNode <int> head)
 {
     while (head != null)
     {
         Console.Write(head.Data + " -> ");
         head = head.NextNode;
     }
     Console.Write("null");
     Console.WriteLine();
 }
Esempio n. 4
0
        /// <summary>
        /// Appends a node to the end of the singly linked list
        /// This will take O(n) time
        /// </summary>
        /// <param name="data">data for the node to be appended</param>
        public void AppendToEnd(T data)
        {
            SingleLinkedListNode <T> endNode     = new SingleLinkedListNode <T>(data);
            SingleLinkedListNode <T> currentNode = this;

            while (currentNode.NextNode != null)
            {
                currentNode = currentNode.NextNode;
            }
            currentNode.NextNode = endNode;
        }
 public void AppendToEnd(T data)
 {
     if (Head == null)
     {
         Head        = new SingleLinkedListNode <T>(data);
         CurrentNode = Head;
     }
     else
     {
         CurrentNode.NextNode = new SingleLinkedListNode <T>(data);
         CurrentNode          = CurrentNode.NextNode;
     }
 }
        public static SingleLinkedListNode <int> GetRandomNode(SingleLinkedListNode <int> head, int linkedListLength)
        {
            Random rnd   = new Random();
            int    index = rnd.Next(0, linkedListLength - 1);
            SingleLinkedListNode <int> randomNode = head;

            while (index != 0)
            {
                index--;
                randomNode = randomNode.NextNode;
            }
            return(randomNode);
        }
        public static SingleLinkedListNode <int> SortedLinkedList(int linkedlistLength, int offset = 0)
        {
            SingleLinkedListNode <int> head        = null;
            SingleLinkedListNode <int> currentNode = null;

            for (int i = 0; i < linkedlistLength; i++)
            {
                if (head == null)
                {
                    head        = new SingleLinkedListNode <int>(i + offset);
                    currentNode = head;
                }
                else
                {
                    currentNode.NextNode = new SingleLinkedListNode <int>(i + offset);
                    currentNode          = currentNode.NextNode;
                }
            }
            return(head);
        }
        public static SingleLinkedListNode <int> CreateSinglyLinkedList(int[] arr)
        {
            SingleLinkedListNode <int> head    = null;
            SingleLinkedListNode <int> current = null;

            for (int i = 0; i < arr.Length; i++)
            {
                if (head == null)
                {
                    head    = new SingleLinkedListNode <int>(arr[i]);
                    current = head;
                }
                else
                {
                    current.NextNode = new SingleLinkedListNode <int>(arr[i]);
                    current          = current.NextNode;
                }
            }
            return(head);
        }