【发布时间】:2011-10-01 22:24:01
【问题描述】:
对于学校实验室,我必须制作一个消息链接列表,然后按优先级对这些消息进行排序,首先取出“高”优先级,然后是中优先级,然后是低优先级。几天来我一直试图弄清楚这一点,但我无法将注意力集中在排序上。我一直试图让它排序,而不是在我的 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 函数中。我真的只是在黑暗中跌跌撞撞。
有谁知道按照我的要求做的简单方法?
【问题讨论】:
-
我把我的乱码扔给你。
-
你会被那个代码折磨自己,首先尝试了解链表的工作原理并查看一些代码示例。
-
您应该考虑的另一件事是列表是否主要用于插入元素,然后是单个排序,然后是删除元素。或者如果插入和删除将混合使用。如果它会被混合,您可以通过按其排序顺序插入元素来维护排序的列表。由于您的列表将始终进行排序,因此还可以使您不必执行排序。
-
为什么每个人都试图避免排序?排序有什么问题?你只是想破解它并让它工作吗?让他学习。
-
@user177883,排序(相对)慢。如果时间复杂度很重要(在这里似乎很重要)并且您可以在线性时间内解决某些问题,那么该解决方案比排序要好得多。
标签: c# visual-studio-2010 sorting linked-list