首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >链表(数据结构)的实现,返回值被忽略

链表(数据结构)的实现,返回值被忽略
EN

Stack Overflow用户
提问于 2020-12-04 12:11:07
回答 1查看 36关注 0票数 0

代码包括链表(javaScript中的数据结构)中的addingToTail实现。我希望如果有人能回答为什么在下面的代码中返回的存在没有明显的区别。as return部件是可选的。

代码语言:javascript
复制
class Node {
  constructor(value) {
    this.value = value;
    this.next = null;
  }
}

class LinkedList {
  constructor() {
    this.head = null;
    this.tail = null;
    this._size = 0;
  }

  addToTail(value) {
   if (this.head === null) {
      this.head = addnode;
      this.tail = addnode;
      this._size++;
      return;
    }
    else {
      this.tail.next = addnode;
      this.tail = addnode;
      this._size++;
      return;
  }
EN

回答 1

Stack Overflow用户

发布于 2020-12-07 04:24:10

在两种可能的条件结束时,您将从function返回。

代码#1 (您的代码,添加了缺少的分号)

代码语言:javascript
复制
addToTail(value) {
   if (this.head === null) {
      this.head = addnode;
      this.tail = addnode;
      this._size++;
      return;
    }
    else {
      this.tail.next = addnode;
      this.tail = addnode;
      this._size++;
      return;
    }
}

代码#2

代码语言:javascript
复制
addToTail(value) {
    if (this.head === null) {
        this.head = addnode;
        this.tail = addnode;
        this._size++;
    }
    else {
        this.tail.next = addnode;
        this.tail = addnode;
        this._size++;
    }
    return;
}

代码#1和#2都与末尾的return are相同。

EDIT 1:更新函数以使用尾部成员。

理想情况下,此函数类似于:

代码语言:javascript
复制
addToTail(value) {
    const newNode = new Node(value);
    this.tail.next = newNode;
    this.tail = newNode;
    this._size++;
    return newNode;
}

如果有效,请让我知道。

另外,看看我创建的这个LinkedList类,_InsertRawTypeItem函数符合您的要求。

Class GitHub

NPM package - @dsinjs/linked-list

Complete documentation

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65137824

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档