Following the one-way linked list, the list can be further expanded to singlet ring, as shown below:
Features:
1, the first node is called a head node, tail node of the last node called
2, each node point to the next node unilaterally
3, the lower tail portion of a node points to the head node
topic:
17th century French mathematician Gaspar told such a story: 15 Christians and 15 non-believers, in the deep distress at sea, half of the people must be put into the Sea, and the rest of the people in order to survive, so the thought of a Measures: 30 people surrounded by a circle, began to count off in order from the first person, per count to the ninth individual he will be thrown into the great Sea, and so on until leaving only 15 people so far. Q. What row Act, to make every big investment Sea are non-believers.
This is a typical problem Joseph ring, the ring can be solved by way linked list, specific code as follows:
Package main Import "FMT" type LinkNode struct { the Data interface {} the Next * LinkNode } type SingleLink struct { head * LinkNode tail * LinkNode size int } // initialization list FUNC InitSingleLink () (* SingleLink) { return & SingleLink { head: nil , tail: nil, size: 0, } } // Get the head node FUNC (SL * SingleLink) GetHead () * {LinkNode return sl.head } // Get tail node func (sl * SingleLink) getTail ( ) * LinkNode { return sl.tail } // print list func (sl * SingleLink) Print ( ) { fmt.Println ( "SingleLink size:", sl.Length ()) IF == 0 {sl.size return } PTR: = sl.GetHead () HeadNode: = sl.GetHead () ! = nil for PTR { fmt.Println ( "the Data:", ptr.Data) PTR = ptr.Next IF ptr.Next == {HeadNode fmt.Println ( "the data:", ptr.Data) BREAK } } } // chain length FUNC (SL * SingleLink) the length () {int return sl.size } // inserting data (head plug) FUNC (SL * SingleLink ) InsertByHead (node * LinkNode) { iF node nil == { return } // check if the first node iF sl.Length () == 0 { sl.head = node sl.tail = node node.Next = nil } the else { oldHeadNode: = sl.GetHead () sl.head Node = sl.tail.Next = Node sl.head.Next = oldHeadNode } sl.size ++ } // inserting data (tail plug) FUNC (SL * SingleLink) InsertByTail (Node * LinkNode) { IF Node nil == { return } // insert the first node IF sl.size == 0 { sl.head = node sl.tail = node node.Next = nil } the else { sl.tail.Next = node node.Next = sl.head SL. Node = tail } sl.size ++ } // insert data (index) position FUNC (SL * SingleLink) insertByIndex (int index, LinkNode Node *) { if Node nil == { return } // inserted into the head if index == 0 { sl.InsertByHead ( node) } the else { IF index> sl.Length () { return } sl.Length the else IF index == () { // add the node to the tail sl.InsertByTail (Node) } the else { preNode: SL = .Search (index-1) // index of a node of index currentNode: = sl.Search (index) // subscript index nodes preNode.Next = node node.Next = the currentNode sl.size ++ } } } // delete data (index) position FUNC (SL * SingleLink) DeleteByIndex (int index) { if sl.Length () == 0 || index> sl.Length () { return } // delete the first node if {0 == index sl.head = sl.head.Next sl.tail.Next = sl.head } the else { preNode: = sl.Search (-index. 1) if index != sl.Length()-1{ nextNode := sl.Search(index).Next preNode.Next = nextNode }else{ sl.tail = preNode preNode.Next = sl.head } } sl.size-- } // 查询数据 func (sl *SingleLink) Search(index int)(node *LinkNode) { if sl.Length() == 0 || index > sl.Length(){ return nil } // 是否头部节点 if index == 0{ return sl.GetHead() } node = sl.head for i:=0;i<=index;i++{ node = node.Next } return } func (sl *SingleLink)pop(){ popIndex := 8 delNode: = sl.Search (popIndex) // winners fmt.Println ( "the POP Node:", delNode.Data) sl.DeleteByIndex (popIndex) sl.tail = sl.Search (popIndex -. 1) sl.head = sl.Search (popIndex) fmt.Printf ( "Head:% V, Tail: V% \ n-", sl.head.Data, sl.tail.Data) } FUNC main () { // initialization list SL: = InitSingleLink () // generates a ring element 30 for i: = 0; I <30; I ++ { SNODE: = {& LinkNode the Data: I, } sl.InsertByIndex (I, SNODE) } // loop out of the 9 elements var int round for sl.size> 15 { fmt.Printf ( " % Round D ================ ================ \ n-", round) sl.pop () round ++ } fmt.Println ( "================ Finish ================") fmt.Println("People who survived.") sl.Print() }
Results of the
================ Round 0 ================ POP node : 9 Head:10 , Tail:8 ================ Round 1 ================ POP node : 19 Head:20 , Tail:18 ================ Round 2 ================ POP node : 29 Head:0 , Tail:28 ================ Round 3 ================ POP node : 10 Head:11 , Tail:8 ================ Round 4 ================ POP node : 21 Head:22 , Tail:20 ================ Round 5 ================ POP node : 2 Head:3 , Tail:1 ================ Round 6 ================ POP node : 14 Head:15 , Tail:13 ================ Round 7 ================ POP node : 26 Head:27 , Tail:25 ================ Round 8 ================ POP node : 8 Head:11 , Tail:7 ================ Round 9 ================ POP node : 23 Head:24 , Tail:22 ================ Round 10 ================ POP node : 6 Head:7 , Tail:5 ================ Round 11 ================ POP node : 22 Head:24 , Tail:20 ================ Round 12 ================ POP node : 7 Head:11 , Tail:5 ================ Round 13 ================ POP node : 25 Head:27 , Tail:24 ================ Round 14 ================ POP node : 13 Head:15 , Tail:12 ================ Finish ================ People who survived. SingleLink size: 15 Data: 15 Data: 16 Data: 17 Data: 18 Data: 20 Data: 24 Data: 27 Data: 28 Data: 0 Data: 1 Data: 3 Data: 4 Data: 5 Data: 11 Data: 12