好的,所以我正在學習資料結構,我的教程遵循class語法。所以現在我正試圖將它轉換為function宣告,因為我想用這種表示法練習鏈接串列。有人可以幫我轉換嗎?我只需要轉換一種方法,其余的我就可以完成。
這是我的class語法代碼:
class Node {
constructor( val ) {
this.val = val;
this.next = null;
}
}
class SinglyLinkedList {
constructor () {
this.head = null;
this.tail = null;
this.length = 0;
}
push( val ) {
const newNode = new Node( val );
if ( !this.head ) {
this.head = newNode;
this.tail = this.head;
} else {
this.tail.next = newNode;
this.tail = newNode;
}
this.length
return this;
}
}
const list = new SinglyLinkedList();
list.push( 'HELLO' );
list.push( 'WORLD' );
到目前為止,這是我為實作基于函式的方法所做的嘗試:
function Node( val ) {
this.val = val;
this.next = null;
}
let head = null;
let tail = null;
let length = 0;
const SinglyLinkedListFunc = function() {
const newNode = new Node( 'HELLO' );
};
SinglyLinkedListFunc()
這是我迄今為止所能達到的。我被它困住了,我不知道下一步該做什么。有人請幫忙
uj5u.com熱心網友回復:
這是功能方法:
function Node( val, next ) {
this.val = val;
this.next = null;
}
const SinglyLinkedListFunc = function() {
this.head = null;
this.tail = null;
this.length = 0;
};
SinglyLinkedListFunc.prototype.push = function( val ) {
const newNode = new Node( val );
if( !this.head ){
this.head = newNode;
this.tail = newNode;
} else{
this.tail.next = newNode;
this.tail = newNode;
}
this.length ;
return this;
}
let list = new SinglyLinkedListFunc();
list.push('HELLO');
list.push('WORLD');
uj5u.com熱心網友回復:
鏈表是一種功能結構,因此將其與功能樣式一起使用會產生最佳效果。這意味著避免諸如突變和變數重新分配之類的事情。下面我們在面向物件的介面中實作一個函式式的不可變鏈表——
class Node {
constructor(head, tail) {
this.head = head
this.tail = tail
}
}
const empty = Symbol("empty")
class List {
constructor(t = empty) {
this.t = t
}
push(v) {
return new List(new Node(v, this.t))
}
toString() {
if (this.t == empty)
return "?"
else
return `${this.t.head} -> ${new List(this.t.tail)}`
}
}
const l = (new List).push("a").push("b").push("c")
console.log(String(l))
console.log(String(l.push("d").push("e").push("f")))
console.log(String(l))
注意每次呼叫如何.push回傳一個新的、未修改的串列 -
c -> b -> a -> ?
f -> e -> d -> c -> b -> a -> ?
c -> b -> a -> ?
但我認為你可以做得比這更好。通過將功能設計和關注點直接與??面向類的語意相結合,我們最終得到了奇怪且通常效率低下的程式。在函式式風格中,我們撰寫具有普通函式的模塊。如果需要一個面向物件的介面,只需要對我們的普通函式進行一個薄包裝 -
// list.js
const empty = Symbol("empty")
const pair = (left, right) =>
({ left, right })
const toString = t =>
t == empty
? "?"
: `${t.left} -> ${toString(t.right)}`
const push = (t, v) =>
pair(v, t)
class List {
static of(t) { return new List(t) }
constructor(t = empty) { this.t = t }
push(v) { return List.of(push(this.t, v)) }
toString() { return toString(this.t) }
}
const list = List.of
export default list
在你的主模塊中,我們將匯入list從我們的名單模塊-
// main.js
import list from "./list.js"
const l = list().push("a").push("b").push("c")
console.log(String(l))
console.log(String(l.push("d").push("e").push("f")))
console.log(String(l))
c -> b -> a -> ?
f -> e -> d -> c -> b -> a -> ?
c -> b -> a -> ?
展開下面的代碼段以驗證瀏覽器中的結果 -
顯示代碼片段
// list.js
const empty = Symbol("empty")
const pair = (left, right) =>
({ left, right })
const toString = t =>
t == empty
? "?"
: `${t.left} -> ${toString(t.right)}`
const push = (t, v) =>
pair(v, t)
class List {
static of(t) { return new List(t) }
constructor(t = empty) { this.t = t }
push(v) { return List.of(push(this.t, v)) }
toString() { return toString(this.t) }
}
const list = List.of
// main.js
const l = list().push("a").push("b").push("c")
console.log(String(l))
console.log(String(l.push("d").push("e").push("f")))
console.log(String(l))
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/406613.html
標籤:
上一篇:查找具有操作限制的最大總和
