如何使用数组在 TypeScript 中创建队列?

typescriptserver side programmingprogramming

在本教程中,我们将学习使用 TypeScript 中的数组从头开始创建队列。

队列是一种数据结构,允许用户从末尾添加元素并从开头删除元素。这意味着它基于 FIFO 概念工作,即先进先出。

此外,我们不能像数组一样随机从队列中删除元素。我们只能从第一个索引中删除元素,并将它们添加到最后一个空索引中。

在这里,我们将使用面向对象编程语言的一些概念,使用数组创建队列。

Queue 的方法

用户可以在下面了解我们将在 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;
   // 其他方法,如果用户想为队列添加新方法
}

我们在上面的语法中使用了 interface 关键字来创建接口。该接口包含 enQueue、deQueue、isEmpty()、isFUll() 等方法的结构。

步骤 2 − 现在,创建一个名为 QueueClass 的类来实现queueInterface。

class QueueClass<Type> implements queueInterface<Type> {
   // 实现队列方法和变量
}

步骤 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()) {
      // 队列已满
   } else {
      // 队列还有一些空间
      this.QueueData.push(dataItem);
   }
}

在上面的代码中,我们首先使用 isFull() 方法检查队列是否有空间。之后,我们使用数组的 push() 方法将元素推送到队列中。

步骤 6 - 实现 deQueue() 方法。

deQueue(): Type | undefined {
   if (this.isEmpty()) {
      // 队列包含零个元素
      return;
   } else {
      // 队列中有多个元素
      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 的不同方法对 Queue 执行各种操作。

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();

输出

The above code will produce the following output −

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 中从头创建 Queue。用户可以使用 Queue 的不同方法,也可以根据需要为 QueueClass 实现其他方法。


相关文章