npm.io
2.0.0 • Published 8 years ago

list-structure

Licence
ISC
Version
2.0.0
Deps
0
Size
120 kB
Vulns
0
Weekly
0
Stars
1

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.git
npm install
npm 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 } = require('list-structure');

const list1 = new LinkedList();
list1.append('A').append('B');
console.log(list1.toArray());   // ['A', 'B']

const list2 = new LinkedList(['C', 'D']);
console.log(list2.toArray());   // ['C', 'D']

const list3 = new LinkedList();
list3.from(['E', 'F']);
console.log(list3.toArray());   // ['E', 'F']

const list4 = new LinkedList([
    { name: 'aer', id: 1 },
    { name: 'ber', id: 2 },
    { name: 'cer', id: 3 },
]);

console.log(list4.has(i => i.name === 'aer')); // true

list4.prepend({ name: 'der', id: 4 });
list4.insert(2, { name: 'eer', id: 5 });
list4.forEach(i => console.log(i));

list4.sort((a, b) => b.id - a.id);
console.log(list4.toArray().map(i => i.id));  // [5, 4, 3, 2, 1]

list4.remove(2);
console.log(list4.length)        // 4

console.log(list4.get(0));       // { name: 'der', id: 4 }
list4.set(0, { name: 'fer', id: 6 });
console.log(list4.get(0));       // { name: 'fer', id: 6 }

const index = list4.findIndex(i => i.id === 6);
console.log(index);              // 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 } = require('list-structure');

const stack1 = new Stack();
stack1.push('A').push('B').push('C');
stack1.toArray();               // ['A', 'B', 'C']

stack1.pop();                   // 'C'
stack1.peek();                  // 'B'

const stack2 = new Stack([{ id: 1 }, { id: 2 }]);

stack2.from([{ id: 3 }, { id: 4 }]);
stack2.size();                  // 4

stack2.has(i => i.id === 3);    // true
stack2.has(i => i.id === 5);    // 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 } = require('list-structure');
const queue = new Queue();

queue.push('A');
queue.from(['B', 'C', 'D']);

queue.front();             // 'A'
queue.back();              // 'D'
queue.pop();               // 'A'
queue.has(i => i === 'D'); // true
queue.size();              // 3
queue.toArray();           // ['B', 'C', 'D']