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++,所以具体实现在网上搜大神的博客来看,我看到的大神们的博客都写得特别好,不止讲了最基本的思想和算法实现,更多的是侧重于实例运用,一边看一边在心里 ...
随机推荐
- RDIFramework.NET敏捷开发框架Web新增邮件中心实现便捷式的邮件收发
1.引言 邮件收发在很多业务系统中都有这样的需求,是比较正式和常用的功能.在我们的框架中提供了邮件中心功能模块,集内部邮件的收发.邮件归类.邮件星标的标记.邮件的删除与彻底删除等,邮件中心功能模块界面 ...
- 微信小程序如何做金额输入限制
引言:金额输入时,需求如下: 1)首位不能出现0或者小数点(.) 2)仅保留两位小数 3)仅保留一个小数点(.),不允许出现多个. 1.wxml核心代码: <!-- 1.adjust-posit ...
- Codeforces Round #600 (Div. 2)
传送门 A. Single Push 直接乱搞即可. Code /* * Author: heyuhhh * Created Time: 2019/11/16 22:36:20 */ #include ...
- java自定义函数调用
一:主类中自定义函数 在主类中,如果想要在main函数中调用自定义的其他方法,则这个函数需要使用static关键字定义,否则会报错Cannot make a static reference to t ...
- CometOJ10C 鱼跃龙门
题目链接 problem 实际上就是对于给定的\(n\)求一个最小的\(x\)满足\(\frac{x(x+1)}{2}=kn(k\in N^*)\). solution 对上面的式子稍微变形可得\(x ...
- 趣谈Linux操作系统学习笔记:第二十九讲
一.引子 在这之前,有一点你需要注意.解析系统调用是了解内核架构最有力力的一把钥匙,这里我们只要重点关注这几个最重要的系统调用就可以了 1.mount 系统调用用于挂载文件系统:2.open 系统调用 ...
- Web自动化测试Selenium 学习笔记(一)
1.Web自动化测试简介自动化基础:自动化用例编写.Selenium优势及原理.自动化环境搭建Selenium基础:常见8大元素定位(表格).常见元素处理.下拉框元素处理.不同窗口切换.元素进阶.元素 ...
- 一个Web前端工程师或程序员的发展方向,未来困境及穷途末路
如果你刚好是一个Web前端工程师,或者你将要从事web前端工作.你应该和我有同样的感慨,web前端技术到了自己的天花板,前端工作我能做多少年?3年或5年?自己的职业规划应该怎么样?收入为什么没有增长? ...
- Delphi 10.2 Tokyo新增JSON类学习——TJsonSerializer
Delphi 10.3.2 for windows 7 编译通过,源码下载地址: Tokyo 10.2新增类,效率更高更快 TJsonSerializer 需要引用单元:System.JSON.Ser ...
- Mac下如何复制webp图片
将 WebP 格式图片拖到 Chrome 浏览器标签栏中(浏览器是肯定支持查看的,而且是 Google 自家的),这个时候图片是能够正常查看的.我们右键选中图片,选择「复制图片」. 打开 macO ...