Data structure — one-way linked list

Time:2021-10-24

A go language implementation of unidirectional linked list

package main

import "fmt"

type Node struct {
    no int
    name string
    next *Node
}

//Insert a node into the linked list at the end of the implementation (an implementation of queue push)
func InsertTail(head *Node, newNode *Node){
    //First find the last node of the linked list. You need to keep the head node still. Here, create an intermediate temporary node
    temp := head
    //Traverse the linked list until the last node is inserted
    for {
        If temp. Next = = nil {// this condition is the last position of the linked list
            break
        }
        temp = temp.next
    }
    //Connect the new node to the end of the linked list
    temp.next = newNode
}

//Realize orderly insertion of a node into the linked list
func InsertSort(head, newNode *Node){
    temp := head
    //Important: when inserting, you must compare temp.next.no and newnode.no at the position of temp, otherwise you will miss the insertion opportunity
    for {
        If temp. Next = = nil {// indicates that it is at the end of the linked list
            //Note: the order of the following two lines cannot be reversed
            newNode.next = temp.next
            temp.next = newNode
            break
        } else if temp.next.no >= newNode.no {
            newNode.next = temp.next
            temp.next = newNode
            break
        }
        temp = temp.next
    }
}

//Implement header insertion node (an implementation of queue push)
func InsertHead(head, newNode *Node){
    newNode.next = head.next
    head.next = newNode
}

//Implement a function to delete linked list nodes
func Delete(head *Node, node *Node) {
    temp := head
    for {
        If temp. Next = = nil {// indicates that it is at the end of the linked list,没有找到要删除的节点
            break
        } else if temp.next.no == node.no {
            temp.next = node.next
            //The following method is also OK, but it's a little confusing
            //temp.next = temp.next.next
            break
        }
        temp = temp.next
    }
}

//Implement a function to delete linked list nodes in the header (an implementation of queue pop)
func DeleteHead(head *Node){
    if head.next == nil{
        return
    }else {
        head.next = head.next.next
    }
}

//Implement a function to delete linked list nodes at the end (an implementation of queue pop)
func DeleteTail(head *Node){
    temp := head
    for {
        If temp. Next = = nil{// Description: the linked list is empty
            return
        }else if temp.next.next == nil{
            temp.next = nil
            break
        }
        temp = temp.next
    }
}

//Realize the display of all node information in the linked list
func List(head *Node){
    //The premise is that the header node cannot be changed
    temp := head
    if temp.next == nil {
        Fmt.println ("the linked list is empty")
        return
    }

    for {
        FMT. Printf ("% d% s -- >", temp.next.no, temp. Next. Name) // print the information of the next node
        temp = temp.next
        if temp.next == nil {
            break
        }
    }
}

func main(){

    //Define a header node
    head := &Node{}
    //Define a node information
    node1 := &Node{
        no:   1,
        name: "Number1",
        next: nil,
    }
    node2 := &Node{
        no:   2,
        name: "Number2",
        next: nil,
    }
    node3 := &Node{
        no:   3,
        name: "Number3",
        next: nil,
    }
    node4 := &Node{
        no:   2,
        name: "Number4",
        next: nil,
    }

    InsertTail(head, node1)
    InsertTail(head, node2)

    InsertSort(head, node3)
    InsertSort(head, node2)
    InsertSort(head, node1)
    InsertSort(head, node4)

    Delete(head, node4)

    InsertHead(head, node1)
    InsertHead(head, node2)
    InsertHead(head, node3)
    InsertHead(head, node4)
    DeleteHead(head)
    DeleteTail(head)
    List(head)
}