/// <summary>
        /// Adds an item to the queue in priority order
        /// </summary>
        /// <param name="item">The item to place in the queue</param>
        public void Enqueue(T item)
        {
            // if the list is empty, just add the item
            if (_items.Count == 0)
            {
                _items.AddLast(item);
            }
            else
            {
                // find the proper insert point
                var current = _items.First;

                // while we're not at the end of the list and the current value
                // is larger than the value being inserted...
                while (current != null && current.Value.CompareTo(item) > 0)
                {
                    current = current.Next;
                }

                if (current == null)
                {
                    // we made it to the end of the list
                    _items.AddLast(item);
                }
                else
                {
                    // the current item is <= the one being added
                    // so add the item before it.
                    _items.AddBefore(current, item);
                }
            }
        }
        /** Inserts a new key <Key> with value 1. Or increments an existing key by 1. */
        public void Inc(string key)
        {
            if (_dict.ContainsKey(key))
            {
                var ln = _dict[key];
                ln.Value.value++;
                if (ln.Previous != null && ln.Previous.Value.value < ln.Value.value)
                {
                    var p = ln.Previous;
                    _list.Remove(ln);
                    while (p.Previous != null && p.Previous.Value.value < ln.Value.value)
                    {
                        p = p.Previous;
                    }

                    _list.AddBefore(p, ln);
                }
            }
            else
            {
                _dict[key] = _list.AddLast(new Node()
                {
                    key = key, value = 1
                });
            }
        }
Exemple #3
0
        public void Enqueue(T item)
        {
            if (_items.Count == 0)
            {
                _items.AddLast(item);
            }
            else
            {
                var current = _items.First;

                while (current != null && current.Value.CompareTo(item) > 0)
                {
                    current = current.Next;
                }

                if (current == null)
                {
                    _items.AddLast(item);
                }
                else
                {
                    _items.AddBefore(current, item);
                }
            }
        }
Exemple #4
0
            public void Add(int n, T t)
            {
                if (n == 0)
                {
                    if (Node != null)
                    {
                        Node.Value = t;
                    }
                    return;
                }

                Enumerator itr = new Enumerator(this);

                if (!itr.Move(n - Math.Sign(n)))
                {
                    if (begin)
                    {
                        List.AddFirst(t);
                    }
                    else
                    {
                        List.AddLast(t);
                    }

                    /*if (!itr.Move(-Math.Sign(n)))
                     * {
                     *  itr.Move(Math.Sign(n));
                     *  n *= -1;
                     * }*/
                }
                else
                {
                    if (n > 0)
                    {
                        List.AddAfter(itr.Node, t);
                    }
                    else if (n < 0)
                    {
                        List.AddBefore(itr.Node, t);
                    }
                }
            }
 public LinkedListNode <T> AddBefore(LinkedListNode <T> node, T value) => linkedList.AddBefore(node, value);
        static void Main(string[] args)
        {
            // this is the default doubleLinkedList
            var lc = new  System.Collections.Generic.LinkedList <int>();

            // adding data

            for (int i = 0; i <= 10; i++)
            {
                lc.AddLast(i);
            }

            var data = lc.First.Next.Next;


            lc.AddAfter(data, 100);
            foreach (var i in lc)
            {
                Console.WriteLine(i);
            }

            // because this LinkedList is  double link List
            // we can add before too
            lc.AddBefore(data, 100);
            foreach (var i in lc)
            {
                Console.WriteLine(i);
            }

            // this will return a bool

            Console.WriteLine(lc.Contains(100));


            LinkedList <String> my_list = new LinkedList <String>();

            // Adding elements in the LinkedList
            // Using AddLast() method
            my_list.AddLast("Zoya");
            my_list.AddLast("Shilpa");
            my_list.AddLast("Rohit");
            my_list.AddLast("Rohan");
            my_list.AddLast("Juhi");
            my_list.AddLast("Zoya");

            // Inital number of elements
            Console.WriteLine("Best students of XYZ " +
                              "university initially:");

            // Accessing the elements of
            // Linkedlist Using foreach loop
            foreach (string str in my_list)
            {
                Console.WriteLine(str);
            }

            // After using Remove(LinkedListNode)
            // method
            Console.WriteLine("Best students of XYZ" +
                              " university in 2000:");

            my_list.Remove(my_list.First);

            foreach (string str in my_list)
            {
                Console.WriteLine(str);
            }

            // After using Remove(T) method
            Console.WriteLine("Best students of XYZ" +
                              " university in 2001:");

            my_list.Remove("Rohit");

            foreach (string str in my_list)
            {
                Console.WriteLine(str);
            }

            // After using RemoveFirst() method
            Console.WriteLine("Best students of XYZ" +
                              " university in 2002:");

            my_list.RemoveFirst();

            foreach (string str in my_list)
            {
                Console.WriteLine(str);
            }

            // After using RemoveLast() method
            Console.WriteLine("Best students of XYZ" +
                              " university in 2003:");

            my_list.RemoveLast();

            foreach (string str in my_list)
            {
                Console.WriteLine(str);
            }

            // After using Clear() method
            my_list.Clear();
            Console.WriteLine("Number of students: {0}",
                              my_list.Count);
        }