JavaScript数据结构复习(篇5-4)双向循环链表

双向循环链表定义

双向链表的每个结点需要连接前一个结点和后一个结点,所以需要定义两个指针域,分别指向前一个结点和后一个结点。

双向链表中头节点的prev指针指向尾节点,尾节点的next指针指向头节点。

双向循环链表图示

说明:图源网络
在这里插入图片描述

创建双向循环链表

function DoublyCircularLinkedList(){
	function Node(element){
		this.element=element
		this.next=null
		this.prev=null
	}
	let length=0
	let head=null
	let tail=null
}

尾部插入新节点

	this.append=function(element){
		let node = new Node(element)
		let current // 当前节点
		let previous // 前一个节点
		if(!head){
			head=node
			tail=node
			head.prev=tail
			tail.next=head
		}else{
			current=head
			while(current.next !== head){
				previous = current
				current = current.next
			}
			current.next=node
			node.next=head
			node.prev=current
		}
		length++
		return true
	}

任意位置插入节点

	// 在任意位置插入一个节点
	this.insert=function(position,element){
		if(position > 0 && position <= length){
			let node = new Node(element)
			let index = 0
			let current = head
			let previous
			if(position === 0){ // 头部插入
				if(!head){
					node.next=node
					node.prev=node
					head=node
					tail=node
				}else{
					current.prev=node
					node.next=current
				}
			}else if(position === length){ // 在尾部插入
				current=tail
				current.next=node
				node.prev=current
				tail=node
				node.next=head
			}else{
				while(index++ < position){
					previous=current
					current=current.next
				}
				current.prev=node
				node.next=current
				previous.next=node
				node.prev=previous
			}
			length++
			return true
		}else{
			return false
		}
	}

根据位置删除节点

this.removeAt = function(position){ 
	    if(position > -1 && position < length){   
	      let current = head
	      let index = 0
	      let previous; 
	      if(position === 0){   
	        current.next.previous = tail
	        head = current.next;   
	      }else if(position === length - 1){   
	        current = tail;  
	        current.prev.next = head
	        head.prev = current.prev
	        tail = current.prev
	      }else{ 
	        while(index++ < position){ 
	          previous = current
	          current = current.next
	        } 
	        previous.next = current.next
	        current.next.prev = previous
	      } 
	      length--
	      return true
	    }else{ 
	      return false
	    } 
	  }

根据节点值删除节点

	this.remove = function(element){ 
	  let current = head
	  let  previous
	  let  indexCheck = 0  
	  while(current && indexCheck < length){ 
		if(current.element === element){ 
		  if(indexCheck === 0){ 
			current.next.prev = tail; 
			head = current.next
		  }else{ 
			current.next.prev = previous
			previous.next = current.next
		  } 
		  length--
		  return true
		} 	    
		previous = current
		current = current.next
		indexCheck++
	  }    
	  return false
	}

其他方法

  this.isEmpty = function(){ 
	return length === 0; 
  }; 
  //
  this.getHead = function(){ 
	return head; 
  }; 
  //
  this.getTail = function(){ 
	return tail; 
  }; 
  //
  this.size = function(){ 
	return length; 
  }; 

测试

let doubleCircularLinkedList=new DoubleCircularLinkedList()
let string11 = doubleCircularLinkedList.toString()
doubleCircularLinkedList.append('E')
let string1 = doubleCircularLinkedList.toString()
doubleCircularLinkedList.insert(1,'A')
let string2 = doubleCircularLinkedList.toString()
doubleCircularLinkedList.insert(1,'C')
let string3 = doubleCircularLinkedList.toString()
doubleCircularLinkedList.append('F')
let string4 = doubleCircularLinkedList.toString()
console.log(doubleCircularLinkedList.size())
doubleCircularLinkedList.remove('A')
let string5 = doubleCircularLinkedList.toString()
debugger
console.log(doubleCircularLinkedList.toString())
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值