首页 > 其他分享 >go的环形数组

go的环形数组

时间:2023-03-25 11:22:34浏览次数:35  
标签:head 队列 fmt 环形 Println tail 数组 go array

package main

import (
	"errors"
	"fmt"
	"os"
)

// 使用一个结构体管理环形队列
type CircleQueue struct {
	maxSize int    // 4
	array   [5]int // 数组
	head    int    //指向队列队首 0
	tail    int    //指向队尾 0
}

// 如队列 AddQueue(push)  GetQueue(pop)
// 入队列
func (this *CircleQueue) Push(val int) (err error) {
	if this.IsFull() {
		return errors.New("queue full")
	}
	//分析出this.tail 在队列尾部,但是包含最后的元素
	this.array[this.tail] = val //把值给尾部
	this.tail = (this.tail + 1) % this.maxSize
	fmt.Printf("入队列>>head=%d,tail=%v, array=%v\n", this.head, this.tail, this.array)
	return
}

// 出队列
func (this *CircleQueue) Pop() (val int, err error) {
	if this.IsEmpty() {
		return 0, errors.New("queue empty")
	}
	//取出,head 是指向队首,并且含队首元素
	val = this.array[this.head]
	this.head = (this.head + 1) % this.maxSize
	fmt.Printf("出队列>>head=%d,tail=%v, array=%v\n", this.head, this.tail, this.array)
	return
}

// 显示队列
func (this *CircleQueue) ListQueue() {
	fmt.Println("环形队列情况如下:")
	//取出当前队列有多少个元素
	size := this.Size()
	if size == 0 {
		fmt.Println("队列为空")
	}
	//设计一个辅助的变量,指向head
	tempHead := this.head
	for i := 0; i < size; i++ {
		fmt.Printf("arr[%d]=%d\t", tempHead, this.array[tempHead])
		tempHead = (tempHead + 1) % this.maxSize
	}
	fmt.Println()
}

// 判断环形队列为满
func (this *CircleQueue) IsFull() bool {
	fmt.Printf("判断环形队列为满>>head=%d,tail=%v, array=%v\n", this.head, this.tail, this.array)
	return (this.tail+1)%this.maxSize == this.head
}

// 判断环形队列是空
func (this *CircleQueue) IsEmpty() bool {
	fmt.Printf("判断环形队列是空>>head=%d,tail=%v, array=%v\n", this.head, this.tail, this.array)
	return this.tail == this.head
}

// 取出环形队列有多少个元素
func (this *CircleQueue) Size() int {
	//这是一个关键的算法.
	fmt.Printf("取出环形队列有多少个元素>>this.head=%d,tail=%v, array=%v\n", this.head, this.tail, this.array)
	return (this.tail + this.maxSize - this.head) % this.maxSize
}

func main() {

	//初始化一个环形队列
	queue := &CircleQueue{
		maxSize: 5,
		head:    0,
		tail:    0,
	}

	var key string
	var val int
	for {
		fmt.Println("1. 输入add 表示添加数据到队列")
		fmt.Println("2. 输入get 表示从队列获取数据")
		fmt.Println("3. 输入show 表示显示队列")
		fmt.Println("4. 输入exit 表示显示队列")

		fmt.Scanln(&key)
		switch key {
		case "add", "1":
			fmt.Println("输入你要入队列数")
			fmt.Scanln(&val)
			err := queue.Push(val)
			if err != nil {
				fmt.Println(err.Error())
			} else {

				fmt.Println("加入队列ok")
			}
		case "get", "2":
			val, err := queue.Pop()
			if err != nil {
				fmt.Println(err.Error())
			} else {
				fmt.Println("从队列中取出了一个数=", val)
			}
		case "show", "3":
			queue.ListQueue()
		case "exit", "4":
			os.Exit(0)
		}
	}
}

标签:head,队列,fmt,环形,Println,tail,数组,go,array
From: https://www.cnblogs.com/heris/p/17185203.html

相关文章

  • php:用数组实现多语言(PHP 7.4.2)
    一,适用的场景:   旧系统需要增加多语言,不想改变原有的运行环境,   所以没有使用gettext,选择简单的用数组来实现说明:刘宏缔的架构森林是一个专注架构的博客,地......
  • 去掉一组整型数组重复的值
    这个问题出现在诸多的前端面试题中,主要考察个人对Object的使用,利用key来进行筛选。letunique=function(arr){lethashTable={};letdata=[];for(leti......
  • 编写一个方法,实现两个数组的合并,并按升序将合并后的数组返回---Java
    packagepractice.people.apple;/**编写一个方法,合并给定的数组,并以升序返回合并后的数组**/importjava.util.Arrays;publicclassArrayMerge{ publicstat......
  • PicGo+Github图床配置
    为了将PicGo设置为使用GitHub作为图床,您需要先创建一个GitHub仓库用于存储图片,然后在PicGo中进行相应的配置。您已经创建了一个仓库,所以让我们来配置PicGo。安......
  • 编写一个方法,计算给定两个数组之和---Java
    要求返回的数组是两个惨呼数组对应元素之和,不对应的元素直接赋给相应的位置packagepractice.people.apple;/**//编写一个计算给定的两数组之和**/publicclassS......
  • go 笔记 init函数
    来自:goinit函数1、golang里的main函数是程序的入口函数,olang还有另外一个特殊的函数init函数,先于main函数执行init函数的主要作用:初始化不能采用初始化表达式初始化的......
  • Django笔记七之ManyToMany和OneToOne介绍
    ManyToMany是一种多对多的关系,在用途和使用方法上和外键ForeignKey类似。以下是本篇笔记的目录:ManyToMany的介绍through参数through_fields参数ManyToMany关系......
  • MongoDB官方性能测试报告:YCSB测试下的并发量提升
    1.前言MongoDB3.0的主要侧重点是提高性能,尤其是写性能和对硬件资源的利用率。为了展示我们在3.0中取得的成果和如何来应用这些新的改善,我们接下来将发布一系列博客来比较......
  • 【LeetCode动态规划#02】图解不同路径I + II(首次涉及二维dp数组,)
    不同路径力扣题目链接(opensnewwindow)一个机器人位于一个mxn网格的左上角(起始点在下图中标记为“Start”)。机器人每次只能向下或者向右移动一步。机器人试图......
  • C-数组
    数组C语言支持数组数据结构,它可以存储一个固定大小的相同类型元素的顺序集合.数组都是由连续的内存位置组成.最低的地址对应第一个元素,最高的地址对应最后一个元素......