我目前正在努力刷上ADT实现,专门为链表(我使用Java 5的做到这一点)的实现。代码审查链接列表中添加(I,X)方法
我有两个问题:
(1)这是实现我为加法书面(I,X)正确和有效的?
public void add(int i, Object x) { // Possible Cases: // // 1. The list is non-empty, but the requested index is out of // range (it must be from 0 to size(), inclusive) // // 2. The list itself is empty, which will only work if i = 0 // // This implementation of add(i, x) relies on finding the node just before // the requested index i. // These will be used to traverse the list Node currentNode = head; int indexCounter = 0; // This will be used to mark the node before the requested index node int targetIndex = i - 1; // This is the new node to be inserted in the list Node newNode = new Node(x); if (currentNode != null) { while (indexCounter < targetIndex && currentNode.getNext() != null) { indexCounter++; currentNode = currentNode.getNext(); } if (indexCounter == targetIndex) { newNode.setNext(currentNode.getNext()); currentNode.setNext(newNode); } else if (i == 0) { newNode.setNext(head); head = newNode; } } else if (i == 0) { head = newNode; } }
(2)我发现这个方法非常具有挑战性的实现。说实话,这花了我好几天的时间。这很难承认,因为我喜欢编程,并且认为自己处于几种语言和平台的中级水平。我从13岁起就开始编程(Applesoft BASIC在Apple IIc上!)并拥有CS学位。我目前是一名软件测试员,并计划在某个时候成为开发人员。所以我的问题的第二部分是:我是否在愚弄自己,这是我擅长的工作类型,还是几乎每个人都觉得这种问题具有挑战性?有些事情告诉我,即使经验丰富的开发人员,面对实施这种方法,会发现它具有挑战性。
感谢您对第二部分的反馈和建议。
@dvanaria:我认为这个问题更适合http://codereview.stackexchange.com。 –
属于http://codereview.stackexchange.com/ –
好的谢谢,我从来没有听说过codereview.stackexchange,它听起来像一个更好的地方。我会考虑现在移动它。 – dvanaria