如何使用数组在 TypeScript 中创建队列?
在本教程中,我们将学习如何使用 TypeScript 中的数组从头开始创建队列。
队列是一种数据结构,允许用户从末尾添加元素并从开头删除元素。这意味着它基于 FIFO 概念工作,即先进先出。
此外,我们不能像数组一样随机地从队列中删除元素。我们只能从第一个索引删除元素,并将它们添加到最后一个空索引中。
在这里,我们将使用一些面向对象编程语言的概念来使用数组创建队列。
队列的方法
用户可以在下面了解我们将在 Queue 类中实现的方法。
enQueue() − enQueue() 方法将元素添加到队列中。
deQueue() − 它从队列中删除元素。
Size () − 它返回队列的长度,表示队列中元素的总数。
isFull() − 如果队列已满,则返回 true;否则返回 false。
isEmpty() − 如果队列不包含任何元素,则返回 true;否则返回 false。
printeQueue() − 它打印队列的所有元素。
使用类和数组实现队列
用户可以按照以下步骤使用类和数组创建队列并实现上述方法。
步骤 1 − 首先,我们需要为队列类创建一个接口,为队列类准备一个结构。
interface queueInterface<Type> {
enQueue(dataItem: Type): void;
deQueue(): Type | undefined;
isEmpty(): boolean;
isFull(): boolean;
size(): number;
printQueue(): void;
// other methods, if users wanted to add new methods for the Queue
}
我们在上面的语法中使用了 interface 关键字来创建接口。该接口包含 enQueue、deQueue、isEmpty()、isFUll() 等方法的结构。
步骤 2 − 现在,创建一个名为 QueueClass 的类,实现 queueInterface。
class QueueClass<Type> implements queueInterface<Type> {
// implement queue methods and variables
}
步骤 3 − 我们需要为在 queueInterface 中声明的队列实现所有方法。
步骤 4 − 实现 isEmpty() 方法和 isFull() 方法以检查队列是否为空或已满。
isEmpty(): boolean {
return this.QueueData.length <= 0;
}
isFull(): boolean {
return this.QueueData.length >= this.maxSize;
}
在上面的代码中,我们使用了数组的 length 属性来检查队列是否已满或为空。
步骤 5 − 实现 enQueue() 方法。
enQueue(dataItem: Type): void {
if (this.isFull()) {
// queue is full
} else {
// Queue has some space
this.QueueData.push(dataItem);
}
}
在上面的代码中,我们首先使用 isFull() 方法检查队列是否有空间。之后,我们使用数组的 push() 方法将元素推入队列。
步骤 6 − 实现 deQueue() 方法。
deQueue(): Type | undefined {
if (this.isEmpty()) {
// Queue contains zero elements
return;
} else {
// Queue has more than zero elements
return this.QueueData.shift();
}
}
在上面的代码中,我们检查队列是否为空,并使用数组的 shift() 方法从第一个索引删除元素。
步骤 7 − 实现 size() 和 printQueue() 方法。
size(): number {
return this.QueueData.length;
}
printQueue(): void {
for (let i = 0; i < this.QueueData.length; i++) {
console.log(this.QueueData[i]);
}
}
}
我们使用了数组的 length 属性来获取队列的大小。在 printQueue() 方法中,我们使用 for 循环打印所有队列数据。
示例
下面的示例包含 QueueClass 的实现。我们在 QueueClass 中实现了所有上述方法。
QueueClass 包含初始化队列类最大大小的构造函数。我们创建了 QueueClass 的对象,并使用了 QueueClass 的不同方法对队列执行各种操作。
interface queueInterface<Type> {
enQueue(dataItem: Type): void;
deQueue(): Type | undefined;
isEmpty(): boolean;
isFull(): boolean;
size(): number;
printQueue(): void;
}
class QueueClass<Type> implements queueInterface<Type> {
private QueueData: Array<Type> = [];
private maxSize: number = 0;
constructor(length: number) {
this.maxSize = length;
}
isEmpty(): boolean {
let result = this.QueueData.length <= 0;
return result;
}
isFull(): boolean {
let result = this.QueueData.length >= this.maxSize;
return result;
}
enQueue(dataItem: Type): void {
if (this.isFull()) {
console.log("The queue is full!");
} else {
this.QueueData.push(dataItem);
}
}
deQueue(): Type | undefined {
if (this.isEmpty()) {
console.log("The Queue is empty! There is no element to pop-out");
return;
} else {
var element = this.QueueData.shift();
return element;
}
}
size(): number {
let len = this.QueueData.length;
return len;
}
printQueue(): void {
for (let i = 0; i < this.QueueData.length; i++) {
console.log(this.QueueData[i]);
}
}
}
const testQueue = new QueueClass<string>(3);
testQueue.enQueue("JavaScript");
testQueue.enQueue("typeScript");
testQueue.enQueue("TutorialsPoint");
console.log("Is Queue full? " + testQueue.isFull());
console.log("The last remove element from the queue is " + testQueue.deQueue());
console.log("The size of the Queue is " + testQueue.size());
console.log("Is Queue empty? " + testQueue.isEmpty());
testQueue.printQueue();
编译后,它将生成以下 JavaScript 代码:
var QueueClass = /** @class */ (function () {
function QueueClass(length) {
this.QueueData = [];
this.maxSize = 0;
this.maxSize = length;
}
QueueClass.prototype.isEmpty = function () {
var result = this.QueueData.length <= 0;
return result;
};
QueueClass.prototype.isFull = function () {
var result = this.QueueData.length >= this.maxSize;
return result;
};
QueueClass.prototype.enQueue = function (dataItem) {
if (this.isFull()) {
console.log("The queue is full!");
}
else {
this.QueueData.push(dataItem);
}
};
QueueClass.prototype.deQueue = function () {
if (this.isEmpty()) {
console.log("The Queue is empty! There is no element to pop-out");
return;
}
else {
var element = this.QueueData.shift();
return element;
}
};
QueueClass.prototype.size = function () {
var len = this.QueueData.length;
return len;
};
QueueClass.prototype.printQueue = function () {
for (var i = 0; i < this.QueueData.length; i++) {
console.log(this.QueueData[i]);
}
};
return QueueClass;
}());
var testQueue = new QueueClass(3);
testQueue.enQueue("JavaScript");
testQueue.enQueue("typeScript");
testQueue.enQueue("TutorialsPoint");
console.log("Is Queue full? " + testQueue.isFull());
console.log("The last remove element from the queue is " + testQueue.deQueue());
console.log("The size of the Queue is " + testQueue.size());
console.log("Is Queue empty? " + testQueue.isEmpty());
testQueue.printQueue();
输出
以上代码将产生以下输出:
Is Queue full? true The last remove element from the queue is JavaScript The size of the Queue is 2 Is Queue empty? falsetypeScript TutorialsPoint
在以上输出中,用户可以观察到 deQueue() 方法删除了我们首先添加到队列中的元素。
我们学习了如何在 TypeScript 中从头开始创建队列。用户可以使用队列的不同方法,还可以根据需要为 QueueClass 实现其他方法。
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP