Data structure — circular linked list

Time：2021-10-21

A go language implementation of ring linked list

package main

import "fmt"

//Defines the node structure of a ring linked list
id int
name string
}

//Insert node into ring linked list
//Determine whether it is an empty linked list
//If it is empty, the first element added will be given to the header node. This is different from other linked lists. Other chain header nodes are empty
//The key point is to make a node also form a ring, that is, the ending is connected
return
}
for{
temp.next = newnode
break
}
temp = temp.next
}
}

//Displays all node information of the ring linked list
//Determine whether the linked list is empty
Fmt.println ("the linked list is empty")
return
}

for {
fmt.Printf("[%d  %s] -> ", temp.id, temp.name)
//Note that this is how to judge the termination condition
break
}
temp = temp.next
}
}

//Delete a node of the ring linked list (difficulty)
Func circledelete (head, node * circlesinglelink) * circlesinglelink {// the reason for the return value is that the value and direction of the head node will change
//Delete idea:
//Let temp point to head first
//Then let the helper point to the end of the ring linked list
//Compare temp with the node to be deleted. If it is the same, ask the helper to delete the node. The key point is to consider whether it is a head node, because the head node of the ring linked list has a value

If temp. Next = = nil {// when the linked list is empty
Fmt.println ("the linked list is empty")
}
temp.next = nil
}
for {
break
}
Helper = helper. Next // locate the pointer to the tail node of the ring linked list
}

//If there are two or more nodes
for {
//It indicates that the last node has not been determined whether it is the node to be deleted
if temp.id == node.id {
helper.next = temp.next
}else {
Fmt.println ("no such node")
}
break
}
If temp. Id = = node. ID {// the node to be deleted is found
If temp = = head {// if the head node is deleted
}
Helper.next = temp.next // Helper is always the last one of temp
break
}
Temp = temp. Next // used for comparison
Helper = helper. Next // used for operation
}

}

func main(){

//Define the first node
id: 1,
name : "number1",
}
id: 2,
name : "number2",
}
id: 3,
name : "number3",
}
}

Joseph question:

package main

import "fmt"

type Boy struct {
id int
next *Boy
}

First: = & boy {} // the header pointer cannot be moved, so an auxiliary pointer is required for loop creation
curBoy := &Boy{}

if num < 1 {
return first
}
for i := 1; i <= num; i++ {
boy := &Boy{
id:   i,
next: nil,
}
If I = = 1 {// because the first child is special
first = boy
curBoy = boy
curBoy.next = first
}else {
curBoy.next = boy
curBoy = boy
Curboy. Next = first // form a circular linked list
}
}
return first
}

func ShowBoy (first *Boy) {
if first.next == nil {
Fmt.println ("the linked list is empty")
return
}
curBoy := first
for {
FMT. Printf ("child% d - >", curboy. ID)
if curBoy.next == first {
break
}
curBoy = curBoy.next
}
}

//Joseph problem using ring linked list
func PlayGame(first *Boy, startNo int, countNum int) {
if first.next == nil {
Fmt.println ("the linked list is empty, the game is over")
return
}
//Define two pointer loops, where first is responsible for comparison and tail is responsible for deletion
tail := first
//Locate the tail correction to the end of the linked list
for {
if tail.next == first {
break
}
tail = tail.next
}
//Start moving startno-1
for i := 0; i < startNo -1; i++ {
first = first.next
tail = tail.next
}
fmt.Println()
//Start circular deletion of the countnum-1 node in the ring linked list
for {
for i := 0; i < countNum - 1; i++ {
first = first.next
tail = tail.next
}
//Print the node information about the circle
FMT. Printf ("node number% d out of circle \ n", first. ID)
//Delete the current node that first points to at this time
first = first.next
tail.next = first
//Determine end condition
if tail == first {
break
}
}
//Print the last circled node
FMT. Printf ("node number% d out of circle \ n", first. ID)
}

func main(){