其他分享
首页 > 其他分享> > 什么是单向链表?单向链表的基本操作?如何封装?......

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

作者:互联网

 什么是单向链表?

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

 

单向链表的基本操作?

 

如何封装?

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