数据结构——单向循环链表&双向循环链表

一、单向循环链表

(一)概念

  • 将单链表尾节点的指针域置为起始节点的地址,而不再是NULL,这样从表中任一节点出发,均可访问到链表中的所有节点

(二)单向循环链表的结构图

(三)程序封装单向循环链表 

   1.创建链表类和节点类

 //节点的结构
        class Cnode {
            constructor(data) {
                this.data = data
                this.next = null
            }

        }
//链表的结构
        class CycleLinkList {
            constructor() {
                this.head = null
                this.length = 0
            }
         }

  2.单向循环链表的操作(方法) 

   1)append() 向链表尾部添加一个节点

append(ele) {
                let newnode = new Cnode(ele)

                if(this.head==null){
                    //空链表
                    this.head=newnode
                    newnode.next=this.head 
                }else{
                    //非空链表
                    //需要找到最后一个节点
                    let current=this.head
                    while(current.next!=this.head){
                        current=current.next
                    }
                    //找到了current表示最后一个节点
                    current.next=newnode
                    newnode.next=this.head
                }
                this.length++
            }

   2)toString()  将链表中的数据连接为字符串

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

   3)insert() 向链表中插入元素 

 insert(position, ele) {
                if (position < 0 || position > this.length || Number.isInteger(position)) {
                    return false
                }
                let newnode = new Cnode(ele)
                let current = this.head,
                    index = 0
                if (position == 0) {
                    //在头部插入
                    if (this.length == 0) {
                        //空链表
                        this.head = newnode
                        newnode.next = this.head
                    } else {
                        //非空链表
                        while (current.next != this.head) {
                            current = current.next
                        }
                        newnode.next = this.head
                        current.next = newnode
                        this.head = newnode
                    }
                    this.length++
                } else if (position == this.length) {
                    //在尾部插入
                    this.append(ele)
                } else {
                    //在中间插入
                    while (index < position - 1) {
                        current = current.next
                        index++
                    }
                    newnode.next = current.next
                    current.next = newnode
                    this.length++
                }

            }

    4)removeAt() 移除在指定位置的元素

removeAt(position) {
                //判断位置是否合法(越界判断)
                if (position < 0 || position > this.length - 1 || !Number.isInteger(position)) {
                    return false
                }
                //非空链表
                let current = this.head,
                    index = 0
                if (position == 0) {
                    //移除头部
                    if (this.length == 1) {
                        //只有一个节点
                        this.head = null
                    } else {
                        //找到最后一个节点 最后一个节点的next指向新头部
                        while (current.next != this.head) {
                            current = current.next
                        }
                        this.head = this.head.next
                        current.next = this.head
                    }
                } else {
                    //移除任意位置(包括末尾)
                    while (index < position - 1) {
                        current = current.next
                        index++
                    }
                    current.next = current.next.next
                }
                this.length--
            }

   5)indexOf() 查找指定元素

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
            }

    6)remove() 移除指定元素

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

(四)完整代码&操作示例

        //节点的结构
        class Cnode {
            constructor(data) {
                this.data = data
                this.next = null
            }

        }
        //链表的结构
        class CycleLinkList {
            constructor() {
                this.head = null
                this.length = 0
            }

            //1.append() 向链表尾部添加一个元素
            append(ele) {
                let newnode = new Cnode(ele)

                if (this.head == null) {
                    //空链表
                    this.head = newnode
                    newnode.next = this.head
                } else {
                    //非空链表
                    //需要找到最后一个节点 最后一个节点的next指向头结点
                    let current = this.head
                    while (current.next != this.head) {
                        //继续往下找
                        current = current.next
                    }
                    //找到了current表示最后一个节点
                    current.next = newnode
                    newnode.next = this.head
                }
                this.length++
            }

            //2.toString()  将链表中的数据链接为字符串
            toString() {
                let current = this.head,
                    index = 0,
                    str = ""
                while (index < this.length) {
                    str += "-" + current.data
                    current = current.next
                    index++
                }
                return str.slice(1)
            }

            //3.向链表中插入元素
            insert(position, ele) {
                if (position < 0 || position > this.length || !Number.isInteger(position)) {
                    return false
                }
                let newnode = new Cnode(ele)
                let current = this.head,
                    index = 0
                if (position == 0) {
                    //在头部插入
                    if (this.length == 0) {
                        //空链表
                        this.head = newnode
                        newnode.next = this.head
                    } else {
                        //非空链表
                        while (current.next != this.head) {
                            current = current.next
                        }
                        newnode.next = this.head
                        current.next = newnode
                        this.head = newnode
                    }
                    this.length++
                } else if (position == this.length) {
                    //在尾部插入
                    this.append(ele)
                } else {
                    //在中间插入
                    while (index < position - 1) {
                        current = current.next
                        index++
                    }
                    newnode.next = current.next
                    current.next = newnode
                    this.length++
                }

            }

            //4.移除在指定位置的元素
            removeAt(position) {
                //判断位置是否合法(越界判断)
                if (position < 0 || position > this.length - 1 || !Number.isInteger(position)) {
                    return false
                }
                //非空链表
                let current = this.head,
                    index = 0
                if (position == 0) {
                    //移除头部
                    if (this.length == 1) {
                        //只有一个节点
                        this.head = null
                    } else {
                        //找到最后一个节点 最后一个节点的next指向新头部
                        while (current.next != this.head) {
                            current = current.next
                        }
                        this.head = this.head.next
                        current.next = this.head
                    }
                } else {
                    //移除任意位置(包括末尾)
                    while (index < position - 1) {
                        current = current.next
                        index++
                    }
                    current.next = current.next.next
                }
                this.length--
            }

            //5.查找指定元素
            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
            }


            //6.移除指定元素
            remove(ele) {
                let index = this.indexOf(ele)
                this.removeAt(index)
            }

        }
        let clist = new CycleLinkList()

       //链表操作
        //在末尾添加元素
        for (let i = 0; i < 9; i++) {
            clist.append(i)
        }
        console.log(clist.toString(),"添加了元素")
        //在指定位置插入元素
        clist.insert(3, "newnode")
        console.log(clist.toString(),"在3的位置插入了newnode")
        //移除指定位置的元素
        clist.removeAt(4)
        console.log(clist.toString(),"移除了第4个节点--3")
        //查找元素
        console.log(clist.indexOf(3),"查找3,已经被移除了所以返回-1")
        console.log(clist.indexOf(5),"存在,返回下标")
        //移除指定元素
        clist.remove(8)
        console.log(clist.toString(),"删除了8")

打印结果:

二、双向循环链表

(一)概念

  • 可以从两个方向进行遍历,可以利用中间的一个节点推出下一个节点和上一个节点

(二)双向循环链表的结构图

(三)程序封装双向循环链表 

1.创建链表类和节点类

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

2.单向循环链表的操作(方法) 

  1)append() 向链表尾部添加一个节点

append(ele) {
                let newnode = new Dnode(ele);
                if (this.length == 0) { //空链表
                    this.head = newnode;
                    this.tail = newnode;
                    newnode.prev = newnode;
                    newnode.next = newnode; //自己连自己
                } else {
                    //将新节点连接
                    newnode.prev = this.tail; //新节点.prev = 尾节点
                    newnode.next = this.head; //新节点的next指向头结点
                    //断开原来的指向,重新指向新节点
                    this.tail.next = newnode; //尾节点.next指向新节点
                    this.head.prev = newnode; //头结点的prev指向新节点
                    this.tail = newnode; //将尾节点指向新节点
                }
                this.length++;
            }

  2)insert() 向链表中插入元素 

insert(position, ele) {
                // 位置是否合法
                if (position < 0 || position > this.length || !Number.isInteger(position)) {
                    return false
                }
                let newnode = new Dnode(ele);
                if (position == 0) {
                    //在头部插入
                    if (this.length == 0) { //空链表
                        this.head = newnode;
                        this.tail = newnode;
                        newnode.prev = this.tail
                        newnode.next = this.head
                    } else {
                        //将新节点连接
                        newnode.next = this.head; //新节点.next指向头节点
                        newnode.prev = this.tail; //新节点的prev指向尾结点
                        //断开原来的指向

                        this.head.prev = newnode; //头节点.prev指向新节点
                        this.tail.next = newnode //尾节点的next指向新节点
                        this.head = newnode; //头节点指向新节点
                    }
                    this.length++
                } else if (position == this.length) {
                    //在尾部插入
                    this.append(ele)
                } else {
                    //在中间任意位置
                    let current = this.head,
                        index = 0;

                    while (index < position - 1) {
                        current = current.next;
                        index++;
                    }
                    // 1.将新节点连上去
                    newnode.prev = current;
                    newnode.next = current.next;
                    // 2.断开原来的指向
                    current.next = newnode;
                    newnode.next.prev = newnode;
                    this.length++;
                }
            }

   3)removeAt() 移除在指定位置的元素

 removeAt(position) {
                //判断位置的合法性(越界判断)
                if (position < 0 || position > this.length - 1 || !Number.isInteger(position)) {
                    return false
                }
                if (this.length == 0) {
                    //空链表
                    return
                } else {
                    if (position == 0) {
                        //移除头节点
                        if (this.length == 1) {
                            this.head = null
                            this.tail = null
                        } else {
                            this.head = this.head.next
                            this.tail.prev=this.head
                            this.head.prev = this.head
                        }
                    } else if (position == this.length - 1) {
                        //移除尾结点
                        this.tail = this.tail.prev
                        this.tail.next = this.head
                        this.head.prev=this.tail
                    } else {
                        //移除中间任意位置的节点
                        let current = this.head,
                            index = 0
                        while (index < position - 1) {
                            current = current.next
                            index++
                        }
                        current.next = current.next.next
                        current.next.prev = current
                    }
                    this.length--
                }
            }

  4)indexOf() 查找指定元素

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)正向遍历

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

  7)反向遍历      

 reverseString() {
                let current = this.tail,
                    index = this.length - 1,
                    str = "";
                while (index >= 0) {
                    str += "-" + current.data
                    current = current.prev
                    index--
                }
                return str.slice(1)
            }

