golang数据结构和算法之QueueLinkedList链表队列
队列和堆栈不一样的地方在于进出顺序:
堆栈是后进先出,
队列是先进先出。
QueueLinkedList.go
package QueueLinkedList
type Node struct {
data int
next *Node
}
type Queue struct {
rear *Node
}
func (list *Queue) Enqueue(i int) {
data := &Node{data: i}
if list.rear != nil {
data.next = list.rear
}
list.rear = data
}
func (list *Queue) Dequeue() (int, bool) {
if list.rear == nil {
return 0, false
}
if list.rear.next == nil {
i := list.rear.data
list.rear = nil
return i, true
}
current := list.rear
for {
if current.next.next == nil {
i := current.next.data
current.next = nil
return i, true
}
current = current.next
}
}
func (list *Queue) Peek() (int, bool) {
if list.rear == nil {
return 0, false
}
return list.rear.data, true
}
func (list *Queue) Get() []int {
var items []int
current := list.rear
for current != nil {
items = append(items, current.data)
current = current.next
}
return items
}
func (list *Queue) IsEmpty() bool {
return list.rear == nil
}
func (list *Queue) Empty() {
list.rear = nil
}
QueueLinkedList_test.go
package QueueLinkedList
import (
"fmt"
"math/rand"
"testing"
"time"
)
func TestQueueLinkedList(t *testing.T) {
random := rand.New(rand.NewSource(time.Now().UnixNano()))
headNode := &Node{
data: random.Intn(100),
next: nil,
}
queue := &Queue{
rear: headNode,
}
fmt.Println(queue.Get())
randNumber := random.Intn(100)
queue.Enqueue(randNumber)
queue.Enqueue(random.Intn(100))
queue.Enqueue(random.Intn(100))
fmt.Println(queue.Get())
queue.Dequeue()
fmt.Println(queue.Get())
retResult, retBool := queue.Peek()
if retBool == true {
fmt.Println(retResult)
}
queue.Empty()
if queue.IsEmpty() == false {
t.Fail()
}
}
输出:
D:/Go/bin/go.exe test -v [D:/go-project/src/QueueLinkedList] === RUN TestQueueLinkedList [68] [12 49 69 68] [12 49 69] 12 --- PASS: TestQueueLinkedList (0.00s) PASS ok QueueLinkedList 2.177s 成功: 进程退出代码 0.
golang数据结构和算法之QueueLinkedList链表队列的更多相关文章
- golang数据结构和算法之CircularBuffer环形缓冲队列
慢慢练语法和思路, 想说的都在代码及注释里. CircularBuffer package CircularBuffer const arraySize = 10 type CircularBuffe ...
- golang数据结构和算法之StackLinkedList链表堆栈
会了上一个,这个就差不离了. StackLinkedList.go package StackLinkedList type Node struct { data int next *Node } t ...
- golang数据结构和算法之LinkedList链表
差不多自己看懂了,可以自己写测试了.:) LinkedList.go package LinkedList //"fmt" type Node struct { data int ...
- JavaScript 版数据结构与算法(二)队列
今天,我们要讲的是数据结构与算法中的队列. 队列简介 队列是什么?队列是一种先进先出(FIFO)的数据结构.队列有什么用呢?队列通常用来描述算法或生活中的一些先进先出的场景,比如: 在图的广度优先遍历 ...
- 数据结构和算法 – 3.堆栈和队列
1.栈的实现 后进先出 自己实现栈的代码 using System; using System.Collections.Generic; using System.Linq; using ...
- JS数据结构及算法(二) 队列
队列是遵循先进先出的一种数据结构,在尾部添加新元素,并从顶部移除元素. 1.普通队列 function Queue() { this.items = []; } Queue.prototype = { ...
- golang数据结构和算法之BinarySearch二分查找法
基础语法差不多了, 就需要系统的撸一下数据结构和算法了. 没找到合适的书, 就参考github项目: https://github.com/floyernick/Data-Structures-and ...
- java:数据结构复习(三)链表队列
@TOC 和栈一样,队列也是表,但是使用队列的特点是先进先出. 队列模型 队列的基本操作是入队,它是在表的末端插入一个元素,和出队,它是删除在表开头的一个元素 graph LR A[<kbd&g ...
- 用JS描述的数据结构及算法表示——栈和队列(基础版)
前言:找了上课时数据结构的教程来看,但是用的语言是c++,所以具体实现在网上搜大神的博客来看,我看到的大神们的博客都写得特别好,不止讲了最基本的思想和算法实现,更多的是侧重于实例运用,一边看一边在心里 ...
随机推荐
- raid组合优缺点介绍和创建LVM实验个人笔记
一.RAID组合介绍 RAID模式优缺点的简要介绍 1.raid 0 模式 优点:在RAID 0状态下,存储数据被分割成两部分,分别存储在两块硬盘上,此时移动硬盘的理论存储速度是单块硬盘的2倍,实际容 ...
- June 03rd, 2019. Week 23rd, Monday
There is no shame in hard work. 努力从来不丢人. Stop complaining about the current work arrangements, just ...
- 图像处理&计算机视觉中upscale,downscale的翻译理解
最近在看SAN网络(Second-order Attention Network for Single Image Super-Resolution)的论文,其中的Upscale module理解的不 ...
- mysql中group by 使用
问题描述 我现在需要查询表test,里面需要安装字段a 进行分组.分组之后还有按照b字段最大的.还要查询出字段c. 我先在使用的数据库是mysql8.0 解决 需注意: group by 分组的时候是 ...
- MySQL常用数据类型 length 专题
MySQL-data_type数据类型 1.查看数据类型 mysql> help data type //通过help对数据进行查看,以及使用的方法 2.MySQL常见的数据类型 整数in ...
- 关于scanf的一些知识
10.22,对现阶段已知道的scanf的一些用法或注意事项的一些总结: 1.scanf中,赋值的那个数据前面一定加&! 2.若情景要求必须输入空格的,scanf("%d%c%d&qu ...
- awk命令使用整理
1. awk默认以空格分隔, 可摘出指定位置的内容, 通常用法如下( 文件名称为file.txt ): 文件中行内容为: 12:3 a 4:56 b awk '{print $1}' ...
- React: React脚手架
一.简言 React开发目前已经非常流行,对于如何实现对React项目的管理和维护,React生态圈出现了大量可用的开发工具,例如Browserify.Gulp.Grunt.webpack等.其中,w ...
- Lambda,递归
1.Lamdba表达式 1.Lambda表达式的标准格式 三部分组成: 一些参数 一个箭头 一段代码 格式: (参数列表) -> {一些重写方法的代码} 解释说明格式: ():接口中抽象方法的参 ...
- GPU跑tf-faster-rcnn demo以及训练自己的数据
https://blog.csdn.net/qq_39123369/article/details/85245512