数据结构之单向循环链接,双向循环链表的常用方法封装

本文介绍了单向循环链表和双向循环链表的实现,包括添加元素、插入元素、移除元素、查找元素等基本操作,并通过示例展示了这些操作的使用。在单向循环链表中,添加、插入和删除元素的逻辑得到了阐述。对于双向循环链表,除了相同的操作外,还额外提供了正向和反向输出链表元素的字符串方法。示例代码演示了链表的创建、元素的添加、删除及查找过程。
摘要由CSDN通过智能技术生成

单向循环链表

上一篇文章已经讲过单向链表和双向链表了,那么这里就不在赘述了,单向循环链接,双向循环链表只是加了一个从尾结点指向头结点的这一步,其他的不变

所以我们就直接封装方法啦,其他的就不在细说了

单向循环链表常用的方法

上一篇文章以及详细讲过,这里就不一一分开写了,直接合并在一起封装出来

<script>
  class Cnode {
        constructor(data) {
            this.data = data;
            this.next = null;
        }
    }
    class CycleLinkList {
        constructor() {
            this.head = null;
            this.len = 0
        }

        // 1.向链表最后添加元素
        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.next = newnode;
                newnode.next = this.head;
            }
            this.len++
        }

        // 2.向链表中插入元素
        insert(position, ele) {
            if (position < 0 || position > this.len || !Number.isInteger(position)) {
            	return
            }
            let newnode = new Cnode(ele);
            let current = this.head,
                index = 0;
            if (position == 0) {
                if (this.len == 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.len++
            } else if (position == this.len) {
                this.append(ele)
            } else {
                while (index < position - 1) {
                    current = current.next;
                    index++;
                }
                newnode.next = current.next;
                current.next = newnode;
                this.len++
            }
        }

        // 3.removeAt(position)  移除指定位置的元素
        removeAt(position) {
            if (position < 0 || position > this.len || !Number.isInteger(position)) {
            	return
            }
            let current = this.head,
                index = 0;
            if (position == 0) {
                if (this.len == 1) {
                    this.head = null;
                } else {
                    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.len--
        }

        // 4.查找指定元素
        indexOf(ele){
            let current = this.head,index=0;
            while(index < this.len){
                if(current.data == ele){
                    return index
                }else{
                    current = current.next;
                    index++
                }
            }
            return -1
        }

        // 5.移除指定元素
        remove(ele){
            this.removeAt(this.indexOf(ele))
        }
        toString() {
            let current = this.head,
                index = 0,
                res = "";
            while (index < this.len) {
                res += "-" + current.data;
                current = current.next;
                index++;
            }
            return res.slice(1)
        }
    }
    
    let clist = new CycleLinkList();
    for (let i = 0; i < 4; i++) {
        clist.append(i)
    }
    
    console.log(clist.indexOf("hello"));
    console.log(clist.indexOf(0));
    console.log(clist.indexOf(2));
    console.log(clist.indexOf(666));

    clist.remove("hello")
    clist.remove(1)
    clist.remove(2)
    clist.remove(0)
    clist.remove(4)

    console.log(clist.toString());
</script>

结果

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
移除过后就只剩下了3这个元素了

双向循环链表

<script>
   class Node {
       constructor(data) {
           this.prev = null;
           this.data = data;
           this.next = null;
       }
   }
   class CycleDoubleLinkList {
       constructor() {
           this.head = null;
           this.tail = null;
           this.len = 0;
       }
       
       // 1.向链表最后添加元素
       append(ele) {
           let newnode = new Node(ele);
           if (this.len == 0) {
               this.head = newnode;
               this.tail = newnode;
               newnode.prev = this.tail;
               newnode.next = this.head;
           } else {
               newnode.prev = this.tail;
               newnode.next = this.head;
               this.tail.next = newnode;
               this.head.prev = newnode;
               this.tail = newnode;
           }
           this.len++
       }

       // 2.向链表中的指定位置插入元素
       insert(position, ele) {
            if (position < 0 || position > this.len || !Number.isInteger(position)) {
            	return
            }
           let newnode = new Node(ele);
           if (position == 0) {
               if (this.len == 0) {
                   this.head = newnode;
                   this.tail = newnode;
                   newnode.prev = this.tail;
                   newnode.next = this.head;
               } else {
                   newnode.prev = this.tail;
                   newnode.next = this.head;
                   this.head.prev = newnode
                   this.tail.next = newnode;
                   this.head = newnode;
               }
               this.len++
           }else if(position == this.len){
               this.append(ele)
           }else{
               let current = this.head,index = 0;
               while(index< position-1){
                   current = current.next;
                   index++;
               }
               newnode.prev = current;
               newnode.next = current.next;
               current.next = newnode;
               newnode.next.prev = newnode;
               this.len++
           }
       }
  
       // 3.移除指定位置的元素
       removeAt(position){
            if (position < 0 || position > this.len || !Number.isInteger(position)) {
            	return
            }
           if(position == 0){
               if(this.len == 1){
                   this.head = null;
                   this.tail = null;
               }else{
                   this.head = this.head.next;
                   this.tail.next = this.head;
                   this.head.prev = this.tail;
               }               
           }else if(position == this.len-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.len--
       }
  
       // 4.查找指定元素
       indexOf(ele){
           let current = this.head,index=0;
           while(index < this.len){
               if(current.data == ele){
                   return index
               }else{
                   current = current.next;
                   index++
               }
           }
           return -1
       }

        // 5.移除指定元素
        remove(ele){
           this.removeAt(this.indexOf(ele))
       }

		// 6.正向字符串输出
       toAfterString(){
           let current= this.head,index=0,res ="";

           while(index < this.len){
               res+= "-" + current.data;
               current = current.next;
               index++;
           }
           return res.slice(1)
       }
		
		// 7.反向字符串输出
       toBeforeString(){
           let current= this.tail,index=this.len-1,res ="";
           while(index >= 0){
               res+= "-" + current.data;
               current = current.prev;
               index--;
           }
           return res.slice(1)
       }
   }
   let list = new CycleDoubleLinkList()
   for (let i = 0; i < 9; i++) {
       list.append(i)
   }
   console.log(list);
   
   list.insert(0,"hello");
   list.insert(5,"world");
   
   list.removeAt(0);
   list.removeAt(0);
   
   console.log(list.toAfterString());
   console.log(list.toBeforeString());
</script>

结果

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值