class RecentFileCircularQueue {
constructor(capacity = 5) {
this.capacity = capacity;
this.queue = new Array(capacity);
this.head = 0;
this.tail = 0;
this.count = 0;
}
enqueue(file) {
for (let i = 0; i < this.count; i++) {
const index = (this.head + i) % this.capacity;
if (this.queue[index] === file) {
this.deleteFile(file);
break;
}
}
if (this.count === this.capacity) {
this.dequeue();
}
this.queue[this.tail] = file;
this.tail = (this.tail + 1) % this.capacity;
this.count++;
}
dequeue() {
if (this.isEmpty()) {
return null;
}
const file = this.queue[this.head];
this.queue[this.head] = null;
this.head = (this.head + 1) % this.capacity;
this.count--;
return file;
}
deleteFile(file) {
for (let i = 0; i < this.count; i++) {
const index = (this.head + i) % this.capacity;
if (this.queue[index] === file) {
for (let j = i; j < this.count - 1; j++) {
const current_index = (this.head + j) % this.capacity;
const next_index = (this.head + j + 1) % this.capacity;
this.queue[current_index] = this.queue[next_index];
}
this.queue[(this.head + this.count - 1) % this.capacity] = null;
this.tail = (this.tail - 1 + this.capacity) % this.capacity;
this.count--;
break;
}
}
}
contains(file) {
for (let i = 0; i < this.count; i++) {
const index = (this.head + i) % this.capacity;
if (this.queue[index] === file) {
return true;
}
}
return false;
}
isEmpty() {
return this.count === 0;
}
isFull() {
return this.count === this.capacity;
}
getFiles() {
const files = [];
for (let i = 0; i < this.count; i++) {
const index = (this.head + i) % this.capacity;
files.push(this.queue[index]);
}
return files;
}
}
class RecentFileStack {
constructor(capacity = 5) {
this.capacity = capacity;
this.stack = [];
}
push(file) {
if (this.stack.includes(file)) {
this.stack.splice(this.stack.indexOf(file), 1);
}
if (this.stack.length === this.capacity) {
this.pop();
}
this.stack.push(file);
}
pop() {
if (this.isEmpty()) {
return null;
}
return this.stack.shift();
}
deleteFile(file) {
const index = this.stack.indexOf(file);
if (index !== -1) {
this.stack.splice(index, 1);
}
}
contains(file) {
return this.stack.includes(file);
}
isEmpty() {
return this.stack.length === 0;
}
isFull() {
return this.stack.length === this.capacity;
}
getFiles() {
return [...this.stack];
}
}
class RecentFileList {
constructor(capacity = 5) {
this.capacity = capacity;
this.list = [];
}
addFile(file) {
const index = this.list.indexOf(file);
if (index !== -1) {
this.list.splice(index, 1);
}
this.list.unshift(file);
if (this.list.length > this.capacity) {
this.list.pop();
}
}
removeFile() {
if (this.isEmpty()) {
return null;
}
return this.list.pop();
}
deleteFile(file) {
const index = this.list.indexOf(file);
if (index !== -1) {
this.list.splice(index, 1);
}
}
contains(file) {
return this.list.includes(file);
}
isEmpty() {
return this.list.length === 0;
}
isFull() {
return this.list.length === this.capacity;
}
getFiles() {
return [...this.list];
}
}
// 示例使用 RecentFileCircularQueue
const circularQueue = new RecentFileCircularQueue();
circularQueue.enqueue("file1.txt");
circularQueue.enqueue("file2.txt");
circularQueue.enqueue("file3.txt");
circularQueue.enqueue("file4.txt");
circularQueue.enqueue("file5.txt");
console.log("循环队列 - 最近打开的文件列表:", circularQueue.getFiles());
circularQueue.enqueue("file6.txt");
console.log("循环队列 - 添加新文件后:", circularQueue.getFiles());
circularQueue.deleteFile("file3.txt");
console.log("循环队列 - 删除文件3后:", circularQueue.getFiles());
console.log("循环队列 - 判断文件4是否存在:", circularQueue.contains("file4.txt"));
// 示例使用 RecentFileStack
const stack = new RecentFileStack();
stack.push("file1.txt");
stack.push("file2.txt");
stack.push("file3.txt");
stack.push("file4.txt");
stack.push("file5.txt");
console.log("栈 - 最近打开的文件列表:", stack.getFiles());
stack.push("file6.txt");
console.log("栈 - 添加新文件后:", stack.getFiles());
stack.deleteFile("file3.txt");
console.log("栈 - 删除文件3后:", stack.getFiles());
console.log("栈 - 判断文件4是否存在:", stack.contains("file4.txt")); // true
// 示例使用 RecentFileList
const fileList = new RecentFileList();
fileList.addFile("file1.txt");
fileList.addFile("file2.txt");
fileList.addFile("file3.txt");
fileList.addFile("file4.txt");
fileList.addFile("file5.txt");
console.log("线性表 - 最近打开的文件列表:", fileList.getFiles());
fileList.addFile("file6.txt");
console.log("线性表 - 添加新文件后:", fileList.getFiles());
fileList.deleteFile("file3.txt");
console.log("线性表 - 删除文件3后:", fileList.getFiles());
console.log("线性表 - 判断文件4是否存在:", fileList.contains("file4.txt"));
循环队列 - 最近打开的文件列表: [ 'file1.txt', 'file2.txt', 'file3.txt', 'file4.txt', 'file5.txt' ]
循环队列 - 添加新文件后: [ 'file2.txt', 'file3.txt', 'file4.txt', 'file5.txt', 'file6.txt' ]
循环队列 - 删除文件3后: [ 'file2.txt', 'file4.txt', 'file5.txt', 'file6.txt' ]
循环队列 - 判断文件4是否存在: true
栈 - 最近打开的文件列表: [ 'file1.txt', 'file2.txt', 'file3.txt', 'file4.txt', 'file5.txt' ]
栈 - 添加新文件后: [ 'file2.txt', 'file3.txt', 'file4.txt', 'file5.txt', 'file6.txt' ]
栈 - 删除文件3后: [ 'file2.txt', 'file4.txt', 'file5.txt', 'file6.txt' ]
栈 - 判断文件4是否存在: true
线性表 - 最近打开的文件列表: [ 'file5.txt', 'file4.txt', 'file3.txt', 'file2.txt', 'file1.txt' ]
线性表 - 添加新文件后: [ 'file6.txt', 'file5.txt', 'file4.txt', 'file3.txt', 'file2.txt' ]
线性表 - 删除文件3后: [ 'file6.txt', 'file5.txt', 'file4.txt', 'file2.txt' ]
线性表 - 判断文件4是否存在: true