(四)完整代码&操作示例

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

            //1.在链表尾部添加元素
            append(ele) {
                let newnode = new Dnode(ele);
                if (this.length == 0) { //空链表
                    this.head = newnode;
                    this.tail = newnode;
                    newnode.prev = newnode;
                    newnode.next = newnode; //自己连自己
                } else {
                    //将新节点连接
                    newnode.prev = this.tail; //新节点.prev = 尾节点
                    newnode.next = this.head; //新节点的next指向头结点
                    //断开原来的指向,重新指向新节点
                    this.tail.next = newnode; //尾节点.next指向新节点
                    this.head.prev = newnode; //头结点的prev指向新节点
                    this.tail = newnode; //将尾节点指向新节点
                }
                this.length++;
            }
            // 2.向链表指定位置插入元素
            insert(position, ele) {
                // 位置是否合法
                if (position < 0 || position > this.length || !Number.isInteger(position)) {
                    return false
                }
                let newnode = new Dnode(ele);
                if (position == 0) {
                    //在头部插入
                    if (this.length == 0) { //空链表
                        this.head = newnode;
                        this.tail = newnode;
                        newnode.prev = this.tail
                        newnode.next = this.head
                    } else {
                        //将新节点连接
                        newnode.next = this.head; //新节点.next指向头节点
                        newnode.prev = this.tail; //新节点的prev指向尾结点
                        //断开原来的指向

                        this.head.prev = newnode; //头节点.prev指向新节点
                        this.tail.next = newnode //尾节点的next指向新节点
                        this.head = newnode; //头节点指向新节点
                    }
                    this.length++
                } else if (position == this.length) {
                    //在尾部插入
                    this.append(ele)
                } else {
                    //在中间任意位置
                    let current = this.head,
                        index = 0;

                    while (index < position - 1) {
                        current = current.next;
                        index++;
                    }
                    // 1.将新节点连上去
                    newnode.prev = current;
                    newnode.next = current.next;
                    // 2.断开原来的指向
                    current.next = newnode;
                    newnode.next.prev = newnode;
                    this.length++;
                }

            }

            //3.移除指定位置的节点
            removeAt(position) {
                //判断位置的合法性(越界判断)
                if (position < 0 || position > this.length - 1 || !Number.isInteger(position)) {
                    return false
                }
                if (this.length == 0) {
                    //空链表
                    return
                } else {
                    if (position == 0) {
                        //移除头节点
                        if (this.length == 1) {
                            this.head = null
                            this.tail = null
                        } else {
                            this.head = this.head.next
                            this.tail.prev=this.head
                            this.head.prev = this.head
                        }
                    } else if (position == this.length - 1) {
                        //移除尾结点
                        this.tail = this.tail.prev
                        this.tail.next = this.head
                        this.head.prev=this.tail
                    } else {
                        //移除中间任意位置的节点
                        let current = this.head,
                            index = 0
                        while (index < position - 1) {
                            current = current.next
                            index++
                        }
                        current.next = current.next.next
                        current.next.prev = current
                    }
                    this.length--
                }
            }
            
            //4.查找指定元素的位置
            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(ele) {
                let index = this.indexOf(ele)
                this.removeAt(index)
            }

            //6.正向遍历
            forwardString() {
                let current = this.head,
                    index = 0,
                    str = "";
                while (index < this.length) {
                    str += "-" + current.data
                    current = current.next
                    index++
                }
                return str.slice(1)
            }
            //7.反向遍历
            reverseString() {
                let current = this.tail,
                    index = this.length - 1,
                    str = "";
                while (index >= 0) {
                    str += "-" + current.data
                    current = current.prev
                    index--
                }
                return str.slice(1)
            }
        }
        let clist = new DoubleCycleLinkList()
        //向链表尾部添加元素
        for (let i = 0; i < 9; i++) {
            clist.append(i)
        }
        console.log(clist.forwardString(),"向链表尾部添加元素后的链表")

        //向链表中的3的位置插入元素
        clist.insert(3,"newnode")
        console.log(clist.forwardString(),"插入元素后的链表")

         //移除指定位置的元素
         clist.removeAt(5)
         console.log(clist.forwardString(),"移除位置为5的元素(4)的链表")
    
         //查找指定元素
         console.log(clist.indexOf(4),"4已经被移除,不存在返回-1")
         console.log(clist.indexOf(6),"6存在返回下标")

         //移除指定元素
         clist.remove(8)
         console.log(clist.reverseString(),"移除元素8后的反向遍历的链表")

打印结果:

  • 4
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

哈哈ha~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值