双向链表的实现(js)

文章目录



前言

本文将介绍双向链表的基本原理和实现


一、双向链表的概念

      由单向链表的缺点引出双向链表的概念:在实际开发中我们经常遇到需要回到上一个节点的情况,这个时候单向链表又需要回到第一个节点,逐个遍历,才能达到需求,恰恰双向链表可以解决这个问题,为什么呢?

     因为双向链表的一个节点既有当前节点的前一个节点的引用,也有当前节点的后一个节点的引用,因此双向链表既可以从头遍历到尾,也可以从尾遍历到头

图例

      

 

二、双向链表的实现

1.双向链表的封装

    1.解析和单向链表的封装基本原理大同小异

function DouLinkList(){
            function Node (data){
                this.data=data
                this.prev=null
                this.next=null
            }
            this.head=null;
            this.tail=null;
            this.lenght=0;
        }

   2.双向链表的一些基本方法

2.1apend方法

  DouLinkList.prototype.append=function(data){
            var newNode=new Node(data);
            if(this.lenght==0){
                this.head=newNode;
                this.tail=newNode
                newNode.prev=head;
            }else{
            var current=this.head;
            while(current.next){
                current=current.next;
            }
            current.next=newNode;
            newNode.prev=current
            this.tail=newNode
            }
             this.length+=1
        }

 2.2backwardString方法:打印链表中所有元素

DouLinkList.prototype.backwardString=function(){
    var current=this.head;
    var listString='';
    while(current!=null){
        listString+=current.data+'';
        current=current.next;
    }
return listString;
}

 2.3insert方法:向元素任何位置插入元素

 DouLinkList.prototype.insert = function (position, date) {

            if (position < 0 || position > this.lenght) { return false; }
            var newNode = new Node(date)
            if (this.lenght == 0) {
                this.head = newNode;
                this.tail = newNode;
            } else {
                //向0号位置插入新节点
                if (position == 0) {
                    this.head.prev = newNode;
                    newNode.next = this.head;
                    this.head = newNode
                } else if (position == this.lenght) {//当向链表尾部插入元素
                    //将新节点的prev指向原来的尾戒点
                    newNode.prev = this.tail;
                    //将原来节点指向新节点
                    this.tail.next = newNode;
                    //改变tail指针位置,将其指向新添加的尾戒点

                } else {
                    //设置指针找到新结点应该插入的位置
                    var current = this.head;
                    var index = 0;
                    while (index++ < position) {
                        current = current.next;
                    }
                    newNode.next = current;
                    newNode.prev = current.prev;
                    current.prev.next = newNode;
                    current.prev = newNode
                }
            }
        }

2.4 get方法获取目标位置元素的值data

  DouLinkList.prototype.get = function () {
            if (position < 0 || position >= this.lenght) { return null }
            var current = this.head;
            var index = 0;
            while (index++ < position) {
                current = current.next;
            }
            return current.data;
        }

2.4 indexOf方法根据位置根据data获取到元素的位置

 DouLinkList.prototype.indexof = function (data) {
            var current = this.head;
            var index = 0;
            while (current) {
                if (current.data == data) {
                    return index;
                }
                current = current.next
                index += 1
            }
            return -1;
        }

2.4 update方法根据元素位置替换此位置的值

DouLinkList.prototype.update = function (possition, newdata) {
            if (possition < 0 || possition >= this.lenght) return false

            var current = this.head;
            var index = 0;
            while (current) {

                current = current.next

            }
            current.data = newdata;
            return true;
        }

2.5 removeAt方法删除任意位置元素

 DouLinkList.prototype.removeAt = function (position) {
            if (position < 0 || position >= this.lenght) return false

            //2.如果有一个节点
            if (this.lenght == 1) {
                this.head == null;
                this.tail == null;
            } else {
                if (position == 0) {
                    //当前节点的下一个节点会的pre指向null
                    this.head.next.prev = null
                    this.head = this.head.next;
                } else if (position == this.lenght - 1) {
                    //将当前节点的next指向为空
                    this.tail = this.tail.prev
               
                } else {
                    var index = 0;
                    var current = this.head;
                    while (index++ < position) {
                        current = current.next;
                    }
                    current.next.prev = current.prev;
                    current.prev.next = current.next;
                }
            }
            this.lenght -= 1
        }

2.6根据对应的值删除元素

   DouLinkList.prototype.remove=function(data){
//根据data获取到对应的索引值
       var index=this.indexof(data)
      return this.removeAt(index)
   }

import numpy as np import pandas as pd import m
atplotlib.pyplot as plt import seaborn as sns import warnings warnings.filterwarnings('ignore') import ssl ssl._create_default_https_context = ssl._create_unverified_context 

2.读入数据

代码如下(示例):

data = pd.read_csv( 'https://labfile.oss.aliyuncs.com/courses/1283/adult.data.csv') print(data.head()) 

该处使用的url网络请求的数据。


总结

提示:这里对文章进行总结:
例如:以上就是今天要讲的内容,本文仅仅简单介绍了pandas的使用,而pandas提供了大量能使我们快速便捷地处理数据的函数和方法。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值