Skip to content

Commit

Permalink
单向链表封装的代码修改
Browse files Browse the repository at this point in the history
  • Loading branch information
XPoet committed Jul 27, 2020
1 parent 3b0865e commit 2b6ad98
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/LinkedList/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { LinkedList } from './linkedList';

// ----- 单向链表结构测试 -----//
// ---------------- 封装的单向链表结构测试 ---------------- //
console.log('// ----- 单向链表结构测试 START -----//');
const linkedList = new LinkedList();

Expand Down
16 changes: 8 additions & 8 deletions src/LinkedList/linkedList.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export class LinkedList {

// ------------ 链表的常见操作 ------------ //

// append() 往链表尾部追加数据
// append(data) 往链表尾部追加数据
append(data) {
// 1、创建新节点
const newNode = new Node(data);
Expand All @@ -46,7 +46,7 @@ export class LinkedList {
this.length++;
}

// insert() 在指定位置(position)插入节点
// insert(position, data) 在指定位置(position)插入节点
insert(position, data) {
// position 新插入节点的位置
// position = 0 表示新插入后是第一个节点
Expand Down Expand Up @@ -91,7 +91,7 @@ export class LinkedList {
return newNode;
}

// getData() 获取指定位置的 data
// getData(position) 获取指定位置的 data
getData(position) {
// 1、position 越界判断
if (position < 0 || position >= this.length) return null;
Expand All @@ -108,7 +108,7 @@ export class LinkedList {
return currentNode.data;
}

// indexOf() 返回指定 data 的 index,如果没有,返回 -1。
// indexOf(data) 返回指定 data 的 index,如果没有,返回 -1。
indexOf(data) {
let currentNode = this.head;
let index = 0;
Expand All @@ -124,7 +124,7 @@ export class LinkedList {
return -1;
}

// update() 修改指定位置节点的 data
// update(position, data) 修改指定位置节点的 data
update(position, data) {
// 涉及到 position 都要进行越界判断
// 1、position 越界判断
Expand All @@ -143,7 +143,7 @@ export class LinkedList {
return currentNode;
}

// removeAt() 删除指定位置的节点
// removeAt(position) 删除指定位置的节点,并返回删除的那个节点
removeAt(position) {
// 1、position 越界判断
if (position < 0 || position >= this.length) return null;
Expand Down Expand Up @@ -175,9 +175,9 @@ export class LinkedList {
return currentNode;
}

// remove() 删除指定 data 的节点
// remove(data) 删除指定 data 的节点,并返回删除的那个节点
remove(data) {
this.removeAt(this.indexOf(data));
return this.removeAt(this.indexOf(data));
}

// isEmpty() 判断链表是否为空
Expand Down

0 comments on commit 2b6ad98

Please sign in to comment.