2011-08-28 4 views
0

아래 코드에서 d.tagName 오류가 발생했습니다. 이것은 알파벳순으로 목록에 새 요소를 삽입 할 때 발생하며 목록의 마지막 요소보다 큽니다. 이 경우 목록 끝에 삽입해야합니다. e와 b는 삽입 될 원소이고, d는 알파벳 순서를 유지하기 위해 앞에 삽입되어야하는 원소이다. 요소가 목록의 마지막 부분 인 경우를 제외하고는 제대로 작동합니다.알파벳순 삽입 알고리즘 - 특수한 경우

while(d=d.nextSibling) 
    { 
    if(d.tagName=="undefined") 
     { 
     a.insertBefore(e,d); 
     a.insertBefore(b,d); 
     break; 
     } 
    else if(d.tagName.toLowerCase()==="a" && (b.innerHTML<d.innerHTML)) 
     {  
     d=d.previousSibling; 
     a.insertBefore(e,d); 
     a.insertBefore(b,d); 
     break; 
     } 
    } 
    return 1; 
+0

이 보여줄 수있는 바이올린을 가지고 (의사 코드이다)? – naveen

+0

이 코드가있는 기능과 다른 관련 코드를 확인하는 것이 도움이됩니다. 두 조건은 약간의 설명을 사용할 수 있습니다 (즉,'d.tagname == 'undefined'와'd.tagName.toLowerCase() === "a") – James

+0

Bascially, 내 루프는 마지막 요소, typeof를 사용하여 이것을 잡을 수 있습니다. MDN 문서 https://developer.mozilla.org/en/JavaScript/Reference/Operators/Special/typeof –

답변

1

알고리즘은 따라서이다

set inserted to false 
go through the list 
if the current item of the list is alphabetic comes after the one to be inserted 
then 
insert it before 
mark it as inserted ie inserted is true 


If after going though the whole list (ie inserted is false) then just append it to the list 
관련 문제