Browse Source

栈和链表

master
YuJian 3 years ago
parent
commit
70c4e193b5
  1. 0
      Structure_LinkedList/Array_For_LinkedList.test.ts
  2. 23
      Structure_LinkedList/Array_For_LinkedList.ts
  3. 0
      Structure_LinkedList/LinkedList_Queue.test.ts
  4. 42
      Structure_LinkedList/LinkedList_Queue.ts
  5. 0
      Structure_LinkedList/Reverse_LinkedList.test.ts
  6. 22
      Structure_LinkedList/Reverse_LinkedList.ts
  7. 0
      Structure_Queue/Stack_Queue.test.ts
  8. 55
      Structure_Queue/Stack_Queue.ts

0
Structure_LinkedList/Array_For_LinkedList.test.ts

23
Structure_LinkedList/Array_For_LinkedList.ts

@ -0,0 +1,23 @@ @@ -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]);

0
Structure_LinkedList/LinkedList_Queue.test.ts

42
Structure_LinkedList/LinkedList_Queue.ts

@ -0,0 +1,42 @@ @@ -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;
}
}

0
Structure_LinkedList/Reverse_LinkedList.test.ts

22
Structure_LinkedList/Reverse_LinkedList.ts

@ -0,0 +1,22 @@ @@ -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;
};

0
Structure_Queue/Stack_Queue.test.ts

55
Structure_Queue/Stack_Queue.ts

@ -0,0 +1,55 @@ @@ -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");
Loading…
Cancel
Save