首页 > 其他分享 >单链表

单链表

时间:2023-11-27 18:11:37浏览次数:24  
标签:head 单链 nodeList next current return data

class Node {
    constructor(data) {
        this.data = data
        this.next = null
    }
}
class NodeList {
    constructor() {
        this.head = null
        this.length = 0
    }

    appendNode(data) {
        const newNode = new Node(data)
        let currentNode = this.head
        if (this.head === null) {
            this.head = newNode
        } else {
            while (currentNode.next) {
                currentNode = currentNode.next
            }
            currentNode.next = newNode
            newNode.next = null
        }
    }

    insert(data, target) {
        const newNode = new Node(data)
        let current = this.head
        while (current) {
            if (current.data === target) {
                newNode.next = current.next
                current.next = newNode
                return true
            }
            current = current.next
        }
        return false
    }

    update(data, target) {
        let current = this.head
        while (current) {
            if (current.data === target) {
                current.data = data
                return true
            }
            current = current.next
        }
        return false
    }

    remove(target) {
        let current = this.head
        let previous = null
        while (current) {
            if (current.data === target) {
                previous.next = current.next
                return true
            }
            previous = current
            current = current.next
        }
        return false
    }

    find(target) {
        let current = this.head
        while (current) {
            if (current.data === target) {
                return current
            }
            current = current.next
        }
        return null
    }

    sort() {
        let current = this.head
        while(current) {
            
        }
    }

    format() {
        let current = this.head
        let res = []
        while (current) {
            res.push(current.data)
            current = current.next
        }
        return res
    }
}

const nodeList = new NodeList()
nodeList.appendNode(1)
nodeList.appendNode(2)
nodeList.appendNode(3)
nodeList.appendNode(4)
nodeList.appendNode(5)
nodeList.appendNode(6)
nodeList.appendNode(7)
nodeList.appendNode(8)
nodeList.insert(10, 5)
nodeList.update(101, 1)
nodeList.remove(4)
console.log(nodeList.find(5))
console.log(nodeList.format())

 

class Node {   constructor(data) {     this.data = data     this.next = null   } } class NodeList {   constructor() {     this.head = null     this.length = 0   }
  appendNode(data) {     const newNode = new Node(data)     let currentNode = this.head     if (this.head === null) {       this.head = newNode     } else {       while (currentNode.next) {         currentNode = currentNode.next       }       currentNode.next = newNode       newNode.next = null     }   }
  insert(data, target) {     const newNode = new Node(data)     let current = this.head     while (current) {       if (current.data === target) {         newNode.next = current.next         current.next = newNode         return true       }       current = current.next     }     return false   }
  update(data, target) {     let current = this.head     while (current) {       if (current.data === target) {         current.data = data         return true       }       current = current.next     }     return false   }
  remove(target) {     let current = this.head     let previous = null     while (current) {       if (current.data === target) {         previous.next = current.next         return true       }       previous = current       current = current.next     }     return false   }
  find(target) {     let current = this.head     while (current) {       if (current.data === target) {         return current       }       current = current.next     }     return null   }
  sort() {     let current = this.head     while(current) {           }   }
  format() {     let current = this.head     let res = []     while (current) {       res.push(current.data)       current = current.next     }     return res   } }
const nodeList = new NodeList() nodeList.appendNode(1) nodeList.appendNode(2) nodeList.appendNode(3) nodeList.appendNode(4) nodeList.appendNode(5) nodeList.appendNode(6) nodeList.appendNode(7) nodeList.appendNode(8) nodeList.insert(10, 5) nodeList.update(101, 1) nodeList.remove(4) console.log(nodeList.find(5)) console.log(nodeList.format())

标签:head,单链,nodeList,next,current,return,data
From: https://www.cnblogs.com/karle/p/17860019.html

相关文章

  • 单链表
    一、算法描述本篇文章讲述的数据结构是单链表,当然不是常规的单链表,而是算法竞赛中常用的用数组模拟的单链表。//常规的单链表定义如下:struct{intval;Node*next;}//用数组模拟的单链表定义如下:inthead;inte[N],ne[N],idx;/* head表示头结点的下标 e[......
  • 单链表(SingleLinkedList)
    单链表1.创建一个Node类//head不能动,头节点作用是表示链表的头privateNodehead;//在linkedList类写一个Node的成员内部类privateclassNode{privateintdata;privateNodenext;publicNode(intdata){this.data=data;th......
  • C语言数据结构_查找并删除单链表中最大值结点并返回值
    代码实现1#include<stdio.h>2#include<stdlib.h>34typedefstructNode//定义一个结构体5{6floatdata;7structNode*next;8}Node;910Node*Chuangzao_LinkedList()//创建一个链表11{12Node*head=NULL;//......
  • 单链表建表,倒置,遍历(不使用Class,简洁易懂)
    在C++中通过递归方法实现单链表倒置初始化列表structListNode{ intval; LiseNode*next; ListNode(intx):val(x),next(NULL){}};遍历voidquery_node(){ node*p=head; while(p!=NULL){ cout<<p->data<<''; p=p->nxt; } cout<<endl;}建表(......
  • (链表)12-单链表的排序
    1importjava.util.*;23/*4*publicclassListNode{5*intval;6*ListNodenext=null;7*publicListNode(intval){8*this.val=val;9*}10*}11*/12publicclassSolution{13/**14*@paramhead......
  • 线性表-单链表
    首先定义一个元素typedefint LlElemtype;然后元素定义单链表,第一个结构体存放数据成员,第二个结构体存放下个节点的地址(可以用指针表示)typedefstruct __LNode{LlElemtypedata;__LNode*next;//用的是前面的名字}LNode,*LinkList  ......
  • Linux多路径IO流量负载和单链路负载压测
     LinuxMultipath的IO流量多链路负载和单链路负载压测 再linux下,对于udev和multipath均能做到自定义并持久化设备名,其中udev还能做到更改设备权限。而multipath也能做到持久化设备名,但无法更改设备权限,但是multipath能够实现更多的功能,比如IO流量负载功能。 测试情况1......
  • 2008秋-计算机软件基础-单链表练习(1)
    /*--------------------------------------------------------设有一个单链表,头结点为head,为递增有序,写一个完整程序,将其改为递减有序。----------------------------------------------------------*/#include<stdio.h>#include<stdlib.h>//定义结点structnodetype......
  • 2008秋-计算机软件基础-单链表完整示例
    /*---------------------------------------------------------Title:CompletedSimpleLinkedListAuthor:EmanLeeDate:Oct22,2008Fuction:OperationonLinkedStoredLinearList.Thisisacompletedsimplesample.Itisrelatedto......
  • 2008秋季-线性表的链式存储(仅单链表)
    /*---------------------------------------------------------Title:单链表Date:September1,2008Fuction:单链表的初始化,创建,插入,删除,查找结点。参考PPT讲稿或者教材2.2.4节.(p56-63)----------------------------------------------------------*/#inclu......