Example #1
0
        public bool IsLower(LinkedNode <T> nodeToCompare)
        {
            if (typeof(T) == typeof(int))
            {
                return(Convert.ToInt32(data) < Convert.ToInt32(nodeToCompare.data));
            }

            return(false);
        }
Example #2
0
        public void AppendToEnd(LinkedNode <T> nodeToApped)
        {
            LinkedNode <T> currentNode = this;

            while (currentNode.nextNode != null)
            {
                currentNode = nextNode;
            }

            currentNode.nextNode = nodeToApped;
        }
Example #3
0
        /// <summary>
        /// Remove the first item in the list
        /// </summary>
        /// <returns>The remove.</returns>
        public T Remove()
        {
            var data = first.data;

            first = first.nextNode;
            if (first == null)
            {
                last = null;
            }

            return(data);
        }
Example #4
0
        public void PrintValues()
        {
            Console.WriteLine("Writing values in Linked Node");
            LinkedNode <T> currentNode = this;

            do
            {
                Console.WriteLine(currentNode.data);
                currentNode = currentNode.nextNode;
            }while (currentNode != null);

            Console.WriteLine("DONE writing values for Linked Node");
        }
Example #5
0
        public static LinkedNode <T> CreateNodesFromList(List <T> input)
        {
            Console.WriteLine($"Creating Linked node from {input.ToString()}");
            LinkedNode <T> headNode    = new LinkedNode <T>(input[0]);
            LinkedNode <T> currentNode = headNode;

            for (int i = 1; i < input.Count; i++)
            {
                var newNode = new LinkedNode <T>(input[i]);
                currentNode.nextNode = newNode;
                currentNode          = currentNode.nextNode;
            }

            return(headNode);
        }
Example #6
0
        //public static LinkedNode<T> GetNodeXPositionsFromCurrent(LinkedNode<T> head, int positionsToAdvance)
        //{
        //    var result = head;
        //    int currentPosition = 0;

        //    while(result.nextNode != null && currentPosition < positionsToAdvance)
        //    {
        //        result = result.nextNode;
        //        currentPosition++;
        //    }

        //    return result;
        //}

        public static LinkedNode <char> CreateNodeFromString(string input)
        {
            Console.WriteLine($"Creating Linked node from {input}");
            var headNode    = new LinkedNode <char>(input[0]);
            var currentNode = headNode;

            for (int i = 1; i < input.Length; i++)
            {
                var newNode = new LinkedNode <char>(input[i]);
                currentNode.nextNode = newNode;
                currentNode          = newNode;
            }

            return(headNode);
        }
Example #7
0
        public LinkedNode <T> GetFirstNodeWithValue(T value)
        {
            LinkedNode <T> currenNode = this;

            while (currenNode != null)
            {
                if (currenNode.data.Equals(value))
                {
                    return(currenNode);
                }

                currenNode = currenNode.nextNode;
            }

            Console.WriteLine($"Did not find {value}. Returning.");
            return(this);
        }
Example #8
0
        public static LinkedNode <T> GetFirstNodeWithValue(LinkedNode <T> head, T value)
        {
            LinkedNode <T> currenNode = head;

            while (currenNode != null)
            {
                if (currenNode.data.Equals(value))
                {
                    return(currenNode);
                }

                currenNode = currenNode.nextNode;
            }

            Console.WriteLine($"Did not find {value}. Returning original node");
            return(head);
        }
Example #9
0
        /// <summary>
        /// Add item to the end of the list (enqueue)
        /// </summary>
        /// <param name="data">Data.</param>
        public void Add(T data)
        {
            //var newNode = new LinkedNode<T>(data);
            //newNode.nextNode = last;
            //last = newNode;

            var newNode = new LinkedNode <T>(data);

            if (last != null)
            {
                last.nextNode = newNode;
            }
            last = newNode;

            if (first == null)
            {
                first = last;
            }
        }
Example #10
0
        public static LinkedNode <T> DeleteNodeWithValue(LinkedNode <T> head, T valueToDelete)
        {
            LinkedNode <T> currenNode = head;

            //move head node
            if (currenNode.data.Equals(valueToDelete))
            {
                return(currenNode.nextNode);
            }

            while (currenNode.nextNode != null)
            {
                if (currenNode.nextNode.data.Equals(valueToDelete))
                {
                    currenNode.nextNode = currenNode.nextNode.nextNode;
                    return(head);
                }
                currenNode = currenNode.nextNode;
            }

            return(head);
        }
Example #11
0
 public MyStack(T data)
 {
     Top = new LinkedNode <T>(data);
 }