完成了我的硬件,我弄错了。我不懂为什么。双向链表插入前端方法
对于我的插入前面我做了以下。
head.next.prev = newNode;
newNode.next = head;
newNode.prev = null;
head.prev = newnode;
head.next.prev = head;
size++;
但相反的解决方案是这样的以下
head.next.prev = newNode(item, head, head.next); // newNode(item,prev,next); So basically head.next.prev is pointing to a newnode here newnode.prev = head and newnode.next = head.next. Ok that make sense.
head.next = head.next.prev; // huh?
size++;
给我的解决方案是没有意义的,我的解决方案是完全合乎逻辑的。如果你让head.next.prev =一个新的节点,你应该使head.next.prev = head,否则会有跳转的权利?另外head.next = head.next.prev;没有任何意义。这条线基本上是说head.prev指向头部本身。不应该是head.next.prev = head;?
任何人都可以指出发生了什么?我知道的解决方案之间的格式是不同的,但我更感兴趣的是逻辑
的完整代码如下所示
有很多困惑。因此,这里的头是如何宣称
public class DList {
/**
* head references the sentinel node.
* size is the number of items in the list. (The sentinel node does not
* store an item.)
*
* DO NOT CHANGE THE FOLLOWING FIELD DECLARATIONS.
*/
protected DListNode head;
protected int size;
/* DList invariants:
* 1) head != null.
* 2) For any DListNode x in a DList, x.next != null.
* 3) For any DListNode x in a DList, x.prev != null.
* 4) For any DListNode x in a DList, if x.next == y, then y.prev == x.
* 5) For any DListNode x in a DList, if x.prev == y, then y.next == x.
* 6) size is the number of DListNodes, NOT COUNTING the sentinel,
* that can be accessed from the sentinel (head) by a sequence of
* "next" references.
*/
/**
* newNode() calls the DListNode constructor. Use this class to allocate
* new DListNodes rather than calling the DListNode constructor directly.
* That way, only this method needs to be overridden if a subclass of DList
* wants to use a different kind of node.
* @param item the item to store in the node.
* @param prev the node previous to this node.
* @param next the node following this node.
*/
protected DListNode newNode(Object item, DListNode prev, DListNode next) {
return new DListNode(item, prev, next);
}
/**
* DList() constructor for an empty DList.
*/
public DList() {
head = newNode(null, head, head);
head.next = head;
head.prev = head;
size = 0;
}
public insertfront(Object item){
???????????}
////////////////////下面是DlistNoe.java
public class DListNode {
/**
* item references the item stored in the current node.
* prev references the previous node in the DList.
* next references the next node in the DList.
*
* DO NOT CHANGE THE FOLLOWING FIELD DECLARATIONS.
*/
public Object item;
protected DListNode prev;
protected DListNode next;
/**
* DListNode() constructor.
* @param i the item to store in the node.
* @param p the node previous to this node.
* @param n the node following this node.
*/
DListNode(Object i, DListNode p, DListNode n) {
item = i;
prev = p;
next = n;
}
}
即时通讯没有得到..你永远不会刷新你的'头'值 – nachokk