Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

第 62 期(数据结构-队列):队列和队列的模拟 #65

Open
wingmeng opened this issue Jul 18, 2019 · 0 comments
Open

第 62 期(数据结构-队列):队列和队列的模拟 #65

wingmeng opened this issue Jul 18, 2019 · 0 comments

Comments

@wingmeng
Copy link
Collaborator

概念:
队列和栈十分相似,他们都是线性表,元素都是有序的,不同之处是,队列遵循的是 FIFO(先进先出)的原则。
队列从尾部添加新元素,从顶部移除元素,最新添加的元素排在队列的末尾。

理解:
队列在生活中最常见的例子就是排队了,先进入队列的先接受服务,后进入队列的排在队列后面。

队列的模拟:

class Queue {
  constructor() {
    this.item = [];
  }

  // 添加一个(或几个)新元素到队列尾部
  enqueue(element) {
    items.push(element);
  }

  // 移除队列的第一个元素,并返回被移除的元素
  dequeue() {
    return items.shift();
  }

  // 返回队列第一个元素,但不对队列做修改
  front() {
    return items[0];
  }

  // 判断是否为空队列(没有任何元素)
  isEmpty() {
    return items.length === 0;
  }

  // 返回队列里的元素个数
  size() {
    return items.length;
  }

  // 清空队列
  clear() {
    items = [];
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant