list-structure
Using ES6 to build linked lists, stacks, queues and other structures. Need CommonJS module support.
使用 ES6 构建的链表,栈,队列等结构,需要 CommonJS 模块的支持。
structures:
insert:
npm install --save list-structure# or yarn add list-structure
test:
git clone https://github.com/Lizhooh/list-structure.gitnpm installnpm test
LinkedList
API
function | return | explain |
---|---|---|
size() |
Number | |
append(element) |
LinkedList | |
prepend(element) |
LinkedList | |
forEach((item, index)) |
LinkedList | |
sort(compareFunction) |
LinkedList | |
remove(index) |
Object | |
toArray() |
Array | |
from(arr) |
LinkedList | |
isEmpty() |
Boolean | |
clear() |
LinkedList | |
insert(index, element) |
Any | |
has(callback) |
Boolean | |
findIndex(callback) |
Number | |
find(callback) |
Any | |
set(index, element) |
LinkedList | |
get(index) |
Any |
example
const LinkedList = ; const list1 = ;list1;console; // ['A', 'B'] const list2 = 'C' 'D';console; // ['C', 'D'] const list3 = ;list3;console; // ['E', 'F'] const list4 = name: 'aer' id: 1 name: 'ber' id: 2 name: 'cer' id: 3 ; console; // true list4;list4;list4; list4;console; // [5, 4, 3, 2, 1] list4;console // 4 console; // { name: 'der', id: 4 }list4;console; // { name: 'fer', id: 6 } const index = list4;console; // 0
stack
Using array to implement stack structure.
API
function | return | explain |
---|---|---|
size() |
Number | |
push(element) |
Stack | |
pop() |
Any | |
peek() |
Any | |
forEach((item, index)) |
Stack | |
toArray() |
Array | |
from(arr) |
Stack | |
isEmpty() |
Boolean | |
clear() |
Stack | |
has(callback) |
Boolean |
example
const Stack = ; const stack1 = ;stack1;stack1; // ['A', 'B', 'C'] stack1; // 'C'stack1; // 'B' const stack2 = id: 1 id: 2 ; stack2;stack2size; // 4 stack2; // truestack2; // false
queue
Using array to implement queue structure.
API
function | return | explain |
---|---|---|
size() |
Number | |
push(element) |
Queue | |
pop() |
Any | |
front() |
Any | |
back() |
Any | |
forEach((item, index)) |
Queue | |
toArray() |
Array | |
from(arr) |
Queue | |
isEmpty() |
Boolean | |
clear() |
Queue | |
has(callback) |
Boolean |
example
const Queue = ;const queue = ; queue;queue; queue; // 'A'queue; // 'D'queue; // 'A'queue; // truequeuesize; // 3queue; // ['B', 'C', 'D']