最小方法,返回一个字符串以允许空消息。我有错误,并非所有代码路径返回值?
这种方法的想法是返回存储在链表中的最小值。如果链表是空的,则返回表示队列为空的消息。
旁注:我正在尝试实现一个带有列表的队列
public string Minimum()
{
string min = "The Queue is empty";
if (head == null)
{
return min;
}
SLLNode curr = head;
SLLNode prev = null;
int minimum = 0;
while (curr != null)
{
// have a match
if (curr.NodeValue <= minimum)
{
minimum = curr.NodeValue;
prev = curr;
curr = curr.Next;
min = minimum.ToString();
}
else
{
// advanced curr and prev
prev = curr;
curr = curr.Next;
}
return min;
}
}
答案 0 :(得分:2)
}
//return min;
}
return min;
}
答案 1 :(得分:2)
这是您的功能简化。您已将minimum
初始化为0
,而不是第一个节点的值或高值。您不必要地使用了两个指针,并在else
内复制了节点指针。您测试了<=
,但<
会测试。
public string Minimum()
{
string min = "The Queue is empty";
if (head == null)
{
return min;
}
SLLNode curr = head;
int minimum = curr.NodeValue;
while (curr != null)
{
if (curr.NodeValue < minimum)
minimum = curr.NodeValue;
curr = curr.Next;
}
min = minimum.ToString();
return min;
}