美文网首页
数据结构:栈、队列、Set

数据结构:栈、队列、Set

作者: coolheadedY | 来源:发表于2017-09-18 22:16 被阅读12次

栈(Stack)

栈的特点:
先进后出,后进先出

栈的方法:
1push: 推入
2pop: 推出

示意图
https://upload.wikimedia.org/wikipedia/commons/thumb/2/29/Data_stack.svg/391px-Data_stack.svg.png

javascript实现

const Stack = function () {
  let count = 0
  let storage = {}

  this.push = function (value) {
    storage[count] = value
    count++
  }

  this.pop = function () {
    if (count === 0) return undefined

    count--
    const result = storage[count]
    delete storage[count]
    return result
  }

  this.peek = function () {
    return storage[count - 1]
  }

  this.size = function () {
    return count
  }
}

队列(queue)

队列的特点:先进先出

javascript实现

function Queue () {
  let collection = []

  this.print = function (element) {
    console.log(collection)
  }

  this.enqueue = function (element) {
    collection.push(element)
  }

  this.dequeue = function () {
    return collection.shift()
  }

  this.front = function () {
    return collection[0]
  }

  this.size = function () {
    return collection.length
  }

  this.isEmpty = function () {
    return (collection.length === 0)
  }
}

优先级队列(PriorityQueue)

优先级队列特点:根据插入元素的优先级插入指定队列位置

javascript实现

function PriorityQueue () {
  let collection = []

  this.print = function () {
    const result = collection.map(item => item[0])
    console.log(result)
  }

  this.enqueue = function (element) {
    if (this.isEmpty()) {
      collection.push(element)
    } else {
      let added = false
      for (let i = 0; i < collection.length; i++) {
        if (element[1] < collection[i][1]) {
          collection.splice(i, 0, element)
          added = true
          break // 一旦满足插入条件跳出循环
        }
      }
      if (!added) { // 优先级最小的情况插入队列
        collection.push(element)
      }
    }
  }

  this.dequeue = function () {
    const result = collection.shift()
    return result[0]
  }

  this.front = function () {
    const result = collection[0]
    return result[0]
  }

  this.size = function () {
    return collection.length
  }

  this.isEmpty = function () {
    return (collection.length === 0)
  }
}

Set数据结构

Set数据结构的特点: 不存在重复数据
Set方法:add添加, remove删除,size长度

javascript实现

const MySet = function () {
  let collection = []

  this.has = function (element) {
    return collection.indexOf(element) !== -1
  }

  this.values = function () {
    return collection
  }

  this.add = function (element) {
    if (!this.has(element)) {
      collection.push(element)
      return true
    }
    return false
  }

  this.remove = function (element) {
    if (this.has(element)) {
      let index = collection.indexOf(element)
      collection.splice(index, 1)
      return true
    }
    return false
  }

  this.size = function () {
    return collection.length
  }

  this.union = function (otherSet) { // 连接
    let unionSet = new MySet()
    let thisSetVal = this.values()
    let otherSetVal = otherSet.values()

    thisSetVal.forEach(item => unionSet.add(item))
    otherSetVal.forEach(item => unionSet.add(item))
    return unionSet
  }

  this.intersection = function (otherSet) { // 交集
    let intersectionSet = new MySet()
    let thisSetVal = this.values()

    thisSetVal.forEach(item => {
      if (otherSet.has(item)) {
        intersectionSet.add(item)
      }
    })
    return intersectionSet
  }

  // 返回thisSet不属于otherSet的值
  this.defference = function (otherSet) {
    let defferenceSet = new MySet()
    let thisSetVal = this.values()

    thisSetVal.forEach(item => {
      if (!otherSet.has(item)) {
        defferenceSet.add(item)
      }
    })
    return defferenceSet
  }

  // thisSet是否为otherSet的子集
  this.subset = function (otherSet) {
    let thisSetVal = this.values()
    return thisSetVal.every(item => {
      return otherSet.has(item)
    })
  }
}

相关文章

  • 复习

    数据结构 数据结构 集合常见数据结构:集合,链表,队列,数组,栈,映射java中:List列表,Set集合,Map...

  • 数据结构:栈、队列、Set

    栈(Stack) 栈的特点:先进后出,后进先出 栈的方法:1push: 推入2pop: 推出 示意图https:/...

  • Algorithm小白入门 -- 队列和栈

    队列和栈队列实现栈、栈实现队列单调栈单调队列运用栈去重 1. 队列实现栈、栈实现队列 队列是一种先进先出的数据结构...

  • java数据结构回顾与分析

    java中用到的主要的数据结构有 数组,list,set, map,队列,栈其实分成两类就是 数组 与 容器 1....

  • Java集合 ---总体框架及主要接口,抽象类分析

    概述 集合类作为主要的工具类,主要用于存储对象,常用的数据结构如 栈,队列,列表等。Java集合主要分为Set, ...

  • 栈和队列—什么是栈

    栈和队列是两种重要的数据结构 从数据结构角度看,栈和队列也是线性表,其特殊性在于栈和队列的基本操作是线性表操作的子...

  • 栈和队列—什么是队列

    栈和队列是两种重要的数据结构 从数据结构角度看,栈和队列也是线性表,其特殊性在于栈和队列的基本操作是线性表操作的子...

  • 泡杯茶,我们坐下聊聊javascript事件环

    栈和队列 在计算机内存中存取数据,基本的数据结构分为栈和队列。 栈(Stack)是一种后进先出的数据结构,注意,有...

  • 队列和栈的应用

    队列和栈的使用 标签(空格分隔): algorithm 队列和栈的应用 1.队列的应用 队列是一种常见的数据结构,...

  • 集合--基础知识

    集合 集合类是一种工具类,存储数量不等的对象,可以实现栈,队列等数据结构。可以分为:Set:无序,不可重复的集合;...

网友评论

      本文标题:数据结构:栈、队列、Set

      本文链接:https://www.haomeiwen.com/subject/aerisxtx.html