mirror of
https://github.moeyy.xyz/https://github.com/trekhleb/javascript-algorithms.git
synced 2024-12-26 07:01:18 +08:00
Add LinkedList.
This commit is contained in:
parent
909976c7bf
commit
53ca279c3b
@ -1,4 +1,4 @@
|
|||||||
import Node from './Node';
|
import LinkedListNode from './LinkedListNode';
|
||||||
|
|
||||||
export default class LinkedList {
|
export default class LinkedList {
|
||||||
constructor() {
|
constructor() {
|
||||||
@ -6,7 +6,7 @@ export default class LinkedList {
|
|||||||
}
|
}
|
||||||
|
|
||||||
append(value) {
|
append(value) {
|
||||||
const newNode = new Node(value);
|
const newNode = new LinkedListNode(value);
|
||||||
|
|
||||||
// If there is no head yet let's make new node a head.
|
// If there is no head yet let's make new node a head.
|
||||||
if (!this.head) {
|
if (!this.head) {
|
||||||
@ -28,8 +28,8 @@ export default class LinkedList {
|
|||||||
}
|
}
|
||||||
|
|
||||||
prepend(value) {
|
prepend(value) {
|
||||||
const newNode = new Node(value);
|
const newNode = new LinkedListNode(value, this.head);
|
||||||
this.head = new Node(value, this.head);
|
this.head = newNode;
|
||||||
|
|
||||||
return newNode;
|
return newNode;
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
export default class Node {
|
export default class LinkedListNode {
|
||||||
constructor(value, next = null) {
|
constructor(value, next = null) {
|
||||||
this.value = value;
|
this.value = value;
|
||||||
this.next = next;
|
this.next = next;
|
@ -1,8 +1,8 @@
|
|||||||
import Node from '../Node';
|
import LinkedListNode from '../LinkedListNode';
|
||||||
|
|
||||||
describe('Node', () => {
|
describe('LinkedListNode', () => {
|
||||||
it('should create list node with value', () => {
|
it('should create list node with value', () => {
|
||||||
const node = new Node(1);
|
const node = new LinkedListNode(1);
|
||||||
expect(node.value).toBe(1);
|
expect(node.value).toBe(1);
|
||||||
expect(node.next).toBeNull();
|
expect(node.next).toBeNull();
|
||||||
});
|
});
|
Loading…
Reference in New Issue
Block a user