可排序的对象链表

时间:2011-10-01 22:24:01

标签: c# visual-studio-2010 sorting linked-list

对于学校实验室,我必须制作一个链接的消息列表,然后按优先级排序这些消息,首先拉出“高”优先级,然后是中等,然后是低。我一直试图解决这个问题好几天,我不能把我的想法包围起来。我一直试图让它排序而不添加除了我的ListofMessages类中的头和大小字段以外的任何东西,但我似乎只是添加垃圾代码。我想自己解决这个问题,但现在我很难过。

这是我到目前为止所拥有的。希望你能理解它:

class ListOfMessages
{
    private int m_nSize;
    public Message m_cListStart;
    //public Message m_cNextItem;
    public Message m_cLastItem;

    public ListOfMessages()
    {
        m_nSize = 0;
        m_cListStart = null;
        //m_cNextItem = null;
    }

    public int Count
    {
        get { return m_nSize; }
    }       

    public string Display(Message cMessage)
    {
        return "Message: " + cMessage.m_strMessage + "\nPriority: " + cMessage.m_strPriority;
    }

    //list additions
    public int Add(Message newMessage)
    {
        Message nextMessage = new Message();
        //inserts objects at the end of the list
        if (m_nSize == 0)
        {
            m_cListStart = newMessage;
                //m_cLastItem = newMessage;
        }
        else
        {
            Message CurrentMessage = m_cListStart;

            if (newMessage.m_strPriority == "High")
            {

                    if (m_cListStart.m_strPriority != "High")
                    {
                        //Make the object at the start of the list point to itself
                        CurrentMessage.m_cNext = m_cListStart;
                        //Replace the object at the start of the list with the new message
                        m_cListStart = newMessage;

                    }
                else
                {
                    Message LastMessage = null;

                    for (int iii = 0; iii < m_nSize; iii++)//(newmessage.m_strpriority == iii.m_strpriority)
                    //&& (iii.m_cnext == null);)
                    {
                        if (m_cListStart.m_strPriority != "High")
                        {
                            nextMessage = newMessage;
                            nextMessage.m_cNext =
                            CurrentMessage = nextMessage;
                            //LastMessage.m_cNext = CurrentMessage;
                        }
                        LastMessage = CurrentMessage;

                        if (m_cListStart.m_cNext != null)
                            m_cListStart = m_cListStart.m_cNext;
                    }
                }
                //iii = iii.m_cnext;
            }
                    // for (int iii = m_cListStart; ; iii = iii.m_cNext)//(newMessage.m_strPriority == iii.m_strPriority)
                    //    //&& (iii.m_cNext == null);)
                    //{
                    //    //Message lastMessage = iii;
                    //    if (iii.m_strPriority != iii.m_strPriority)
                    //    {
                    //        //iii.m_cNext = newMessage;
                    //        newMessage.m_cNext = iii.m_cNext;
                    //        iii.m_cNext = newMessage;
                    //    }


                    //m_cLastItem.m_cNext = newMessage;
        }
            //m_cLastItem = newMessage;
            return m_nSize++;
    }

    public Message Pop()
    {
        //Message Current = m_cListStart;
        //if the object at the start of the list has another object after it, make that object the start of the list
        //and decrease the size by 1 after popping an object off if there is more than 1 object after the start of the list
        if (m_cListStart.m_cNext != null)
        {
            m_cListStart = m_cListStart.m_cNext;
        }
        if (m_nSize > 0)
            m_nSize--;
        else
            m_cListStart = null;
        return m_cListStart;
        //if (m_cListStart.m_cNext != null)
        //    m_cListStart = m_cListStart.m_cNext;
        //if (m_nSize > 1)
        //    m_nSize--;
        //return m_cListStart;
    }

我检索消息的pop函数可能需要一些改进,但现在大部分工作都在Add函数中。我真的只是在那里黯然失色。

有没有人知道我正在问的一种简单方法?

2 个答案:

答案 0 :(得分:1)

为什么不按如下方式编写自定义链表:

class Node<T> : IComparable<T>
{
   public int Priority {set;get;}
   public T Data {set;get;}
   public Node<T> Next {set;get;}
   public Node<T> Previous {set;get;}

   // you need to implement IComparable here for sorting.
}

这是您的节点定义。现在我们需要实现一个LinkedList类。

您的链表类可以是双向链表,因为您没有任何规格。使用双向链表会更容易。

这是定义:

class LinkedList<T> : IEnumerable<T> where T: IComparable
{
    public Node<T> Head {set;get;}
    public Node<T> Tail {set;get;}

    // set of constructors
    //.....

    public void Insert(Node<T> node)
    {
       // you can do recursive or iterative impl. very easy.
    }

    // other public methods such as remove, insertAfter, insert before, insert last etc.

    public void Sort()
    {
       // easiest solution is to use insertion sort based on priority.
    }

}

如果你可以通过创建额外的内存来逃避,即:另一个链表。插入排序没问题。为此,您还需要实现插入功能。

我有LinkedList实施,您可以查看。您只需要根据优先级实现排序,您可以使用冒泡排序,插入排序或合并排序。

此外,您可能希望查看可用于实现优先级队列的堆,它可用于此目的。我有一个Heap Data Structure实施,您可以查看。

答案 1 :(得分:1)

最简单的解决方案是拥有三个单链接列表,每个优先级都有一个。

添加时,您将添加到正确列表的末尾。删除时,首先尝试从优先级最高的列表中删除。如果是空的,请尝试中间的。如果即使是空的,请使用最低列表。

如果你有多个优先级,那么两种情况下的时间复杂度都是O(1)。

相关问题