diff --git a/Structure_LinkedList/Array_For_LinkedList.test.ts b/Structure_LinkedList/Array_For_LinkedList.test.ts new file mode 100644 index 0000000..e69de29 diff --git a/Structure_LinkedList/Array_For_LinkedList.ts b/Structure_LinkedList/Array_For_LinkedList.ts new file mode 100644 index 0000000..402001f --- /dev/null +++ b/Structure_LinkedList/Array_For_LinkedList.ts @@ -0,0 +1,23 @@ +interface LinkedList { + value: Number | null; + next: LinkedList | null; +} + +const toLinkedList = (array: Number[]): LinkedList => { + let linkedList: LinkedList = { + value: null, + next: null, + }; + + const endLength = array.length - 1; + for (let index = endLength; index >= 0; index--) { + linkedList = { + value: array[index], + next: index === endLength ? null : linkedList, + }; + } + + return linkedList; +}; + +toLinkedList([1, 2, 3, 4, 5]); diff --git a/Structure_LinkedList/LinkedList_Queue.test.ts b/Structure_LinkedList/LinkedList_Queue.test.ts new file mode 100644 index 0000000..e69de29 diff --git a/Structure_LinkedList/LinkedList_Queue.ts b/Structure_LinkedList/LinkedList_Queue.ts new file mode 100644 index 0000000..3a67ca4 --- /dev/null +++ b/Structure_LinkedList/LinkedList_Queue.ts @@ -0,0 +1,42 @@ +interface LinkedList { + value: number | null; + next: LinkedList | null; +} + +class LinkedListQueue { + private head: LinkedList | null = null; + private tail: LinkedList | null = null; + + private len: number = 0; + + add(value: number) { + if (this.len === 0) { + this.head = { value, next: null }; + this.tail = this.head; + this.len++; + return; + } + + this.tail!.next = { value, next: null }; + this.tail = this.tail!.next; + this.len++; + } + + pop(): number | null { + if (!this.head || !this.tail) return null; + + const popValue = this.head.value; + this.head = this.head.next; + this.len--; + + return popValue; + } + + get length() { + return this.len; + } + + get link() { + return this.head; + } +} \ No newline at end of file diff --git a/Structure_LinkedList/Reverse_LinkedList.test.ts b/Structure_LinkedList/Reverse_LinkedList.test.ts new file mode 100644 index 0000000..e69de29 diff --git a/Structure_LinkedList/Reverse_LinkedList.ts b/Structure_LinkedList/Reverse_LinkedList.ts new file mode 100644 index 0000000..e95c998 --- /dev/null +++ b/Structure_LinkedList/Reverse_LinkedList.ts @@ -0,0 +1,22 @@ +interface LinkedList { + value: Number | null; + next: LinkedList | null; +} + +const reverseLinkedList = (LinkedList: LinkedList) => { + let preNode: LinkedList | null = null; + let curNode: LinkedList | null = LinkedList; + let nexNode: LinkedList | null = LinkedList.next; + + while (curNode) { + curNode.next = preNode; + + preNode = curNode; + + if (nexNode == null) break; + curNode = nexNode; + nexNode = nexNode.next; + } + + return curNode; +}; diff --git a/Structure_Queue/Stack_Queue.test.ts b/Structure_Queue/Stack_Queue.test.ts new file mode 100644 index 0000000..e69de29 diff --git a/Structure_Queue/Stack_Queue.ts b/Structure_Queue/Stack_Queue.ts new file mode 100644 index 0000000..8443c02 --- /dev/null +++ b/Structure_Queue/Stack_Queue.ts @@ -0,0 +1,55 @@ +/** + * @description 使用两个栈实现一个队列 + * @date 2022-05-11 + */ + +export class StackQueue { + private stack1: number[] = []; + private stack2: number[] = []; + + add(value: number) { + this.stack1.push(value); + } + + popup() { + if (!this.stack1.length) return; + + while (this.stack1.length) { + const stackValue = this.stack1.pop(); + this.stack2.push(stackValue!); + } + + const popValue = this.stack2.pop(); + + while (this.stack2.length) { + const stackValue = this.stack2.pop(); + this.stack1.push(stackValue!); + } + + return popValue; + } + + get length() { + return this.stack1.length; + } +} + +// console.time("StackQueue"); +// const queue = new StackQueue(); +// for (let index = 0; index < 10000; index++) { +// queue.add(index); +// } +// for (let index = 0; index < 10000; index++) { +// queue.popup(); +// } +// console.timeEnd("StackQueue"); + +// console.time("ArrayQueue"); +// const ArrayQueue = []; +// for (let index = 0; index < 10000; index++) { +// ArrayQueue.push(index); +// } +// for (let index = 0; index < 10000; index++) { +// ArrayQueue.shift(); +// } +// console.timeEnd("ArrayQueue");