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++,所以具体实现在网上搜大神的博客来看,我看到的大神们的博客都写得特别好,不止讲了最基本的思想和算法实现,更多的是侧重于实例运用,一边看一边在心里 ...
随机推荐
- [Go] protobuffer 的环境配置
一般使用gprc是需要使用protobuf作为数据传输的格式标准,可以对要传输的对象结构体进行序列化 先安装protoc,找到对应版本,下载直接把二进制文件复制到环境变量可以访问到的地方就行 http ...
- 使用python对整个网页进行截图
方法一.使用PyQt4的QtWebKit组件 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 ...
- numpy和matplotlib的pyplot函数的简单应用实现
- springmvc的文件上传和JWT图形验证码
相关pom依赖 <dependency> <groupId>commons-fileupload</groupId> <artifactId>commo ...
- java8-07-方法引用总结
一:方法引用 如果Lambda体中的内容 已经有方法实现了 我们可以使用"方法引用" (可以理解为 ...
- R1-5天
R语言笔记文档 2019.11.24 R语言的安装 工作目录查看与更改 变量的三种赋值 如何查看R语言帮助 ? args 基础数据类型 基本数据类型 因子.数据框.数组.矩阵.列表.向量 2019.1 ...
- java非四舍五入
Double htert=34.9768; DecimalFormat df = new DecimalFormat("#.00");// 保留五位小数非四舍五入型 df.setR ...
- React: 研究React的组件化
一.简介大概 在以往的Web开发中,会把web页面所有的复杂控件作为一个单一的整体进行开发,由于控件之间需要进行通信,因此不同的组件之间的耦合度会很多,由于开发一个控件的时候要考虑到控件与控件之间的联 ...
- 【STM32H7教程】第20章 STM32H7的GPIO应用之无源蜂鸣器
完整教程下载地址:http://www.armbbs.cn/forum.php?mod=viewthread&tid=86980 第20章 STM32H7的GPIO应用之无源蜂鸣器 ...
- angularjs中使用锚点,angular路由导致锚点失效的两种解决方案
壹 ❀ 引 公司新项目开发中,首页要做个楼层导航效果(如下图),要求能点击图标对应跳到楼层即可,因为不需要跳转过度动画,也要求最好别用JQ,想着原生js操作dom计算top的兼容性,想着用锚点实现算 ...