2009-06-07 7 views
0
void push_front(const dataType &item) 
    { 
    head=new dnode<dataType> (item,NULL,head); 
    if (!empty()) 
     head->next->prev=head; 
    else 
     tail=head; 
    numItems++; 
    } 

여기 코드 조각이 있는데, 실제로 이해할 수 없지만 줄은 무엇입니까 head->next->prev=head합니까? 사람이 이전 머리는 말했다 너무 head->next->prev=head;prev 필드를 고정하고, 감사연결된 목록 밀어 넣기 전면

아마도
+0

나는 그것을 한 적이. 편집기에서 10101 버튼을 사용하십시오. –

+0

나는 또한 그 코드의 포맷을 고치려고 노력하고 있지만, 내 편집물과 당신이 충돌하는 것처럼 보입니다 ...! -) –

답변

1

head=new dnode<dataType> (item,NULL,head); 이전 headhead->next을 설정 설명해주십시오 수 (이전 NULL했다, 지금은 HEW head입니다).

13

목록은 다음과 같습니다 경우 : 이제

 *************** 
head->*Data: XXX * 
     *Prev: NULL * *************** 
     *Next: --------> * Data: YYY * 
     *************** <----Prev:  *  *************** 
         * Next: --------> * Data: ZZZ * 
         ***************<------Prev:  * 
              * Next: NULL * 
              *************** 

: 체인에 새 항목

head = new dnode<dataType> (AAA,NULL,head); 


     *************** 
head->*Data: AAA * 
     *Prev: NULL * *************** 
     *Next: --------> * Data: XXX * 
     *************** * Prev: NULL *  *************** 
         * Next: --------> * Data: YYY * 
         ***************<------Prev:  *  *************** 
              * Next: --------> * Data: ZZZ * 
              ***************<------Prev:  * 
                   * Next: NULL * 
                   *************** 

공지 사항을 두 번째 항목을 추가합니다. 이전 멤버는 여전히 NULL입니다.
두 번째 항목의 링크를 머리에 다시 추가하십시오.

head->next->prev=head; 

     *************** 
head->*Data: AAA * 
     *Prev: NULL * *************** 
     *Next: --------> * Data: XXX * 
     ***************<-----Prev:  *  *************** 
         * Next: --------> * Data: YYY * 
         ***************<------Prev:  *  *************** 
              * Next: --------> * Data: ZZZ * 
              ***************<------Prev:  * 
                   * Next: NULL * 
                   *************** 

그래서 당신은 라인 생각할 수 :

head->next->prev=head; 

    // This is equivelant too: 

    oldHead  = head->next; 
    oldHead->prev = head; 
+0

ahh. 그게 너무 도움이된다, 고마워. –

+2

+1, 그림은 천 단어의 가치가있다. –