내 hw 완료하고 잘못 됐어. 나는 왜 그런지 이해하지 못한다.이중 링크 목록 삽입 앞 메서드
내 삽입 앞면에 대해 다음을 수행합니다.
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;
}
}
나는 무언가를 얻지 못한다. 당신은 결코 당신의'head' 값을 새롭게하지 않을 것이다. – nachokk