Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add deque for JavaScript and typescript #267

Merged
merged 9 commits into from
Feb 4, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 16 additions & 79 deletions codes/javascript/chapter_stack_and_queue/deque.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,97 +4,34 @@
* Author: Zhuo Qinyue (1403450829@qq.com)
*/

/* 基于数组方法实现的双端队列 */
class Deque {
array;
len;
constructor() {
this.len = 0;
this.array = [];
}

/* 在队尾添加元素 */
pushBack(value) {
this.array.push(value);
this.len++;
}

/* 在队首添加元素 */
pushFront(value) {
this.array.unshift(value);
this.len++;
}

/* 删除队尾元素 */
popBack() {
this.len--;
return this.array.pop();
}

/* 删除队首元素 */
popFront() {
this.len--;
return this.array.shift();
}

/* 访问队尾元素 */
peekLast() {
return this.array[this.len - 1];
}

/* 访问队首元素 */
peekFirst() {
return this.array[0];
}

/* 判断队列是否为空 */
isEmpty() {
return this.len === 0;
}

/* 获取队列的长度 */
size() {
return this.len;
}

/* 打印队列 */
printDeque() {
const dequeStr = this.array.join(', ')
console.log(dequeStr);
};

}

/* 初始化双向队列 */
const deque = new Deque();
/* JavaScript 没有内置的双端队列,可以把 Array 当作双端队列来使用 */
const deque = [];

/* 元素入队 */
deque.pushBack(2);
deque.pushBack(5);
deque.pushBack(4);
deque.pushFront(3);
deque.pushFront(1);
console.log("双向队列 deque = ");
deque.printDeque();
deque.push(2);
deque.push(5);
deque.push(4);
deque.unshift(3);
deque.unshift(1);
console.log("双向队列 deque = ", deque);

/* 访问元素 */
const peekFirst = deque.peekFirst();
const peekFirst = deque[0];
console.log("队首元素 peekFirst = " + peekFirst);
const peekLast = deque.peekLast();
const peekLast = deque[deque.length - 1];
console.log("队尾元素 peekLast = " + peekLast);

/* 元素出队 */
const popFront = deque.popFront();
console.log("队首出队元素 popFront = " + popFront + ",队首出队后 deque = ");
deque.printDeque();
const popBack = deque.popBack();
console.log("队尾出队元素 popBack = " + popBack + ",队尾出队后 deque = ");
deque.printDeque();
const popFront = deque.shift();
console.log("队首出队元素 popFront = " + popFront + ",队首出队后 deque = " + deque);
const popBack = deque.pop();
console.log("队尾出队元素 popBack = " + popBack + ",队尾出队后 deque = " + deque);

/* 获取双向队列的长度 */
const size = deque.size();
const size = deque.length;
console.log("双向队列长度 size = " + size);

/* 判断双向队列是否为空 */
const isEmpty = deque.isEmpty();
const isEmpty = size === 0;
console.log("双向队列是否为空 = " + isEmpty);
97 changes: 17 additions & 80 deletions codes/typescript/chapter_stack_and_queue/deque.ts
Original file line number Diff line number Diff line change
@@ -1,102 +1,39 @@
/**
* File: deque.ts
* File: deque.js
krahets marked this conversation as resolved.
Show resolved Hide resolved
* Created Time: 2023-01-17
* Author: Zhuo Qinyue (1403450829@qq.com)
*/

/* 基于数组方法实现的双端队列 */
class Deque {
array: number[];
len: number;
constructor() {
this.len = 0;
this.array = [];
}

/* 在队尾添加元素 */
pushBack(value: number): void {
this.array.push(value);
this.len++;
}

/* 在队首添加元素, 时间复杂度: O(n) */
pushFront(value: number): void {
this.array.unshift(value);
this.len++;
}

/* 删除队尾元素 */
popBack(): number | undefined {
this.len--;
return this.array.pop();
}

/* 删除队首元素, 时间复杂度: O(n) */
popFront(): number | undefined {
this.len--;
return this.array.shift();
}

/* 访问队尾元素 */
peekLast(): number {
return this.array[this.len - 1];
}

/* 访问队首元素 */
peekFirst(): number {
return this.array[0];
}

/* 判断队列是否为空 */
isEmpty(): boolean {
return this.len === 0;
}

/* 获取队列的长度 */
size(): number {
return this.len;
}

/* 打印队列 */
printDeque(): void {
const dequeStr: string = this.array.join(', ')
console.log(dequeStr);
};

}

/* 初始化双向队列 */
const deque: Deque = new Deque();
/* JavaScript 没有内置的双端队列,可以把 Array 当作双端队列来使用 */
const deque: number[] = [];

/* 元素入队 */
deque.pushBack(2);
deque.pushBack(5);
deque.pushBack(4);
deque.pushFront(3);
deque.pushFront(1);
console.log("双向队列 deque = ");
deque.printDeque();
deque.push(2);
deque.push(5);
deque.push(4);
deque.unshift(3);
deque.unshift(1);
console.log("双向队列 deque = ", deque);

/* 访问元素 */
const peekFirst: number = deque.peekFirst();
const peekFirst: number = deque[0];
console.log("队首元素 peekFirst = " + peekFirst);
const peekLast: number = deque.peekLast();
const peekLast: number = deque[deque.length - 1];
console.log("队尾元素 peekLast = " + peekLast);

/* 元素出队 */
const popFront: number | undefined = deque.popFront();
console.log("队首出队元素 popFront = " + popFront + ",队首出队后 deque = ");
deque.printDeque();
const popBack: number | undefined = deque.popBack();
console.log("队尾出队元素 popBack = " + popBack + ",队尾出队后 deque = ");
deque.printDeque();
const popFront: number = deque.shift() as number;
console.log("队首出队元素 popFront = " + popFront + ",队首出队后 deque = " + deque);
const popBack: number = deque.pop() as number;
console.log("队尾出队元素 popBack = " + popBack + ",队尾出队后 deque = " + deque);

/* 获取双向队列的长度 */
const size: number = deque.size();
const size: number = deque.length;
console.log("双向队列长度 size = " + size);

/* 判断双向队列是否为空 */
const isEmpty: boolean = deque.isEmpty();
const isEmpty: boolean = size === 0;
console.log("双向队列是否为空 = " + isEmpty);

export { };
krahets marked this conversation as resolved.
Show resolved Hide resolved