golang数据结构和算法之DoublyLinkedList双向链表
双向链表比起单向链表,
多了一个向前指向的指针,
所以在增删查改时,要同时照顾到两个指针的指向。
DoublyLinkedList.go
package DoublyLinkedList
//双向链表
type Node struct {
data int
next *Node
prev *Node
}
type DoublyLinkedList struct {
head *Node
tail *Node
}
func (list *DoublyLinkedList) InsertFirst(i int) {
data := &Node{data: i}
if list.head != nil {
list.head.prev = data
data.next = list.head
}
list.head = data
}
func (list *DoublyLinkedList) InsertLast(i int) {
data := &Node{data: i}
if list.head == nil {
list.head = data
list.tail = data
return
}
if list.tail != nil {
list.tail.next = data
data.prev = list.tail
}
list.tail = data
}
func (list *DoublyLinkedList) RemoveByValue(i int) bool {
if list.head == nil {
return false
}
if list.head.data == i {
list.head = list.head.next
list.head.prev = nil
return true
}
if list.tail.data == i {
list.tail = list.tail.prev
list.tail.next = nil
return true
}
current := list.head
for current.next != nil {
if current.next.data == i {
if current.next.next != nil {
current.next.next.prev = current
}
current.next = current.next.next
return true
}
current = current.next
}
return false
}
func (list *DoublyLinkedList) RemoveByIndex(i int) bool {
if list.head == nil {
return false
}
if i < 0 {
return false
}
if i == 0 {
list.head.prev = nil
list.head = list.head.next
return true
}
current := list.head
for u := 1; u < i; u++ {
if current.next.next == nil {
return false
}
current = current.next
}
if current.next.next != nil {
current.next.next.prev = current
}
current.next = current.next.next
return true
}
func (list *DoublyLinkedList) SearchValue(i int) bool {
if list.head == nil {
return false
}
current := list.head
for current != nil {
if current.data == i {
return true
}
current = current.next
}
return false
}
func (list *DoublyLinkedList) GetFirst() (int, bool) {
if list.head == nil {
return 0, false
}
return list.head.data, true
}
func (list *DoublyLinkedList) GetLast() (int, bool) {
if list.head == nil {
return 0, false
}
current := list.head
for current.next != nil {
current = current.next
}
return current.data, true
}
func (list *DoublyLinkedList) GetSize() int {
count := 0
current := list.head
for current != nil {
count += 1
current = current.next
}
return count
}
func (list *DoublyLinkedList) GetItemFromStart() []int {
var items []int
current := list.head
for current != nil {
items = append(items, current.data)
current = current.next
}
return items
}
func (list *DoublyLinkedList) GetItemFromEnd() []int {
var items []int
current := list.tail
for current != nil {
items = append(items, current.data)
current = current.prev
}
return items
}
DoublyLinkedList_test.go
package DoublyLinkedList
//使用随机数作测试
import (
"fmt"
"math/rand"
"testing"
"time"
)
func TestDoublyLinkedList(t *testing.T) {
random := rand.New(rand.NewSource(time.Now().UnixNano()))
headNode := &Node{
data: random.Intn(100),
next: nil,
prev: nil,
}
list := &DoublyLinkedList{
head: headNode,
tail: headNode,
}
fmt.Println(list.GetItemFromStart())
list.InsertFirst(random.Intn(100))
fmt.Println(list.GetItemFromStart())
list.InsertLast(random.Intn(100))
fmt.Println(list.GetItemFromStart())
randNumber := random.Intn(100)
list.InsertFirst(randNumber)
fmt.Println(list.GetItemFromStart())
list.InsertLast(random.Intn(100))
fmt.Println(list.GetItemFromStart())
list.InsertFirst(random.Intn(100))
fmt.Println(list.GetItemFromStart())
fmt.Println(list.GetItemFromEnd())
if list.SearchValue(randNumber) == false {
t.Fail()
}
list.RemoveByValue(randNumber)
if list.SearchValue(randNumber) == true {
t.Fail()
}
fmt.Println(list.GetFirst())
fmt.Println(list.GetLast())
fmt.Println(list.GetSize())
}

golang数据结构和算法之DoublyLinkedList双向链表的更多相关文章
- golang数据结构和算法之BinarySearch二分查找法
基础语法差不多了, 就需要系统的撸一下数据结构和算法了. 没找到合适的书, 就参考github项目: https://github.com/floyernick/Data-Structures-and ...
- 数据结构与算法-python描述-双向链表
# coding:utf-8 # 双向链表的相关操作: # is_empty() 链表是否为空 # length() 链表长度 # travel() 遍历链表 # add(item) 链表头部添加 # ...
- golang数据结构和算法之QueueLinkedList链表队列
队列和堆栈不一样的地方在于进出顺序: 堆栈是后进先出, 队列是先进先出. QueueLinkedList.go package QueueLinkedList type Node struct { d ...
- golang数据结构和算法之StackLinkedList链表堆栈
会了上一个,这个就差不离了. StackLinkedList.go package StackLinkedList type Node struct { data int next *Node } t ...
- golang数据结构和算法之StackArray数组堆栈
用数组实现的堆栈, 另一种,是用链表实现的堆栈, 在各种不同的编程语言上, 实现都是类似的. StackArray.go package StackArray //基于数组实现的堆栈 const ar ...
- golang数据结构和算法之LinkedList链表
差不多自己看懂了,可以自己写测试了.:) LinkedList.go package LinkedList //"fmt" type Node struct { data int ...
- golang数据结构和算法之CircularBuffer环形缓冲队列
慢慢练语法和思路, 想说的都在代码及注释里. CircularBuffer package CircularBuffer const arraySize = 10 type CircularBuffe ...
- 数据结构与算法 Big O 备忘录与现实
不论今天的计算机技术变化,新技术的出现,所有都是来自数据结构与算法基础.我们需要温故而知新. 算法.架构.策略.机器学习之间的关系.在过往和技术人员交流时,很多人对算法和架构之间的关系感 ...
- 数据结构和算法(Golang实现)(15)常见数据结构-列表
列表 一.列表 List 我们又经常听到列表 List数据结构,其实这只是更宏观的统称,表示存放数据的队列. 列表List:存放数据,数据按顺序排列,可以依次入队和出队,有序号关系,可以取出某序号的数 ...
随机推荐
- 记录python上传文件的坑(1)
import random import string import requests from requests_toolbelt import MultipartEncoder f = '2019 ...
- golang中的viper示例
这是第二次实操viper了, 年纪大了就多练练,才能记住. http://go.coder55.com/article/6589 https://github.com/spf13/viper pack ...
- R1-5天
R语言笔记文档 2019.11.24 R语言的安装 工作目录查看与更改 变量的三种赋值 如何查看R语言帮助 ? args 基础数据类型 基本数据类型 因子.数据框.数组.矩阵.列表.向量 2019.1 ...
- MySQL Explain详解 查看mysql语句详情
在日常工作中,我们会有时会开慢查询去记录一些执行时间比较久的SQL语句,找出这些SQL语句并不意味着完事了,些时我们常常用到explain这个命令来查看一个这些SQL语句的执行计划,查看该SQL语句有 ...
- 2019 CVPR 基于GAN的ImageCaptioning论文
1.MSCap: Multi-Style Image Captioning with Unpaired Stylized Text 生成多种风格的caption 当前的image captioning ...
- AFO!
\(update:2019-12-13\) 成绩已经出了,我的OI生涯也算是正式结束了.虽然成绩并不满意,但好在也是收获了一个省一(虽然我不一定用).总的来说,作为正式选手不到两年半的OI之路走得并不 ...
- 如何查看PDF的坐标
有时候,我们明知道现状并不够科学.不够合理,但没有时间和条件去改变现状,还得硬要照着这种方式去维护,很是痛苦. 在程序生成文字报告通常使用docx,如果需要更通用.更灵活,还可以使用rtf,而前期设计 ...
- UmiJS 目录及约定
在文件和目录的组织上,umi 更倾向于选择约定的方式. 一个复杂应用的目录结构如下: . ├── dist/ // 默认的 build 输出目录 ├── mock/ // mock 文件所在目录,基于 ...
- tensorflow2.0安装
版本: python3.5 Anaconda 4.2.0 tensorflow2.0 cpu版本 1.安装命令 pip3 install tensorflow==2.0.0.0a0 -i https: ...
- Day02stu
⦁ 环境搭建之接口测试工具Jmeter搭建 1) 什么是Jmeter? Jmeter是一个接口测试工具,基于Java开发,是是Apche公司使用Java平台开发的一款测试工具. 运行环境需要配置:JD ...