差不多自己看懂了,可以自己写测试了。:)

LinkedList.go
package LinkedList

//"fmt"

type Node struct {
	data int
	next *Node
}

type LinkedList struct {
	head *Node
}

func (list *LinkedList) InsertFirst(i int) {
	data := &Node{data: i}
	if list.head != nil {
		data.next = list.head
	}
	list.head = data
}

func (list *LinkedList) InsertLast(i int) {
	data := &Node{data: i}
	if list.head == nil {
		list.head = data
		return
	}
	current := list.head
	for current.next != nil {
		current = current.next
	}
	current.next = data
}

func (list *LinkedList) RemoveByValue(i int) bool {
	if list.head == nil {
		return false
	}

	if list.head.data == i {
		list.head = list.head.next
		return true
	}
	current := list.head
	for current.next != nil {
		if current.next.data == i {
			current.next = current.next.next
			return true
		}
		current = current.next
	}
	return false
}

func (list *LinkedList) RemoveByIndex(i int) bool {
	if list.head == nil {
		return false
	}
	if i < 0 {
		return false
	}

	if i == 0 {
		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
	}
	current.next = current.next.next
	return true
}

func (list *LinkedList) 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 *LinkedList) GetFirst() (int, bool) {
	if list.head == nil {
		return 0, false
	}
	return list.head.data, true
}

func (list *LinkedList) 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 *LinkedList) GetSize() int {
	count := 0
	current := list.head
	for current != nil {
		count += 1
		current = current.next
	}
	return count
}

func (list *LinkedList) getItems() []int {
	var items []int
	current := list.head
	for current != nil {
		items = append(items, current.data)
		current = current.next
	}
	return items
}

  

LinkedList_test.go
package LinkedList

import (
	"fmt"
	"math/rand"
	"testing"
	"time"
)

func TestLinkedList(t *testing.T) {
	random := rand.New(rand.NewSource(time.Now().UnixNano()))
	headNode := &Node{
		data: random.Intn(100),
		next: nil,
	}
	list := &LinkedList{
		head: headNode,
	}
	fmt.Println(list.getItems())
	list.InsertFirst(random.Intn(100))
	fmt.Println(list.getItems())
	list.InsertLast(random.Intn(100))
	fmt.Println(list.getItems())
	randNumber := random.Intn(100)
	list.InsertFirst(randNumber)
	fmt.Println(list.getItems())
	list.InsertLast(random.Intn(100))
	fmt.Println(list.getItems())
	list.InsertFirst(random.Intn(100))
	fmt.Println(list.getItems())

	if list.SearchValue(randNumber) == false {
		t.Fail()
	}
	list.RemoveByValue(randNumber)
	if list.SearchValue(randNumber) == true {
		t.Fail()
	}
	fmt.Println(list.getItems())
	fmt.Println(list.RemoveByIndex(1))
	fmt.Println(list.getItems())
	fmt.Println(list.GetFirst())
	fmt.Println(list.GetLast())
	fmt.Println(list.GetSize())

}

  

