双向链表比起单向链表,

多了一个向前指向的指针,

所以在增删查改时,要同时照顾到两个指针的指向。

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双向链表的更多相关文章

  1. golang数据结构和算法之BinarySearch二分查找法

    基础语法差不多了, 就需要系统的撸一下数据结构和算法了. 没找到合适的书, 就参考github项目: https://github.com/floyernick/Data-Structures-and ...

  2. 数据结构与算法-python描述-双向链表

    # coding:utf-8 # 双向链表的相关操作: # is_empty() 链表是否为空 # length() 链表长度 # travel() 遍历链表 # add(item) 链表头部添加 # ...

  3. golang数据结构和算法之QueueLinkedList链表队列

    队列和堆栈不一样的地方在于进出顺序: 堆栈是后进先出, 队列是先进先出. QueueLinkedList.go package QueueLinkedList type Node struct { d ...

  4. golang数据结构和算法之StackLinkedList链表堆栈

    会了上一个,这个就差不离了. StackLinkedList.go package StackLinkedList type Node struct { data int next *Node } t ...

  5. golang数据结构和算法之StackArray数组堆栈

    用数组实现的堆栈, 另一种,是用链表实现的堆栈, 在各种不同的编程语言上, 实现都是类似的. StackArray.go package StackArray //基于数组实现的堆栈 const ar ...

  6. golang数据结构和算法之LinkedList链表

    差不多自己看懂了,可以自己写测试了.:) LinkedList.go package LinkedList //"fmt" type Node struct { data int ...

  7. golang数据结构和算法之CircularBuffer环形缓冲队列

    慢慢练语法和思路, 想说的都在代码及注释里. CircularBuffer package CircularBuffer const arraySize = 10 type CircularBuffe ...

  8. 数据结构与算法 Big O 备忘录与现实

    不论今天的计算机技术变化,新技术的出现,所有都是来自数据结构与算法基础.我们需要温故而知新.        算法.架构.策略.机器学习之间的关系.在过往和技术人员交流时,很多人对算法和架构之间的关系感 ...

  9. 数据结构和算法(Golang实现)(15)常见数据结构-列表

    列表 一.列表 List 我们又经常听到列表 List数据结构,其实这只是更宏观的统称,表示存放数据的队列. 列表List:存放数据,数据按顺序排列,可以依次入队和出队,有序号关系,可以取出某序号的数 ...

随机推荐

  1. SQL高效运行注意事项(四)

    为了SQLSERVER能高效运行,SQLSERVER的磁盘子系统是一个重要的方面 Avg. Disk Sec/Read 这个计数器是指每秒从磁盘读取数据的平均值 下面的列表显示这个计数器值的范围,并指 ...

  2. MySQL学习——管理用户权限

    MySQL学习——管理用户权限 摘要:本文主要学习了使用DCL语句管理用户权限的方法. 了解用户权限 什么是用户 用户,指的就是操作和使用MySQL数据库的人.使用MySQL数据库需要用户先通过用户名 ...

  3. glibc提供的malloc()的调试工具

    关键词:MALLOC_CHECK_.mtrace().muntrace().MALLOC_TRACE.mprobe().-lmcheck等等. 1. MALLOC_CHECK_环境变量(double ...

  4. np.unique( )的用法

    该函数是去除数组中的重复数字,并进行排序之后输出. 换句话,我想从一个图片选取 1000个不同的点,随机采点经常遇到相同的点,造成重复.np.unique就是用来解决这个问题

  5. python爬虫之csv文件

     一.二维数据写入csv文件 题目要求: 读入price2016.csv文件,将其中的数据读出,将数字部分计算百分比后输出到price2016out.csv文件中 知识点: 对于列表中存储的二维数据, ...

  6. BZOJ1001/LG4001 「ICPC Beijing2006」狼抓兔子 平面图最小割转对偶图最短路

    问题描述 BZOJ1001 LG4001 题解 平面图最小割=对偶图最短路 假设起点和终点间有和其他边都不相交的一条虚边. 如图,平面图的若干条边将一个平面划分为若干个图形,每个图形就是对偶图中的一个 ...

  7. vue-cil3 运行报错 --- warnings potentially fixable with the `--fix` option

    warnings potentially fixable with the `--fix` option. 将一下部分:"lint": "vue-cli-service ...

  8. Python 生成器 (generator) & 迭代器 (iterator)

    python 生成器 & 迭代器 生成器 (generator) 列表生成式 列表生成式用来生成一个列表,虽然写的是表达式,但是储存的是计算出来的结果,因此生成的列表受到内存大小的限制 示例: ...

  9. Fiddler修改请求数据

    截断方法一: 在菜单中选择“Rules”->“Automatic Breakpoint”->“Before Requests”,这种方式会截断所有Request请求. 2.浏览器打开站点, ...

  10. IT兄弟连 HTML5教程 HTML5和JavaScript的关系

    JavaScript可是实现HTML5重要语言.长久以来,JavaScript一直都是在HTML中实现动态效果的不二之选,而JavaScript在一些程序员眼里都是编程语言中的二等公民.早先,它经常是 ...