如何在 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 的接口中所有栈方法及其参数和返回类型。我们将在栈类中实现上述接口。
步骤 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
});
}
在上面的代码中,我们遍历 data 数组并打印所有数组值。
示例
在下面的示例中,我们创建了 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 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
在本教程中,我们学习了如何从头开始创建栈。我们已经从头开始实现了栈的各种方法。此外,用户可以创建任何数据类型的栈。甚至用户可以创建“any”数据类型的栈并添加多种数据类型的元素。
数据结构
网络
关系数据库管理系统(RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP