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:存放数据,数据按顺序排列,可以依次入队和出队,有序号关系,可以取出某序号的数 ...
随机推荐
- form-data、x-www-form-urlencoded、raw、binary的区别(非原创)
文章大纲 一.form-data介绍二.x-www-form-urlencoded介绍三.raw介绍四.binary介绍五.参考文章 一.form-data介绍 http请求中的multipart/f ...
- 工作日志,证书无效 unable to find valid certification path to requested target
工作日志,证书无效 unable to find valid certification path to requested target 最近被这个问题弄得头大.导致所有用到 se.transmod ...
- ramfs 和 tmpfs 以及 ramdisk相关调研
最近需要使用到 ramfs 和 tmpfs 做内存文件系统,下面对这两个文件系统相关的信息,做一下总结: 参考链接: https://www.thegeekstuff.com/2008/11/over ...
- JavaScript调用mysql查询bigint数据精度失真解决方案
最近我遇上了如题这个问题,后端用node.js写了一个读取mysql数据的接口,之前使用了很久都没发现什么问题,在查询订单表的订单ID时返回的值却是错的 正确的值是 19102818002800002 ...
- vue踩坑--细节决定成败
1.错误示例 . 2.错误的地方 3.修改后代码 4.错误分析
- 201871010109-胡欢欢《面向对象程序设计(java)》第十六周学习总结
项目 内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh/ 这个作业的要求在哪里 https://www.cnblogs.com/nwnu-daizh/p ...
- leetcode 1041. 困于环中的机器人
题目地址 : https://leetcode-cn.com/problems/robot-bounded-in-circle/ 在无限的平面上,机器人最初位于 (0, 0) 处,面朝北方.机器人可以 ...
- LG1345 「USACO5.4」Telecowmunication 最小割
问题描述 LG1345 题解 点边转化,最小割,完事. \(\mathrm{Code}\) #include<bits/stdc++.h> using namespace std; tem ...
- Paper | LISTEN, ATTEND AND SPELL: A NEURAL NETWORK FOR LARGE VOCABULARY CONVERSATIONAL SPEECH RECOGNITION
目录 1. 相关工作 2. 方法细节 2.1 收听器 2.2 注意力和拼写 本文提出了一个基于神经网络的语音识别系统List, Attend and Spell(LAS),能够将语音直接转录为文字. ...
- SourceTree3.2.6版本跳过注册办法
一.去sourceTree官网下载最新的包 官网:https://www.sourcetreeapp.com/windows版下载地址:https://product-downloads.atlass ...