-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
132 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
code/linked-list/book/doubly-linked-list/doubly-linked-list.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import { defaultEquals } from '../../../utils'; | ||
import LinkedList from '../LinkedList'; | ||
import DoublyNode from './doubly-node'; | ||
|
||
export default class DoublyLinkedList<T> extends LinkedList<T> { | ||
tail: undefined | DoublyNode<T>; | ||
head: undefined | DoublyNode<T>; | ||
|
||
constructor(eqFn = defaultEquals) { | ||
super(eqFn); | ||
this.tail = undefined; | ||
} | ||
|
||
push(element: T): void { | ||
const node = new DoublyNode(element); | ||
if (this.head === undefined) { | ||
this.head = node; | ||
} else { | ||
let c = this.head; | ||
while (c.next != undefined) { | ||
c = c.next; | ||
} | ||
c.next = node; | ||
node.prev = c; | ||
} | ||
this.count++; | ||
} | ||
|
||
insert(e: T, index: number) { | ||
if (index >= 0 && index <= this.count) { | ||
const node = new DoublyNode(e); | ||
if (index == 0) { | ||
let c = this.head; | ||
node.next = c; | ||
c && (c.prev = node); | ||
this.head = node; | ||
} else if (index === this.count - 1) { | ||
let c = this.tail; | ||
node.next = c; | ||
c && (c.prev = node); | ||
this.tail = node; | ||
} else { | ||
let c = this.head; | ||
for (let i = 0; i < this.count && c !== undefined; i++) { | ||
if (i === index) { | ||
let prev = c.prev; | ||
prev && (prev.next = node); | ||
node.prev = prev; | ||
node.next = c; | ||
c.prev = node; | ||
break; | ||
} | ||
c = c?.next; | ||
} | ||
} | ||
this.count++; | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
removeAt(index: number) { | ||
if (index >= 0 && index < this.count) { | ||
let c = this.head; | ||
if (index == 0) { | ||
this.head = c?.next; | ||
if (this.count === 1) { | ||
this.tail = undefined; | ||
} else { | ||
this.head && (this.head.prev = undefined); | ||
} | ||
} else if (index === this.count - 1) { | ||
c = this.tail; | ||
this.tail = c?.prev; | ||
if (this.tail) { | ||
this.tail.next = undefined; | ||
} | ||
} else { | ||
c = this.getElementAt(index) as DoublyNode<T> | undefined; | ||
const p = c?.prev; | ||
if (p) { | ||
p.next = c?.next?.next; | ||
} | ||
if (c && c.next) { | ||
c.next.prev = p; | ||
} | ||
} | ||
this.count--; | ||
return c?.val; | ||
} | ||
return undefined; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { LinkedNode } from "../LinkedNode"; | ||
|
||
/** | ||
* 双向链表 node 节点 | ||
*/ | ||
export default class DoublyNode<T> extends LinkedNode<T> { | ||
prev: DoublyNode<T> | undefined; | ||
next: DoublyNode<T> | undefined; | ||
|
||
constructor(element: T, next: DoublyNode<T> | undefined = undefined, prev: DoublyNode<T>| undefined = undefined) { | ||
super(element, next) | ||
this.prev = prev; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import DoublyLinkedList from "../book/doubly-linked-list/doubly-linked-list"; | ||
|
||
describe('doubly linked list', () => { | ||
it('should create empty doubly linked list', () => { | ||
const linkedList = new DoublyLinkedList<number>(); | ||
expect(linkedList.toString()).toBe(''); | ||
}); | ||
|
||
it('test insert doubly linked list', () => { | ||
const linkedList = new DoublyLinkedList<number>(); | ||
linkedList.push(2); | ||
linkedList.push(5); | ||
linkedList.push(7); | ||
linkedList.insert(1, 1) | ||
expect(linkedList.toString()).toBe("2,1,5,7"); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters