ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

什么是单向链表?单向链表的基本操作?如何封装?......

2022-08-24 23:34:00  阅读:214  来源: 互联网

标签:current head index 单向 next 链表 position 基本操作


 什么是单向链表?

单向链表(单链表)是链表的一种,其特点是链表的链接方向是单向的,对链表的访问要通过顺序读取从头部开始;链表是使用指针进行构造的列表--百度

 

单向链表的基本操作?

  • append(element):向列表尾部添加一个新的项

  • insert(position, element):向列表的特定位置插入一个新的项。

  • remove(element):从列表中移除一项。

  • indexOf(element):返回元素在列表中的索引。如果列表中没有该元素则返回-1

  • removeAt(position):从列表的特定位置移除一项。

  • isEmpty():如果链表中不包含任何元素,返回true,如果链表长度大于0则返回false

  • size():返回链表包含的元素个数。与数组的length属性类似。

  • toString():由于列表项使用了Node类,就需要重写继承自JavaScript对象默认的toString方法,让其只输出元素的值。

 

如何封装?

1、向链表最后添加元素

// 节点的结构
        class Lnode {
            constructor(data) {
                this.data = data;
                this.next = null;
            }
        }
        // 链表的结构
        class LinkList {
            constructor() {
                this.head = null;
                this.length = 0;
            }


            append(ele) {
                // 创建新节点
                let newnode = new Lnode(ele);
                console.log(newnode);

                if (this.head == null) {
                    this.head = newnode;

                } else {
                    let current = this.head;

                    while (current.next != null) {
                        // 继续找
                        current = current.next
                    };

                    current.next = newnode;
                }
                this.length++
            }
        }

        let list = new LinkList();
        for (let i = 0; i < 5; i++) {
            list.append(i)
        }
        console.log(list);

 

打印:结果

 

 

2、链表的insert操作

       insert(position, el) {
                // 位置是否合法
                if (position < 0 || position > this.length || Number.isInteger(position)) {
                    return false
                }

                let newnode = new Lnode(ele)
                // 1、在头部插入

                if (position == 0) {
                    if (this.head == null) {
                        this.head = newnode;
                    } else {
                        newnode.next = this.head;
                        this.head = newnode
                    }
                    this.length++;
                } else if (position == this.length) {
                    //尾部
                    this.append(ele)
                } else {
                    let current = this.head
                    let index = 0

                    while (index < position - 1) {
                        current = current.next;
                        index++
                    }

                    newnode.next = current.next
                    current.next = newnode;
                    this.length++
                }

            }

 

3、移除指定位置的元素

       removeAt(position) {
                if (position < 0 || position > this.length - 1 || !Number.isInteger(position)) {
                    return false
                }
                if (this.head == null) { 
                    return
                }else{
                    if(position ==0 ){
                        this.head = this.head.next
                    }else{
                        let current = this.head,                        index = 0;
                        index = 0;
                        while (index< position -1){
                            current = current.next;
                            index++;
                        }
                        current.next = current.next.next;
                    }
                    this.length--;
                }
            }

 

4、查找指定元素的位置,存在返回index

       indexOf(ele){
                let  current = this.head,
                index =0;
                while(index<this.length){
                    if(current.data ==ele){
                        return index
                    }else{
                        current = current.next
                        index++;
                    }
                }
                return -1;
            }

 

5、remove 移除指定元素

          remove(ele){
                let index = this.indexOf(ele);
                this.removeAt(index)
            }

 

6、将链表中的数据连接为字符串

 

          toString(){
                let current = this.head,index = 0,res = "";
                while(index <this.length){
                    res += "-"+current.next;
                    current = current.next;
                    index++;
                }
                return res.slice(1)
            }

 

标签:current,head,index,单向,next,链表,position,基本操作
来源: https://www.cnblogs.com/LIXI-/p/16614631.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有