Go语言实现单链表

  • 初始化单链表:通过NewList函数初始化单链表,并指定头结点和大小。
  • 打印单链表:通过display方法打印出单链表中所有节点的值。
  • 判断单链表中是否存在环:使用快慢指针的方式判断单链表中是否存在环。
  • 就地翻转单链表:使用迭代的方式实现单链表的就地翻转。
  • 清除单链表中所有节点:通过clear方法清除单链表中的所有节点,并返回节点值的列表。
  • 判断单链表是否为空:通过isEmpty方法判断单链表是否为空。
  • 返回单链表的长度大小:通过length方法返回单链表的长度。
  • 返回单链表的首节点:通过getHead方法返回单链表的首节点。
  • 设置单链表的首节点:通过setHead方法设置单链表的首节点。
  • 手动计算单链表的长度大小:通过count方法手动计算单链表的长度。
  • 判断给定的值是否在单链表中:通过search方法判断给定的值是否在单链表中。
  • 根据给定的索引返回单链表中相应的元素值:通过getNth方法根据给定的索引返回单链表中相应的元素值。
  • 在头结点前插入元素:通过insertHead方法在头结点前插入元素。
  • 在尾结点后插入元素:通过insertTail方法在尾结点后插入元素。
  • 在指定位置上插入元素:通过insertNth方法在指定位置上插入元素。
  • 删除头结点:通过deleteHead方法删除头结点,并返回删除的元素值。
  • 删除尾结点:通过deleteTail方法删除尾结点,并返回删除的元素值。
  • 删除指定位置的元素:通过deleteNth方法删除指定位置的元素,并返回删除的元素值。
  • 检查给定位置是否超出单链表范围:通过checkBounds方法检查给定位置是否超出单链表范围。
package singlylinkedlist

import (
	"fmt"
)

type Node struct {
	value int
	next  *Node
}

func NewNode(value int, next *Node) *Node {
	return &Node{value, next}
}

type singlylinkedlist struct {
	head *Node
	size int
}

//
// NewList
//  @Description: 初始化单链表,指定头结点和大小
//  @param head
//  @param size
//  @return *singlylinkedlist
//
func NewList(head *Node, size int) *singlylinkedlist {
	return &singlylinkedlist{head, size}
}

//
// display
//  @Description: 打印出单链表中所有结点的值
//  @receiver l
//
func (l *singlylinkedlist) display() {
	if l.isEmpty() {
		fmt.Println("The list to display is empty")
	}
	cur := l.head
	for cur != nil {
		if cur.next == nil {
			fmt.Println(cur.value)
			break
		}
		fmt.Print(cur.value, "->")
		cur = cur.next
	}
}

//
// detectLoop
//  @Description: 判断单链表中是否存在环(使用快慢指针的方式)
//  @return bool
//
func (l *singlylinkedlist) detectLoop() bool {
	slow := l.head
	fast := l.head
	for (fast != nil) && (fast.next != nil) {
		slow = slow.next
		fast = fast.next.next
		if fast == slow {
			return true
		}
	}
	return false
}

//
// reverseList
//  @Description: 就地翻转单链表
//  @receiver l
//
func (l *singlylinkedlist) reverseList() {

	var pre *Node
	cur := l.head
	nxt := l.head

	for cur != nil {
		nxt = cur.next
		cur.next = pre
		pre = cur
		cur = nxt
	}
	l.head = pre
}


//
// clear
//  @Description: 清除单链表中所有结点
//  @receiver l
//  @return res
//
func (l *singlylinkedlist) clear() (res []int) {

	cur := l.head
	for cur != nil {
		prev := cur
		res = append(res, prev.value)
		cur = cur.next
		prev = nil
	}
	l.head = nil
	l.size = 0
	return res
}

//
// isEmpty
//  @Description: 判断单链表是否为空
//  @receiver l
//  @return bool
//
func (l *singlylinkedlist) isEmpty() bool{
	return l.size == 0
}

//
// count
//  @Description: 返回单链表的长度大小
//  @receiver l
//  @return int the count of the list
//
func (l *singlylinkedlist) length() int {
	return l.size
}


//
// getHead
//  @Description: 返回单链表的首结点
//  @receiver l
//  @return *Node the head of the list
//
func (l *singlylinkedlist) getHead() *Node {
	return l.head
}


//
// setHead
//  @Description: 设置单链表的首结点
//  @receiver l
//  @param node
//
func (l *singlylinkedlist) setHead(node *Node) {
	l.head = node
}

//
// count
//  @Description: 手动计算单链表的长度大小
//  @receiver l
//  @return int the count of the list
//
func (l *singlylinkedlist) count() int {
	count := 0
	cur := l.head
	for cur != nil {
		count++
		cur = cur.next
	}
	return count

}

//
// search
//  @Description: 判断所给的key值是否在单链表中
//  @receiver l
//  @param key the value to be searched
//  @return bool {@code true} if key is present in the list, otherwise {@code false}
//
func (l *singlylinkedlist) search(key int) bool {
	cur := l.head
	for cur != nil {
		if cur.value == key {
			return true
		}
		cur = cur.next
	}
	return false
}

//
// getNth
//  @Description: 根据所给的index返回单链表中相应的元素值
//  @receiver l
//  @param index
//  @return int
//
func (l *singlylinkedlist) getNth(index int) int {
	l.checkBounds(index, 0, l.size - 1)
	cur := l.head
	for i := 0; i < index; i++ {
		cur = cur.next
	}
	return cur.value
}


//
// insertHead
//  @Description: 在头结点前插入元素
//  @receiver l
//  @param x
//
func (l *singlylinkedlist) insertHead(x int) {
	l.insertNth(x, 0)
}


//
// insertTail
//  @Description: 在尾结点之后插入元素
//  @receiver l
//  @param data
//
func (l *singlylinkedlist) insertTail(data int) {
	l.insertNth(data, l.size)
}



//
// insertNth
//  @Description: 在指定的位置上插入元素
//  @receiver l
//  @param data
//  @param position
//
func (l *singlylinkedlist) insertNth(data int, position int) {
	l.checkBounds(position, 0, l.size)
	newNode := NewNode(data, nil)
	if l.head == nil {
		l.head = newNode
		l.size++
		return
	}
	if position == 0 {
		newNode.next = l.head
		l.head = newNode
		l.size++
		return
	}

	cur := l.head
	for i := 0; i < position - 1; i++ {
		cur = cur.next
	}
	newNode.next = cur.next
	cur.next = newNode
	l.size++
}

//
// deleteHead
//  @Description: 删除头结点
//  @receiver l
//  @return int
//
func (l *singlylinkedlist) deleteHead() int {
	return l.deleteNth(0)
}


//
// deleteTail
//  @Description: 删除尾结点
//  @receiver l
//  @return int
//
func (l *singlylinkedlist) deleteTail() int {
	return l.deleteNth(l.size - 1)
}


//
// deleteNth
//  @Description: 删除指定位置的元素并返回其响应的元素值
//  @receiver l
//  @param position
//  @return int
//
func (l *singlylinkedlist) deleteNth(position int) int {
	l.checkBounds(position, 0, l.size - 1)
	if position == 0 {
		destroy := l.head
		deleteValue := destroy.value
		l.head = l.head.next
		destroy = nil
		l.size--
		return deleteValue
	}

	cur := l.head
	// 找到需要删除的元素的前一个位置
	for i := 0; i < position - 1; i++ {
		cur = cur.next
	}

	destroy := cur.next
	deleteValue := destroy.value
	cur.next = cur.next.next
	destroy = nil
	l.size--
	return deleteValue
}




//
// checkBounds
//  @Description: 判断所给的position是否超过了单链表的范围
//  @receiver l
//  @param position
//  @param low
//  @param high
//
func (l *singlylinkedlist) checkBounds(position int, low int, high int) {
	if position > high || position < low {
		panic("IndexOutOfBound")
	}
}

package singlylinkedlist

import (
	"fmt"
)

type Node struct {
	value int
	next  *Node
}

func NewNode(value int, next *Node) *Node {
	return &Node{value, next}
}

type singlylinkedlist struct {
	head *Node
	size int
}

//
// NewList
//  @Description: Init singly lined list with specified head and size
//  @param head
//  @param size
//  @return *singlylinkedlist
//
func NewList(head *Node, size int) *singlylinkedlist {
	return &singlylinkedlist{head, size}
}

//
// display
//  @Description: Display the value of each node in singly linked list
//  @receiver l
//
func (l *singlylinkedlist) display() {
	if l.isEmpty() {
		fmt.Println("The list to display is empty")
	}
	cur := l.head
	for cur != nil {
		if cur.next == nil {
			fmt.Println(cur.value)
			break
		}
		fmt.Print(cur.value, "->")
		cur = cur.next
	}
}

//
// detectLoop
//  @Description: Detect there is a loop in singly linked list (using slow&fast pointer)
//  @return bool
//
func (l *singlylinkedlist) detectLoop() bool {
	slow := l.head
	fast := l.head
	for (fast != nil) && (fast.next != nil) {
		slow = slow.next
		fast = fast.next.next
		if fast == slow {
			return true
		}
	}
	return false
}

//
// reverseList
//  @Description: Reverse a singly linked list
//  @receiver l
//
func (l *singlylinkedlist) reverseList() {

	var pre *Node
	cur := l.head
	nxt := l.head

	for cur != nil {
		nxt = cur.next
		cur.next = pre
		pre = cur
		cur = nxt
	}
	l.head = pre
}


//
// clear
//  @Description: Clear all nodes in singly linked list
//  @receiver l
//  @return res
//
func (l *singlylinkedlist) clear() (res []int) {

	cur := l.head
	for cur != nil {
		prev := cur
		res = append(res, prev.value)
		cur = cur.next
		prev = nil
	}
	l.head = nil
	l.size = 0
	return res
}

//
// isEmpty
//  @Description: Check if the list is empty
//  @receiver l
//  @return bool
//
func (l *singlylinkedlist) isEmpty() bool{
	return l.size == 0
}

//
// count
//  @Description: Return the size of list
//  @receiver l
//  @return int the count of the list
//
func (l *singlylinkedlist) length() int {
	return l.size
}


//
// getHead
//  @Description: Return head of the list
//  @receiver l
//  @return *Node the head of the list
//
func (l *singlylinkedlist) getHead() *Node {
	return l.head
}


//
// setHead
//  @Description: Set head of the list
//  @receiver l
//  @param node
//
func (l *singlylinkedlist) setHead(node *Node) {
	l.head = node
}

//
// count
//  @Description: Calculate count of the list manually
//  @receiver l
//  @return int the count of the list
//
func (l *singlylinkedlist) count() int {
	count := 0
	cur := l.head
	for cur != nil {
		count++
		cur = cur.next
	}
	return count

}

//
// search
//  @Description: Test if the value key is present in the list
//  @receiver l
//  @param key the value to be searched
//  @return bool {@code true} if key is present in the list, otherwise {@code false}
//
func (l *singlylinkedlist) search(key int) bool {
	cur := l.head
	for cur != nil {
		if cur.value == key {
			return true
		}
		cur = cur.next
	}
	return false
}

//
// getNth
//  @Description: Return element at special index
//  @receiver l
//  @param index
//  @return int
//
func (l *singlylinkedlist) getNth(index int) int {
	l.checkBounds(index, 0, l.size - 1)
	cur := l.head
	for i := 0; i < index; i++ {
		cur = cur.next
	}
	return cur.value
}


//
// insertHead
//  @Description: Insert an element at the head of the list
//  @receiver l
//  @param x
//
func (l *singlylinkedlist) insertHead(x int) {
	l.insertNth(x, 0)
}


//
// insertTail
//  @Description: Insert an element at the tail of the list
//  @receiver l
//  @param data
//
func (l *singlylinkedlist) insertTail(data int) {
	l.insertNth(data, l.size)
}



//
// insertNth
//  @Description: Insert a new node at a specified position of the list
//  @receiver l
//  @param data
//  @param position
//
func (l *singlylinkedlist) insertNth(data int, position int) {
	l.checkBounds(position, 0, l.size)
	newNode := NewNode(data, nil)
	if l.head == nil {
		l.head = newNode
		l.size++
		return
	}
	if position == 0 {
		newNode.next = l.head
		l.head = newNode
		l.size++
		return
	}

	cur := l.head
	for i := 0; i < position - 1; i++ {
		cur = cur.next
	}
	newNode.next = cur.next
	cur.next = newNode
	l.size++
}

//
// deleteHead
//  @Description: Delete a node at the head
//  @receiver l
//  @return int
//
func (l *singlylinkedlist) deleteHead() int {
	return l.deleteNth(0)
}


//
// deleteTail
//  @Description: Delete an element at the tail
//  @receiver l
//  @return int
//
func (l *singlylinkedlist) deleteTail() int {
	return l.deleteNth(l.size - 1)
}


//
// deleteNth
//  @Description: Delete an element at Nth position and return the deleted value
//  @receiver l
//  @param position
//  @return int
//
func (l *singlylinkedlist) deleteNth(position int) int {
	l.checkBounds(position, 0, l.size - 1)
	if position == 0 {
		destroy := l.head
		deleteValue := destroy.value
		l.head = l.head.next
		destroy = nil
		l.size--
		return deleteValue
	}

	cur := l.head
	// To find the node before the specified position
	for i := 0; i < position - 1; i++ {
		cur = cur.next
	}

	destroy := cur.next
	deleteValue := destroy.value
	cur.next = cur.next.next
	destroy = nil
	l.size--
	return deleteValue
}




//
// checkBounds
//  @Description: Check if the position in the range
//  @receiver l
//  @param position
//  @param low
//  @param high
//
func (l *singlylinkedlist) checkBounds(position int, low int, high int) {
	if position > high || position < low {
		panic("IndexOutOfBound")
	}
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值