Insert In Order Link List issue

j0rd4nn

New member
Joined
Dec 18, 2013
Messages
4
Programming Experience
5-10
I cannot get my head around this. I have created a generic Link List class, and Link class and I'm now trying to create a function within the LinkList class that will insert the inputted variable into the correct spot within the linklist. Here is my code so far:

C#:
        public void InsertInOrder(T item)
        {
            // is list in non-descending 

            LinkGen<T> temp = list;
            if (temp == null)
            {
                AddItem(item);
            }
            else
            {
                while (item.CompareTo(temp.Data) > 0)
                {
                    temp = temp.Next;
                }
                list = new LinkGen<T>(item, list);
            }
        }
The code has looked quite different, quite a few times. I've tried endless possibilities. I just cannot insert 'item' in the correct spot within the linklist :(

Any help is greatlyyyyyyyy appreciated!! :drunk:
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,141
Location
Sydney, Australia
Programming Experience
10+
Firstly, do you actually mean "linked list", which is a list of linked items, rather than a "link list", which implies a list of links, i.e. the links are the items? If so then may I ask why you are creating your own class rather than using the System.Collections.Generic.LinkedList<T> class? Here's an example of adding items in ascending order using that class:
using System;
using System.Collections.Generic;

namespace ConsoleApplication1
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            var list = new LinkedList<int>();

            foreach (var n in new[] {3, 6, 1, 7, 4, 2, 9, 8, 5, 7, 4})
            {
                InsertItem(list, n);
            }

            foreach (var n in list)
            {
                Console.WriteLine(n);
            }

            Console.ReadLine();
        }

        private static void InsertItem<T>(LinkedList<T> list, T item) where T : IComparable
        {
            if (list.Count == 0)
            {
                // The new item is the first item.
                list.AddFirst(item);
            }
            else if (item.CompareTo(list.Last.Value) >= 0)
            {
                // The new item is not less than any existing items.
                list.AddLast(item);
            }
            else
            {
                var node = list.First;

                // Traverse the list.
                while (true)
                {
                    if (item.CompareTo(node.Value) < 0)
                    {
                        // This is the first item that the new item is less than.
                        list.AddBefore(node, item);

                        break;
                    }

                    node = node.Next;
                }
            }
        }
    }
}
 
Top Bottom