如何使用数组在 TypeScript 中创建堆栈?
堆栈是一种基于 LIFO 的数据结构,即后进先出。简而言之,它表示您在堆栈中添加的任何元素都是最先从堆栈中出来的。
用户可以在堆栈上执行一些基本操作。例如,我们可以将元素推送到堆栈,从堆栈中弹出元素,或者从堆栈中查看元素。
在这里,用户可以看到堆栈的基本方法,我们也将在本教程中创建堆栈时实现这些方法。
堆栈方法
Push() - 它允许我们将元素添加到堆栈中。
Pop() - 它允许从堆栈中删除最后一个元素。
Peek() - 它从堆栈中返回顶部元素而不将其删除。
isEmpty() - 它根据堆栈是否为空返回布尔值。
isFull() - 它根据堆栈是否为空返回 true 或 false 值完整。
Size() − size() 方法返回表示堆栈大小的数值。
printStack() − printStack() 方法打印所有堆栈值。
实现堆栈类
现在,我们将使用 TypeScript 中的类和接口实现堆栈。用户可以按照以下步骤创建堆栈。
步骤 1 − 创建一个包含所有堆栈方法的接口。
interface stackInterface<dataType> { push(dataItem: dataType): void; pop(): dataType | null | undefined; peek(): dataType | null; isEmpty(): boolean; isFull(): boolean; size(): number; printStack(): void; }
在上面的代码中,用户可以看到我们已经在 stackInterface 命名接口中声明了所有堆栈方法及其参数和返回类型。我们将在 stack 类中实现上述接口。
步骤 2 − 现在,我们需要创建一个 StackClass 并定义上述接口中声明的所有方法。此外,创建 data 和 stackSize 私有成员分别存储堆栈元素和堆栈的最大大小。
步骤 3 − 之后,创建一个构造函数来初始化堆栈的最大大小,如下所示。
constructor(size: number) { this.stackSize = size; }
步骤 4 − 现在,我们需要为堆栈实现 push() 方法。
push(dataItem: dataType): void { if (this.data.length === this.stackSize) { // throw error } this.data.push(dataItem); }
在上面的代码中,我们检查堆栈是否已满。如果堆栈已满,则抛出错误;否则,使用 Array.push() 方法将元素推送到数组。
步骤 5 − 接下来,为堆栈实现 pop() 方法。
pop(): dataType | null | undefined { return this.data.pop(); }
在上面的代码中,我们使用 Array.pop() 方法从数组的最后一个索引中删除元素。
步骤 6 − 接下来,在 StackClass 中实现 peek() 方法。
peek(): dataType | null { return this.data[this.size() - 1]; }
在上面的代码中,我们访问数组的最后一个元素并返回它。
步骤 7 − 为 StackClass 实现 isEmpty() 方法。
isEmpty(): boolean { return this.data.length <= 0; }
我们使用数组的 length 方法来检查堆栈是否为空。
步骤 8 − 用户可以按照以下语法定义 isFull() 方法。
isFull(): boolean { let result = this.data.length >= this.stackSize; return result; }
我们比较了数组的长度和 stackSize 属性的值来检查堆栈是否已满。
第 9 步 - 用户可以使用以下代码为 StackClass 实现 size() 方法。
size(): number { return this.data.length; }
堆栈大小与数组大小类似,要获取数组的大小,我们可以使用 length 属性。
第 10 步 - 接下来,定义 printStack() 方法来打印堆栈的元素。
printStack(): void { this.data.forEach((dataItem) => { // print dataItem }); }
在上面的代码中,我们遍历数据数组并打印所有数组值。
示例
在下面的示例中,我们创建了 StackClass,它实现了 stackInterface。此外,用户可以看到如何使用自定义数据类型创建堆栈。
下面示例的 StackClass 包含上述所有方法的实现。之后,我们创建了 StackClass 的对象并使用数字作为数据类型。之后,我们使用 StackClass 的各种方法对堆栈执行不同的操作。
interface stackInterface<dataType> { push(dataItem: dataType): void; pop(): dataType | null | undefined; peek(): dataType | null; isEmpty(): boolean; isFull(): boolean; size(): number; printStack(): void; } class StackClass<dataType> implements stackInterface<dataType> { private data: Array<dataType> = []; private stackSize: number = 0; constructor(size: number) { this.stackSize = size; } push(dataItem: dataType): void { if (this.data.length === this.stackSize) { throw Error( "You can't add more elements to stack as stack storage is full!" ); } this.data.push(dataItem); } pop(): dataType | null | undefined { let element = this.data.pop(); return element; } peek(): dataType | null { let element = this.data[this.size() - 1]; return element; } isEmpty(): boolean { let result = this.data.length <= 0; return result; } isFull(): boolean { let result = this.data.length >= this.stackSize; return result; } size(): number { let len = this.data.length; return len; } printStack(): void { console.log("All stack values are printed below!"); this.data.forEach((dataItem) => { console.log(dataItem); }); } } const newstack = new StackClass<number>(5); newstack.push(10); newstack.push(12); newstack.push(897); newstack.push(54); newstack.push(14); console.log("The peek element of the stack is " + newstack.peek()); console.log("Is stack full? " + newstack.isFull()); console.log("The last removed element from the stack is " + newstack.pop()); console.log("The size of the stack is " + newstack.size()); console.log("Is stack empty? " + newstack.isEmpty()); newstack.printStack();
编译后,它将生成以下 JavaScript 代码 -
var StackClass = /** @class */ (function () { function StackClass(size) { this.data = []; this.stackSize = 0; this.stackSize = size; } StackClass.prototype.push = function (dataItem) { if (this.data.length === this.stackSize) { throw Error("You can't add more elements to stack as stack storage is full!"); } this.data.push(dataItem); }; StackClass.prototype.pop = function () { var element = this.data.pop(); return element; }; StackClass.prototype.peek = function () { var element = this.data[this.size() - 1]; return element; }; StackClass.prototype.isEmpty = function () { var result = this.data.length <= 0; return result; }; StackClass.prototype.isFull = function () { var result = this.data.length >= this.stackSize; return result; }; StackClass.prototype.size = function () { var len = this.data.length; return len; }; StackClass.prototype.printStack = function () { console.log("All stack values are printed below!"); this.data.forEach(function (dataItem) { console.log(dataItem); }); }; return StackClass; }()); var newstack = new StackClass(5); newstack.push(10); newstack.push(12); newstack.push(897); newstack.push(54); newstack.push(14); console.log("The peek element of the stack is " + newstack.peek()); console.log("Is stack full? " + newstack.isFull()); console.log("The last removed element from the stack is " + newstack.pop()); console.log("The size of the stack is " + newstack.size()); console.log("Is stack empty? " + newstack.isEmpty()); newstack.printStack();
输出
The above code will produce the following output −
The peek element of the stack is 14 Is stack full? true The last removed element from the stack is 14 The size of the stack is 4 Is stack empty? false All stack values are printed below! 10 12 897 54
在本教程中,我们学习了如何从头开始创建堆栈。我们从头开始实现了堆栈的各种方法。此外,用户可以创建任何数据类型的堆栈。用户甚至可以创建"任何"数据类型的堆栈并添加多种数据类型的元素。