简体   繁体   中英

Adding in Sorted Linked List in C#

I have such a class for implementation of Sorted Linked List in C#. Now it's not actually sorted, but what changes do I have to make to this method it become one?

class LinkedSortedList<T>
    {
        public Node<T> Head { get; set; }
        public Node<T> Tail { get; set; }
        public int Count { get; set; }

        public LinkedSortedList()
        {
            Head = null;
            Tail = null;
            Count = 0;
        }

        public LinkedSortedList(T data)
        {
            CreateList(data);
        }

        public void AddElement(T data)
        {            
            if (Tail != null)
            {
                var node = new Node<T>(data);
                Tail.Next = node;
                Tail = node;
                Count++;
            }

            else
            {
                CreateList(data);
            }
        }

        public void CreateList(T data)
        {
            var node = new Node<T>(data);
            Head = node;
            Tail = node;
            Count = 1;
        }

I want to modify AddElement function so that the list is and remains sorted. How can I implement this logic?

A key observation you need to make in order to complete the task is that at the beginning of AddElement the list is either empty, or sorted. If the list is empty, your task is trivial; if the list is sorted, you must pick an insertion point for the element being added, and insert the new element there. The list will remain sorted after the insertion, because no other elements would need to be moved.

To find the insertion point, start walking the list from the head until you either (1) find an element that is greater than the one being inserted, or (2) you reach the end of the list. In both cases you simply insert the new element immediately after the last element that you've passed during the traversal, or at the head if the initial element is greater than the one you are inserting.

Currently you're just adding elements to the end of the list. Instead, we want to walk the list and look at each value until we find the correct place to insert the node.

One way to do this is to do the following checks:

  1. Is the Head null? If so, create a new list with this data and we're done.
  2. Is the new node's data less than the Head data? If so, put this node in front of the Head and point the Head to our new node.
  3. Is the new node's data greater than the Tail data? If so, put this node after the Tail and point the Tail to our new node.
  4. Otherwise, create a "current" node that points to the head, compare the data of the current node with our new node, and continue to move through the list by setting "current" to it's Next property until we find the proper place to insert the new node. Then rearrange the Next and Previous properties of the three nodes (the new node, the one before it, and the one after it).

For example:

public void AddElement(T data)
{            
    if (Head == null)
    {
       CreateList(data);
    }
    else
    {
        var node = new Node<T>(data);

        if (node.Data < Head.Data)
        {
            Head.Previous = node;
            node.Next = Head;
            Head = node;
        }
        else if (node.Data > Tail.Data)
        {
            Tail.Next = node;
            node.Previous = Tail;
            Tail = node;
        }
        else
        {
            var current = Head;
   
            while(node.Data >= current.Data && current.Next != null)
            {
                current = current.Next;
            }

           node.Previous = current.Previous;
           if (node.Previous != null) node.Previous.Next = node;
           current.Previous = node;
           node.Next = current;
           if (Head == current) Head = node;
           Count++;
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM