go realize one-way linked list

Posted by jnutter on Sun, 19 Dec 2021 16:32:21 +0100

golang implements one-way linked list

Introduction to one-way linked list

Unidirectional linked list (single linked list) is a kind of linked list, which is characterized by the unidirectional link direction of the linked list. Each element of the unidirectional linked list is called a node. Each node can be anywhere in memory. The node contains data field and pointer field. The data field is the part of the node that stores data elements. The pointer field is the part of the address of the next node.

Head pointer

In a one-way linked list, there is no precursor node in front of the first node. It is the starting address of the whole linked list. If you want to operate the node, you must have a pointer variable pointing to the node, which is called the head pointer.
The head pointer has the function of identification, so it is often preceded by the name of the linked list. The access to the single linked list must start from the pointer. Since the last node of the single linked list has no direct successor, the pointer is NULL.

Head node

In order to unify the operation, the header pointer is not a null pointer when the first element node of the operation is null. A header node is often added before the first node of the linked list. The header node is the same as other node types, but the data field is generally meaningless. In some cases, the data field of the head node can place the information of the whole linked list, such as the length of the whole linked list.
The head pointer points to the head node, and the pointer field of the head node points to the first valid node.

The head pointer is a necessary element of the linked list, and the head node is not necessary for the linked list

golang implements one-way linked list

package main

import (
	"fmt"
	"testing"
)

//Structure defining nodes
type LinkNode struct {
	Data    interface{} //Data domain
	Next    *LinkNode	//Pointer field
}

//Define linked list structure
type LinkList struct {
	HeadPointer  *LinkNode	//Head node
}

//Create a node
func NewLinkNode(v interface{}) *LinkNode {
	return &LinkNode{v, nil}
}

//Create header pointer
func NewLinkList() *LinkList {
	//Create a head node and save the length of the linked list using the data field of the head node
	headPointer := &LinkNode{
		Data: 0,
		Next: nil,
	}
	return &LinkList{headPointer}
}

//Get linked list length
func(linkList *LinkList) GetListLength() int {
	return  linkList.HeadPointer.Data.(int)
}

//The length of the linked list increases automatically
func (linkList *LinkList)incListLength()  {
	linkList.HeadPointer.Data = linkList.HeadPointer.Data.(int) + 1
}

//The length of the linked list increases automatically
func (linkList *LinkList)decListLength()  {
	linkList.HeadPointer.Data = linkList.HeadPointer.Data.(int) - 1
}

//Append nodes to the end of the linked list
func (linkList *LinkList)AppendNode(v interface{})  {
	newNode := NewLinkNode(v)
	cuutentNode := linkList.HeadPointer
	for cuutentNode.Next != nil {
		cuutentNode = cuutentNode.Next
	}
	cuutentNode.Next = newNode
	linkList.incListLength()
}

//Add a node at the specified location in the linked list
func (linkList *LinkList) Insert(i int, v interface{}) {
	newNode := NewLinkNode(v)
	cuutentNode := linkList.HeadPointer

	//Save the pointer field of the previous node in the pointer field of this node,
	//Write the address of the new node into the pointer field of the previous node
	for j:=0; j<=i; j++ {
		if j == i-1 {
			newNode.Next = cuutentNode.Next
			cuutentNode.Next = newNode
			linkList.incListLength()
		}
		cuutentNode = cuutentNode.Next
	}
}

//Delete specified node
func (linkList *LinkList) Remove(v interface{})  {
	currentNode := linkList.HeadPointer
	//Remove the address of the specified node from the linked list
	for currentNode.Next != nil {
		if currentNode.Next.Data == v {
			currentNode.Next = currentNode.Next.Next
			linkList.decListLength()
		}
		currentNode = currentNode.Next
	}
	return
}

//Traversal linked list
func (linkList *LinkList) Range()  {
	currentNode := linkList.HeadPointer
	fmt.Println("Start traversing the linked list")
	i := 1
	for currentNode.Next != nil {
		fmt.Printf("node : %d ,data : %v \n" ,i,currentNode.Next.Data)
		i++
		currentNode = currentNode.Next
	}
	fmt.Println("End of traversal of linked list")
}

func TestList(t *testing.T) {
	list := NewLinkList()
	list.AppendNode("hello 2")
	list.AppendNode("hello 3")
	list.Insert(1,"hello 1")
	list.Range()
	list.Remove("hello 1")
	list.Range()
}


Topics: Go data structure linked list Singly Linked List