如何在JavaScript中使用双向链表

  介绍

这篇文章给大家介绍如何在JavaScript中使用双向链表,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

JavaScript的特点

1. JavaScript主要用来向HTML页面添加交互行为。   2. javascript可以直接嵌入到HTML页面,但写成单独的js文件有利于结构和行为的分离。   3.JavaScript具有跨平台特性,在绝大多数浏览器的支持下,可以在多种平台下运行。

//,创建双向链表的构造函数   function  DoublyLinkedList (), {   ,//创建节点构造函数   ,function 节点(元素),{   时间=this.element 才能;元素   this.next 才能=零   时间=this.prev 才能;null //,新添加的   ,}      ,//定义属性   this.length =, 0   ,this.head =零=,,this.tail  null //,新添加的      ,//定义相关操作方法   ,//在尾部追加数据=,,DoublyLinkedList.prototype.append  function (元素),{//1。才能根据元素创建节点   var 才能;newNode =, new 节点(元素)//2。才能判断列表是否为空列表   if 才能;(this.head ==, null), {   ,,this.head =newNode   ,,this.tail =newNode   ,,},{else    ,,this.tail.next =newNode   ,,newNode.prev =this.tail   ,,this.tail =newNode   ,,}//才能,3.长度+ 1   ,this.length + +   ,}      ,//在任意位置插入数据=,,DoublyLinkedList.prototype.insert  function (位置,,元素),{//1。才能判断越界的问题   if 才能;(position  & lt;, 0, | |, position 祝辞,this.length), return 错误的//2。才能创建新的节点   var 才能;newNode =, new 节点(元素)//3。才能判断插入的位置   if 才能;(position ===, 0),{//大敌;在第一个位置插入数据   ,,//,判断链表是否为空   ,,if  (this.head ==, null), {   ,,,this.head =newNode   ,,,this.tail =newNode   ,,},{else    ,,,this.head.prev =newNode   ,,,newNode.next =this.head   ,,,this.head =newNode   ,,}   ,,},else  if  (position ===, this.length),{,//插入到最后的情况//,,,思考:,这种情况是否需要判断链表为空的情况呢?,答案是不需要,,为什么?   ,,this.tail.next =newNode   ,,newNode.prev =this.tail   ,,this.tail =newNode   ,,},else {//大敌;在中间位置插入数据   ,,//,定义属性   ,,var  index =0   ,,var  current =this.head   ,,var  previous =零//,,,查找正确的位置   ,,while (指数+ +,& lt;,位置),{   ,,,previous =,电流   ,,,current =current.next   ,,}//,,,交换节点的指向顺序   ,,newNode.next =,电流   ,,newNode.prev =之前   ,,current.prev =newNode   ,,previous.next =newNode   ,,}//才能,4.长度+ 1   ,this.length + +      return 才能正确   ,}      ,//根据位置删除对应的元素=,,DoublyLinkedList.prototype.removeAt  function (位置),{//1。才能判断越界的问题   if 才能;(position  & lt;, 0, | |, position 祝辞=,this.length), return  null//2。才能判断移除的位置   var 才能;current =this.head   if 才能;(position ===, 0), {   ,,if  (this.length ==, 1), {   ,,,this.head =零   ,,,this.tail =零   ,,},{else    ,,,this.head =this.head.next   ,,,this.head.prev =零   ,,}   ,,},else  if  (position ===, this.length  1), {   ,,current =this.tail   ,,this.tail =this.tail.prev   ,,this.tail.next =零   ,,},{else    ,,var  index =0   ,,var  previous =零      ,,while (指数+ +,& lt;,位置),{   ,,,previous =,电流   ,,,current =current.next   ,,}      ,,previous.next =current.next   ,,current.next.prev =之前   ,,}//才能,3.的长度是1   this.length才能—      return  current.element才能   ,}      ,//根据元素获取在链表中的位置   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null

如何在JavaScript中使用双向链表