scala版数据结构----环形队列

5 篇文章 0 订阅
2 篇文章 0 订阅

点个关注吧,球球啦!
scala数据结构:
数组环形队列:https://blog.csdn.net/Mr_kidBK/article/details/105026438
二叉搜索树:   https://blog.csdn.net/Mr_kidBK/article/details/105501805

前言
       博主发现网上对于scala版本的数据结构代码非常少,甚至有些nc营销号挂着标题复制粘贴别人的python,java代码,也是给我看醉了。
       为了方便学弟学妹们学习(chaozuoye),我准备做一个scala版本数据结构系列,如果觉的有帮助的话,关注下博主呗
class CircleQueue(arrMaxSize: Int) {
  val maxSize: Int = arrMaxSize + 1
  val array: Array[Int] = new Array[Int](maxSize)
  var front: Int = 0 //队头
  var rear: Int = 0 //队尾,指向队尾的下一个节点

  def isEmpty: Boolean = {
    front == rear
  }

  def isFull: Boolean = {
    (rear + 1) % maxSize == front
  }

  def size: Int = {
    (rear + maxSize - front) % maxSize
  }

  def showAllData: Unit = {
    if (isEmpty) {
      println("队列为空")
      return
    }

    for (i <- front until front + size)  {
      var s = array(i % maxSize).toString
      s += " ,"
      println(s"[${i % maxSize}]=$s")
    }

  }

  def add(elem: Int): Unit = {
    if (isFull) {
      println("队列满")
      return
    }
    array(rear) = elem
    rear = (rear + 1) % maxSize
  }

  def nextElem(): Any = {
    if (isEmpty) {
      return new Exception("队列空")
    }
    array(front)
  }

  def get(): Any = {
    if (isEmpty) {
      return new Exception("队列空")
    }
    val value = nextElem().asInstanceOf[Int]
    front = (front + 1) % maxSize
    value
  }

main:

  def main(args: Array[String]): Unit = {
    var circleQueuetmp: Any = null
    Breaks.breakable {
      while (true) {
        val in = StdIn.readLine("输入队列大小:")

        try {
          circleQueuetmp = new CircleQueue(in.toInt)
          Breaks.break
        } catch {
          case ex: Exception => println("请输入正确的数字")
        }
      }
    }
    var queue: CircleQueue = null
    if (circleQueuetmp != null && circleQueuetmp.isInstanceOf[CircleQueue]) {
      queue = circleQueuetmp.asInstanceOf[CircleQueue]
      println("-------")
    }

    var key = ""
    while (true) {
      println("show:显示队列所有数据")
      println("exit:退出")
      println("add:添加")
      println("get:取出数据")
      println("next:显示队头数据")

      key = StdIn.readLine()
      key.trim match {
        case "show" => queue.showAllData
        case "exit" => return
        case "add" => queue.add(StdIn.readInt())
        case "get" => {
          val res = queue.get()
          if (res.isInstanceOf[Exception]) {
            println(res.asInstanceOf[Exception].getMessage)
          } else {
            println(res)
          }
        }
        case "next" => {
          val res = queue.nextElem()
          if (res.isInstanceOf[Exception]) {
            println(res.asInstanceOf[Exception].getMessage)
          } else {
            println(res)
          }
        }
      }
    }

  }

点个赞再走,球球啦!

原创不易,白嫖不好,各位的支持和认可,就是我创作的最大动力,我们下篇文章见!

本博客仅发布于CSDN—一个帅到不能再帅的人 Mr_kidBK。转载请标明出处。
https://blog.csdn.net/Mr_kidBK

点赞!收藏!转发!!!么么哒!
点赞!收藏!转发!!!么么哒!
点赞!收藏!转发!!!么么哒!
点赞!收藏!转发!!!么么哒!
点赞!收藏!转发!!!么么哒!
————————————————
在这里插入图片描述

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值