我出的一道面试题,95%的人写不出,大家看看是否太难
不得不说,招人很难,我接手公司招聘工作一年多了,但几乎招不到人,有4/5的人对面试邀请爽约,而剩下的1/5的人中又大多数发觉很不合适,好不容易觉得合适,最后还不来。我为了面试最初一共出了6道题目,后来感觉有点多,删掉了4道,后来还是觉得有点多,又删掉了一道,只剩这道题,大家别笑,在开始做题前我都在应聘者旁边图文声色俱全耐心讲解了如何解答:什么是链表?如何插入?如何删除?笔试者其实只需要按照我说的写一点代码即可,可事实上真正能完成这道题目的人我只遇到一个,大多数都交了白卷(时间大约20分钟),好吧,我把这道题目原原本本贴在这里,一个字不变:
----题目开始----
实现一个链表,要求各个节点的Name都不重复,若试图增加重复节点,就抛出异常,填写下面的Append方法和Remove方法的代码。
class Node
{
public string Name{get;set;}
public int Score{get;set;}
public Node Next{get;set;}
}
class Link
{
public Node Head {
get{return m_nodeHead;}
}
public void Append(string strName, int iScore)
{
//Add a node: implement your code here
}
public void Remove(string strName)
{
//Delete a node: implement your code here
}
private Node m_nodeHead = new Node(){
Name="_HeadNode",
Score=0,
Next=null};
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace NodeTest
{
class Program
{
static void Main(string[] args)
{
Link link = new Link();
link.Append("1", 1);
link.Append("2", 2);
link.Append("3", 3);
link.Print();
Console.WriteLine();
link.Remove("2");
link.Print();
Console.ReadKey();
}
}
class Node
{
public string Name { get; set; }
public int Score { get; set; }
public Node Next { get; set; }
}
class Link
{
public Node Head
{
get { return m_nodeHead; }
}
public void Append(string strName, int iScore)
{
//Add a node: implement your code here
Node _node = m_nodeHead;
Node _preNode = null;
while (_node != null && _node.Name != strName)
{
_preNode = _node;
_node = _node.Next;
}
if (_node != null)
{
throw new Exception(strName + "已存在");
}
else
{
_node = new Node { Name = strName, Score = iScore, Next = null };
_preNode.Next = _node;
}
}
public void Remove(string strName)
{
//Delete a node: implement your code here
Node _delNode = m_nodeHead;
Node _preNode = null;
while (_delNode != null && _delNode.Name != strName)
{
_preNode = _delNode;
_delNode = _delNode.Next;
}
if (_delNode != null)
{
_preNode.Next = _delNode.Next;
}
}
public void Print()
{
Node _node = m_nodeHead;
while (_node != null)
{
Console.Write(_node.Name + " -> ");
_node = _node.Next;
}
}
private Node m_nodeHead = new Node()
{
Name = "_HeadNode",
Score = 0,
Next = null
};
}
}
public void Append(string strName, int iScore)
{
//implement your code here.
Node next = new Node() { Score = iScore, Next = null, Name = strName };
if (Head.Next == null && Head != next)
{
Head.Next = next;
}
else
{
if (Head == next)
{
throw new Exception("");
}
else
{
m_nodeHead = Head.Next;
Append(strName, iScore);
}
}
}
public static bool operator ==(Node n1, Node n2)
{
return object.Equals(n1.Name, n2.Name);
}
public static bool operator !=(Node n1, Node n2)
{
return !object.Equals(n1.Name, n2.Name);
}
while (loopNode.Next != null)//遍历列表
{
loopNode = loopNode.Next;
if (loopNode.Name.Equals(strName))//重复抛出异常
{
throw new Exception();
}
}
if (loopNode.Next == null)//列表末尾
{
Node addNode = new Node { Name = strName, Score = score, Next = null };
loopNode.Next = addNode;
}
}
public void Remove(string strName)
{
Node loopNode = Head;//指向头节点
while (loopNode.Next != null)//遍历列表
{
Node nextNode = loopNode.Next;
if (nextNode.Name.Equals(strName))
{
if (nextNode.Next == null)
loopNode.Next = null;
else
{
loopNode.Next = nextNode.Next;
break;
}
}
}
}
}
没有验证过,不知道对不?
[解决办法]
恩,头节点给了,然后就是迭代。Append函数 就是查询Node Next为null的对象并赋予Node对象,Remove 也是同样的原理。
[解决办法]