# 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
type circleSingleLink struct {
id int
name string
next *circleSingleLink
}

//Insert node into ring linked list
func Insert(head, newnode *circleSingleLink) {
//Determine whether it is an empty linked list
if head.next == nil{
//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
head.id = newnode.id
head.name = newnode.name
//The key point is to make a node also form a ring, that is, the ending is connected
head.next = head
return
}
//Add non head node to ring linked list
temp := head
for{
if temp.next == head {
//Tail addition
temp.next = newnode
newnode.next = head
break
}
temp = temp.next
}
}

//Displays all node information of the ring linked list
func CircleList(head *circleSingleLink){
//Determine whether the linked list is empty
if head.next == nil {
Fmt.println ("the linked list is empty")
return
}

temp := head
for {
fmt.Printf("[%d  %s] -> ", temp.id, temp.name)
//Note that this is how to judge the termination condition
if temp.next == head{
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

temp := head
If temp. Next = = nil {// when the linked list is empty
Fmt.println ("the linked list is empty")
return head
}
If temp. Next = = head {// a circular linked list with only one head node
temp.next = nil
return head
}
helper := head
for {
if helper.next == head {
break
}
Helper = helper. Next // locate the pointer to the tail node of the ring linked list
}

//If there are two or more nodes
for {
if temp.next == head {
//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
head = head.next
}
//Delete non header node
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
}

return head
}

func main(){
//Define the header of a linked list
head := &circleSingleLink{}

//Define the first node
node1 := &circleSingleLink{
id: 1,
name : "number1",
}
node2 := &circleSingleLink{
id: 2,
name : "number2",
}
node3 := &circleSingleLink{
id: 3,
name : "number3",
}
Insert(head, node1)
Insert(head, node2)
Insert(head, node3)
head = CircleDelete(head, node1)
CircleList(head)
}``````

Joseph question:

``````package main

import "fmt"

type Boy struct {
id int
next *Boy
}

//Create a circular linked list and return the header pointer
func CreateLink(num int) *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
}

//Show circular linked list
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) {
//Empty linked list
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(){
first := CreateLink(51)
ShowBoy(first)
PlayGame(first, 2, 3)
}``````

## Web vulnerability scanner

Web vulnerability scanner should include three basic functions: crawling, leaking and reporting Crawl, crawl web page Leak, scan for possible vulnerabilities Report and generate the final report