golang数据结构和算法之LinkedList链表的更多相关文章

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

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

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

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

  3. 每周一练 之 数据结构与算法(LinkedList)

    这是第三周的练习题,原本应该先发第二周的,因为周末的时候,我的母亲大人来看望她的宝贝儿子,哈哈,我得带她看看厦门这座美丽的城市呀. 这两天我抓紧整理下第二周的题目和答案,下面我把之前的也列出来: 1. ...

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

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

  5. Android版数据结构与算法(三):基于链表的实现LinkedList源码彻底分析

    版权声明:本文出自汪磊的博客,未经作者允许禁止转载. LinkedList 是一个双向链表.它可以被当作堆栈.队列或双端队列进行操作.LinkedList相对于ArrayList来说,添加,删除元素效 ...

  6. java数据结构和算法04(链表)

    前面我们看了数组,栈和队列,大概就会这些数据结构有了一些基本的认识,首先回顾一下之前的东西: 在数组中,其实是分为有序数组和无序数组,我简单实现了无序数组,为什么呢?因为有序数组的实现就是将无序数组进 ...

  7. 数据结构与算法JavaScript描述——链表

    1.数组的缺点 数组不总是组织数据的最佳数据结构,原因如下. 在很多编程语言中,数组的长度是固定的,所以当数组已被数据填满时,再要加入新的元素就会非常困难. 在数组中,添加和删除元素也很麻烦,因为需要 ...

  8. 数据结构与算法(c++)——反转链表

    算法概述:要求实现将一条单向链表反转并考虑时间复杂度. 算法分析: 数组法(略): 将列表元素逐个保存进数组,之后再逆向重建列表 点评:实现逻辑最简单,需要额外的内存开销. 移动指针: 通过三个指针逐 ...

  9. 数据结构和算法之单向链表二:获取倒数第K个节点

    我们在做算法的时候或多或少都会遇到这样的问题,那就是我们需要获取某一个数据集的倒数或者正数第几个数据.那么今天我们来看一下这个问题,怎么去获取倒数第K个节点.我们拿到这个问题的时候自然而然会想到我们让 ...

随机推荐

  1. linux学习(十)Shell中的控制语句

    目录 1. 条件测试语句 1.1 test语句 1.2[]语句 1.3文件测试 1.4字符串测试 1.5数值测试 1.5逻辑操作符 @(Shell中的控制语句) 1. 条件测试语句 测试语句十Shel ...

  2. Django2.1.1与xadmin0.6.0遇到的坑(一)

    (1)django2.0把from django.core.urlresolvers修改成了django.urls 异常信息: ModuleNotFoundError: No module named ...

  3. 【新人填坑008】django升级2.x后报'WSGIRequest' object has no attribute 'session',

    1.X 到2.x后中间件定义的名字也有所不同 改一下就好了 在setting文件中将原先的 MIDDLEWARE_CLASS 改成MIDDLEWARE 如果还运行不成功  注释掉标白线的那一行 删掉也 ...

  4. Linux系统学习 一、安装,调试

    环境 主机: Windows 10 虚拟机: VMware 15 Pro 镜像: 一.安装过程: 然后开启虚拟机 设置主机名 时区 密码 最小安装 等着 重启 登录 二.配置静态IP地址 输入ifco ...

  5. 201871010116-祁英红《面向对象程序设计(java)》第十一周学习总结

    博文正文开头格式:(2分) 项目 内容 <面向对象程序设计(java)> https://www.cnblogs.com/nwnu-daizh/ 这个作业的要求在哪里 https://ww ...

  6. python 格式化打印

    #coding=utf-8 import time; start_time = time.time()for a in range(0,1001): for b in range(0,1001): f ...

  7. BZOJ2007/LG2046 「NOI2010」海拔 平面图最小割转对偶图最短路

    问题描述 BZOJ2007 LG2046 题解 发现左上角海拔为 \(0\) ,右上角海拔为 \(1\) . 上坡要付出代价,下坡没有收益,所以有坡度的路越少越好. 所以海拔为 \(1\) 的点,和海 ...

  8. docker修改系统时间总结

    最近弄docker烦躁的一笔,时区问题踩了不少坑,为了以后再遇到类似问题再花时间查资料,特记录一下... Ubuntu: echo "Asia/Shanghai" > /et ...

  9. 【ECNU77】位与数对个数(数位DP)

    点此看题面 大致题意: 求\(\sum_{x=0}^{a-1}\sum_{y=0}^{b-1}[(x\&y)<k]\). 数位\(DP\) 显然数位\(DP\)吧. 我们设\(f_{n, ...

  10. 报错:Something is already running on port 8000.

    在用react框架的时候,用cnpm run dev命令执行项目时,有时会出现这种错误, 这是因为你之前执行过该命令,但是没关闭,解决办法是打开任务管理器, 在进程中找到node.exe,右键关闭这个 ...