定义一个包含学生信息的链队列
时间: 2023-05-27 13:03:18 浏览: 51
一个包含学生信息的链队列可以定义为:
```javascript
class Student {
constructor(name, age, gender, score) {
this.name = name;
this.age = age;
this.gender = gender;
this.score = score;
}
}
class Node {
constructor(data) {
this.data = data;
this.next = null;
}
}
class Queue {
constructor() {
this.front = null;
this.rear = null;
}
isEmpty() {
return this.front === null;
}
enqueue(data) {
const newNode = new Node(data);
if (this.isEmpty()) {
this.front = newNode;
this.rear = newNode;
} else {
this.rear.next = newNode;
this.rear = newNode;
}
}
dequeue() {
if (this.isEmpty()) {
return null;
} else {
const removedNode = this.front;
this.front = removedNode.next;
return removedNode.data;
}
}
display() {
let currentNode = this.front;
let queueString = '';
while (currentNode !== null) {
queueString += JSON.stringify(currentNode.data) + '\n';
currentNode = currentNode.next;
}
console.log(queueString);
}
}
```
这个链队列包含一个`Student`类来存储学生信息,一个`Node`类表示队列中的每个节点,和一个`Queue`类表示链队列。`Queue`类具有`enqueue()`方法将数据添加到队列的末尾,`dequeue()`方法从队列的前面取出数据并将其从队列中移除,`display()`方法显示队列中所有的数据。