首页 > web前端 > js教程 > 正文

后进先出还是先进先出?堆栈/队列指南

WBOY
发布: 2024-08-21 08:40:03
转载
1021人浏览过

后进先出还是先进先出?堆栈/队列指南

假设理解 big o 表示法。 javascript 中有示例。资料参考 gayle laakmann mcdowell 的《cracking the coding interview》

今天,我们将探讨两种基本的数据结构:堆栈队列。我们将深入研究它们的概念、用例,并使用经典和基于原型的方法在 javascript 中实现它们。


堆栈:后进先出 (lifo)

想象一下一堆煎饼——你最后放在上面的一个是你第一个吃的。这正是堆栈数据结构的工作原理。它遵循后进先出(lifo)原则.

关键操作

  • push(item): 将一个项目添加到堆栈顶部
  • pop():从堆栈中删除顶部项目
  • peek():返回顶部项目而不删除它
  • isempty():检查栈是否为空

使用案例

堆栈在涉及以下场景时特别有用:

  • 递归算法
  • 文本编辑器中的撤消机制

javascript 实现

经典面向对象编程

class stack {
  constructor() {
    this.items = [];
  }

  push(element) {
    this.items.push(element);
  }

  pop() {
    if (this.isempty()) {
      return "stack is empty";
    }
    return this.items.pop();
  }

  peek() {
    if (this.isempty()) {
      return "stack is empty";
    }
    return this.items[this.items.length - 1];
  }

  isempty() {
    return this.items.length === 0;
  }

  size() {
    return this.items.length;
  }

  clear() {
    this.items = [];
  }
}
登录后复制

基于原型

function stack() {
  this.items = [];
}

stack.prototype.push = function(element) {
  this.items.push(element);
};

stack.prototype.pop = function() {
  if (this.isempty()) {
    return "stack is empty";
  }
  return this.items.pop();
};

stack.prototype.peek = function() {
  if (this.isempty()) {
    return "stack is empty";
  }
  return this.items[this.items.length - 1];
};

stack.prototype.isempty = function() {
  return this.items.length === 0;
};

stack.prototype.size = function() {
  return this.items.length;
};

stack.prototype.clear = function() {
  this.items = [];
};
登录后复制

队列:先进先出 (fifo)

现在,让我们将注意力转移到队列上。与堆栈不同,队列遵循先进先出(fifo)原则。想象一下音乐会场地的排队——第一个到达的人就是第一个进入的人。

关键操作

  • enqueue(item): 将一个项目添加到队列末尾
  • dequeue():从队列中删除第一个项目
  • peek():返回第一项而不删除它
  • isempty():检查队列是否为空

使用案例

队列常用于:

  • 广度优先搜索算法
  • 任务调度

javascript 实现

经典面向对象编程

class node {
  constructor(data) {
    this.data = data;
    this.next = null;
  }
}

class queue {
  constructor() {
    this.start = null;
    this.end = null;
    this.size = 0;
  }

  enqueue(element) {
    const newnode = new node(element);
    if (this.isempty()) {
      this.start = newnode;
      this.end = newnode;
    } else {
      this.end.next = newnode;
      this.end = newnode;
    }
    this.size++;
  }

  dequeue() {
    if (this.isempty()) {
      return "queue is empty";
    }
    const removeddata = this.start.data;
    this.start = this.start.next;
    this.size--;
    if (this.isempty()) {
      this.end = null;
    }
    return removeddata;
  }

  peek() {
    if (this.isempty()) {
      return "queue is empty";
    }
    return this.start.data;
  }

  isempty() {
    return this.size === 0;
  }

  getsize() {
    return this.size;
  }

  clear() {
    this.start = null;
    this.end = null;
    this.size = 0;
  }
}
登录后复制

基于原型

function Node(data) {
  this.data = data;
  this.next = null;
}

function Queue() {
  this.start = null;
  this.end = null;
  this.size = 0;
}

Queue.prototype.enqueue = function(element) {
  const newNode = new Node(element);
  if (this.isEmpty()) {
    this.start = newNode;
    this.end = newNode;
  } else {
    this.end.next = newNode;
    this.end = newNode;
  }
  this.size++;
};

Queue.prototype.dequeue = function() {
  if (this.isEmpty()) {
    return "Queue is empty";
  }
  const removedData = this.start.data;
  this.start = this.start.next;
  this.size--;
  if (this.isEmpty()) {
    this.end = null;
  }
  return removedData;
};

Queue.prototype.peek = function() {
  if (this.isEmpty()) {
    return "Queue is empty";
  }
  return this.start.data;
};

Queue.prototype.isEmpty = function() {
  return this.size === 0;
};

Queue.prototype.getSize = function() {
  return this.size;
};

Queue.prototype.clear = function() {
  this.start = null;
  this.end = null;
  this.size = 0;
};
登录后复制

绩效分析

堆栈和队列都提供css"> o(1)o(1)o(1) 有效实现时,其核心操作(堆栈的压入/弹出、队列的入队/出队)的时间复杂度。这使得它们在特定用例中具有高性能。

它们都为许多常见的编程问题提供了优雅的解决方案,并构成了更复杂的数据结构和算法的基础。通过在 javascript 中理解和实现这些结构,您就可以很好地解决各种 leetcode/算法问题 ?.

以上就是后进先出还是先进先出?堆栈/队列指南的详细内容,更多请关注php中文网其它相关文章!

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:dev.to网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号