5-栈-Scala实现

栈是一种先进后出的一种数据结构,可以用数组和双向链表来实现,这里用数组来实现

package com.atguigu.datastructures.stack

import scala.io.StdIn

object ArrayStackDemo {
  def main(args: Array[String]): Unit = {
    val stack = new ArrayStack(4)
    var key = ""
    while (true){
      println()
      println("push:入栈操作")
      println("list:遍历栈")
      println("pop:出栈")
      println("peek:查看栈顶")
      key = StdIn.readLine()
      key match {
        case "push" =>
          println("请输入一个数")
          val num =StdIn.readInt()
          stack.push(num)
        case "list" =>
          stack.list()
        case "pop" =>
          val res = stack.pop()
          if (res.isInstanceOf[Exception]){
            println(res.asInstanceOf[Exception].getMessage)
          }else{
            println()
            println("取出栈顶的值=%d",res)
          }
        case "peek" =>
          val res = stack.peek()
          if (res.isInstanceOf[Exception]){
            println(res.asInstanceOf[Exception].getMessage)
          }else{
            println()
            println("栈顶的值=%d",res)
          }
      }
    }
  }

}
//使用数组模拟一个栈
class ArrayStack(arrMaxSize:Int){
  val maxSize = arrMaxSize
  //创建数组
  val arr = new Array[Int](maxSize)
  //栈顶
  var top = -1
  //入栈
  def push(num:Int):Unit={
    if (isFull()){
      println("栈满")
      return
    }
    top +=1 //后移一位
    arr(top) = num

  }

  def pop():Any={
    if (isEmpty()){
      return new Exception("栈空")
    }
    val res  = arr(top)
    top -= 1
    return res
  }

  //遍历
  def list():Unit={
    if (isEmpty()){
      println("栈空不能遍历")
      return
    }
    //逆序遍历
    for(i <- 0 to top reverse){
      printf("arr(%d)=%d\n",i,arr(i))
    }
  }

  //查看栈顶的值,但是不取出
  def peek():Any={
    if (isEmpty()){
      return new Exception("栈空")
    }
    return arr(top)
  }
  //判断栈满
  def isFull():Boolean={
    top == maxSize-1
  }

  //判断栈空
  def isEmpty():Boolean={
    top == -1
  }
}

  

posted @ 2020-05-22 15:51  济默  阅读(278)  评论(0编辑  收藏  举报