JavaScript 데이터 구조의 양 방향 링크 정의 와 사용 방법 예제
4475 단어 JavaScript데이터 구조양 방향 링크
양 방향 링크 와 일반 링크 의 차 이 는 링크 에서 한 노드 는 체인 이 다음 노드 의 링크 만 있 고 양 방향 링크 에서 링크 는 양 방향 이다.한 체인 은 다음 요소 이 고 다른 체인 은 한 요소 앞으로 간다.
양 방향 링크 는 두 가지 교체 목록 의 방법 을 제공 했다.처음부터 끝까지 또는 반대로.우 리 는 또한 특정한 노드 의 다음 또는 이전 요 소 를 방문 할 수 있다.단 방향 링크 에서 목록 을 교체 할 때 찾 아야 할 요 소 를 놓 치면 목록 의 출발점 으로 돌아 가 다시 교체 해 야 합 니 다.이것 은 양 방향 링크 의 장점 이다.
function DoubleLink(){
var length=0;//
var head=null;//
var tail=null;//
function Node(e){
this.element=e;
this.next=null;
this.previous=null;
}
this.insertAt=function(position,e){//
if(position>=0&&position<=length){//
var node=new Node(e);
var current=head;
var previous;
var index=0;
if(position==0){//
if(!head){//
head=node;
tail=node;
}else{
current=head;
node.next=current;
current.previous=node;
head=node;
}
}else if(position==length){//
current=tail;
current.next=node;
node.previous=current;
tail=node;
}else{
while(index<position){
previous=current;
current=current.next;
index++;
}
previous.next=node;
node.previous=previous;
node.next=current;
current.previous=node;
}
length++;
return true;
}else{
return null;
}
}
this.removeAt=function(position){//
if(position>-1&&position<length){//
var current=head;
var previous;
var index=0;
if(position==0){//
head=current.next;
if(length==1){//
tail=null;
}else{
head.previous=null;
}
}else if(position==length-1){//
current=tail;
tail=current.previous;
tail.next=null;
}else{
while(index<position){
previous=current;
current=current.next;
index++;
}
previous.next=current.next;
current.next.previous=previous;
}
length--;
return current.element;
}else{
return null;
}
}
this.indexOf=function(e){// ,
var current=head;
var index=0;
while(current){
if(current.element==e){
return index;
}
current=current.next;
index++;
if(index>=length)return null;
}
}
this.isEmpty=function(){//
return length==0;
}
this.mylength=function(){//
return length;
}
this.print1=function(){//
var current=head;
while(current){
console.log(current.element);
current=current.next;
}
}
this.print2=function(){//
var current=tail;
while(current){
console.log(current.element);
current=current.previous;
}
}
this.getHead=function(){//
return head;
}
this.getTail=function(){//
return tail;
}
}
var link=new DoubleLink();//
link.insertAt(0,'d');
link.insertAt(1,'e');
link.insertAt(2,'f');
link.insertAt(3,'g');
link.insertAt(4,'h');
link.insertAt(5,'i');
link.insertAt(6,'j');
link.insertAt(7,'k');
link.removeAt(7);
link.removeAt(0);
link.print1();//efghij
link.print2();//jihgfe
console.log(link.getHead());//e
console.log(link.getTail());//j
console.log(link.indexOf('f'));//1
실행 결과:
자 바스 크 립 트 관련 내용 에 관심 이 있 는 독자 들 은 본 사이트 의 주 제 를 볼 수 있다.
본 고 에서 말 한 것 이 여러분 의 자 바스 크 립 트 프로 그래 밍 에 도움 이 되 기 를 바 랍 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
기초 정리 - 1문자 (String) 숫자 (Number) 불린 (Boolean) null undefined 심볼 (Symbol) 큰정수 (BigInt) 따옴표로 묶어 있어야 함 Not-A-Number - 숫자 데이터 / 숫자로 표...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.