1.Go-copy function, sort order, doubly linked list, list operations, and bidirectional circular list

1.1.copy function

Function can be copied by a copy another slice to slice content

(1) The length of the short sections copied to the slice

main Package 

Import "FMT" 

FUNC main () { 
	S1: = [] int {1,2} 
	S2: = [] int {3,4,5,6} 
	// Copy subscript is, without increasing the slice meta the length 
	Copy (S1, S2) 
	fmt.Println (S1) // [. 4. 3] 
	fmt.Println (S2) // [. 5. 4. 3. 6] 
}

(2) The short slice copied into long sections 

main Package 

Import "FMT" 

FUNC main () { 
	S1: = [] int {1,2} 
	S2: = [] int {3,4,5,6} 
	// Copy subscript is, without increasing the slice meta the length 
	Copy (S2, S1) 
	fmt.Println (S1) // [2. 1] 
	fmt.Println (S2) // [2. 5. 6. 1] 
}

(3) copied to the slice sections fragment

main Package 

Import "FMT" 

FUNC main () { 
	S1: = [] int {1,2} 
	S2: = [] int {3,4,5,6} 
	// Copy subscript is, without increasing the slice meta the length 
	Copy (S1, S2 [. 1:. 3]) 
	fmt.Println (S1) // [[. 4. 5] 
	fmt.Println (S2) // [. 5. 4. 3. 6] 
}

Sort 1.2.sort

main Package 

Import ( 
	"FMT" 
	"Sort" 
) 

FUNC main () { 
	NUM: = [] int {1,7,3,5,2} 
	// ascending order 
	sort.Ints (NUM) 
	fmt.Println (NUM) / / [12357] 

	// Sort descending 
	sort.Sort (sort.Reverse (sort.IntSlice (NUM))) 
	fmt.Println (NUM) // [2. 7. 5. 3. 1] 
}

1.3. Doubly linked list

 (1) two-way linked list structure

Doubly linked list structure elements in memory is not close to the space, but each element in an address on an element and a rear element storage

  • The first element is called a (head) element, connecting the front (front pointer domain) is nil
  • The last element is called a tail (Foot) element, the connection (rear pointer domain) tail nil

 The advantages of a doubly linked list

  • In the implementation of new elements or remove elements aging rate and get any one element, it can easily insert elements around the element
  • Full use of the memory space to achieve flexible memory management
  • It can achieve positive and reverse traversal
  • When you add or delete elements of the head and tail elements more efficient

  Doubly linked list of shortcomings

  •  List pointer field adds an element of the space overhead is relatively large
  • While traversing the jump to find content, a large amount of data traversing the low performance

 (2) doubly linked list container List

In the container Go language standard library / list package provides a doubly linked list List

List structure is defined as follows

  • root represents the root element
  • len represents how many elements in the list
// List represents a doubly linked list.
// The zero value for List is an empty list ready to use.
type List struct {
	root Element // sentinel list element, only &root, root.prev, and root.next are used
	len  int     // current list length excluding (this) sentinel element
}

 Wherein the structure is defined as follows Element

  • It represents a next element, using the Next () can be acquired
  • prev denotes a element, Prev, using () can be acquired
  • list represents the elements which belong to the list
  • Value represents the value of the element, interface () represents any type, in Go 
// Element is an element of a linked list.
type Element struct {
	// Next and previous pointers in the doubly-linked list of elements.
	// To simplify the implementation, internally a list l is implemented
	// as a ring, such that &l.root is both the next element of the last
	// list element (l.Back()) and the previous element of the first list
	// element (l.Front()).
	next, prev *Element

	// The list to which this element belongs.
	list *List

	// The value stored with this element.
	Value interface{}
}

1.4. Operation List

(1) New directly under the container / list pack () Create an empty List

Add, traverse, taking inclusive, take intermediate element

package main

import (
	"container/list"
	"fmt"
)

func main() {
	//实例化
	mylist := list.New()
	fmt.Println(mylist)

	//添加
	mylist.PushFront("a")                        //["a"]
	mylist.PushBack("b")                         //["a","b"]
	mylist.PushBack("c")                         //["a","b","c"]
	//在最后一个元素的前面添加
	mylist.InsertBefore("d",mylist.Back())       //["a","b","d","c"]
	mylist.InsertAfter("e",mylist.Front())       //["a","e","b","d","c"] 
	/ / take inclusive
	fmt.Println ( "")
	}
		fmt.Print (e.Value, "") // aebdc
	for E:! = Mylist.Front (); E = nil; E = e.next () {

	// Loop

	fmt.Println (mylist.Front () the Value.) // A 
	fmt.Println (mylist.Back () the Value.) // C 

	// taking the middle element, through constant the Next () 
	n-: =. 3 
	var Curr * list.Element 
	IF n-> n-0 && <= mylist.Len () { 
		IF. 1 n-== { 
			Curr = mylist.Front () 
		} n-== mylist.Len the else IF () { 
			Curr = mylist.Back () 
		} {the else 
			Curr mylist.Front = () 
			for I: =. 1; I <n; I ++ { 
				Curr = curr.Next () 
			} 
		} 
	} the else { 
		fmt.Println ( "the wrong value of n") 
	} 
	fmt.Println (Curr .Value) // B 
}

(2) moving element 

package main

import (
	"container/list"
	"fmt"
)

func main() {
	//实例化
	mylist := list.New()
	fmt.Println(mylist)

	//添加
	mylist.PushFront("a")                        //["a"]
	mylist.PushBack("b")                         //["a","b"]
	mylist.PushBack("c")                         //["a","b","c"]
	//在最后一个元素的前面添加
	mylist.InsertBefore("d",mylist.Back())       //["a","b","d","c"]
	mylist.InsertAfter("e",mylist.Front())       //["a","e","b","d","c"] 
	// the last element to move to the front
	//mylist.MoveAfter(mylist.Back (), mylist.Front ())
	mylist.MoveBefore (mylist.Front (), mylist.Back ())

	// move the first element in front of a last element
	
	//mylist.MoveToFront (mylist.Back ()) 
	// the first element is moved to the rearmost 
	//mylist.MoveToBack (mylist.Front ()) 

	for E: = mylist.Front (); E = nil;! e.next = E () { 
		fmt.Print (e.Value, "") // ebdac 
	} 
}

(3) Delete

mylist.Remove(mylist.Front())

1.5. Two-way circulation list

(1) features a circular linked list node pointer field is not to nil, any element other elements can be found

Circular linked list structure is as follows

And two-way circular list doubly linked list difference

  • Two-way circular list is not the first element in the strict sense and the last element
  • Front and rear connecting element is not connected to nil
  • A cycle of length n two-way linked list, moving through an element in a certain direction, in the look up to n-1 times, will find another element

(2) in the container / ring Ring source packet structure below

  • Official clearly illustrates the Ring cycle are the elements of the list, but also a circular linked list
  • Ring traversing element is a circular linked list, the first practical use
// A Ring is an element of a circular list, or ring.
// Rings do not have a beginning or end; a pointer to any ring element
// serves as reference to the entire ring. Empty rings are represented
// as nil Ring pointers. The zero value for a Ring is a one-element
// ring with a nil Value.
//
type Ring struct {
	next, prev *Ring
	Value      interface{} // for use by client; untouched by this library
}

(3) Creating and Viewing  

main Package 

Import ( 
	"Container / Ring" 
	"FMT" 
) 

FUNC main () { 
	// R & lt representative of the circular linked list, and represents the first element of 
	R & lt: as ring.New = (. 5) 
	r.Value = 0 
	r.Next ( ) .Value =. 1 
	r.Next (). the Next (). the Value = 2 
	//r.Next().Next().Next().Value =. 3 
	//r.Next().Next().Next () .next (). Value = 4 
	r.Prev (). Value = 4 
	r.Prev (). Prev, (). Value = 3 
	// Check the element content 
	// circular list has several elements, func on the implementation of several times, i the current content execution element 
	r.Do (FUNC (interface I {}) { 
		fmt.Print (I, "") // 2. 3. 1 0. 4 
	}) 
	fmt.Println ( "") 
	// takes an intermediate element by moving 
	fmt.Println (r.Move (. 3) .Value) //. 3 


}

(4) increase

main Package 

Import ( 
	"Container / Ring" 
	"FMT" 
) 

FUNC main () { 
	// R & lt representative of the circular linked list, and represents the first element of 
	R & lt: as ring.New = (. 5) 
	r.Value = 0 
	r.Next ( ) .Value =. 1 
	r.Next (). the Next (). the Value = 2 
	//r.Next().Next().Next().Value =. 3 
	//r.Next().Next().Next . () .NEXT () = the Value. 4 
	r.Prev () = the Value. 4. 
	r.Prev () Prev, () = the Value. 3.. 

	// increase 
	R1: = as ring.New (2) 
	r1.Value. 5 = 
	R1 .NEXT (). the Value =. 6 
	r.Link (R1) 

	r.Do (FUNC (interface I {}) { 
		fmt.Print (I, "") // 0. 6. 5. 4. 3. 1 2 
	}) 
}

(5) Delete

main Package 

Import ( 
	"Container / Ring" 
	"FMT" 
) 

FUNC main () { 
	// R & lt representative of the circular linked list, and represents the first element of 
	R & lt: as ring.New = (. 5) 
	r.Value = 0 
	r.Next ( ) .Value =. 1 
	r.Next (). the Next (). the Value = 2 
	//r.Next().Next().Next().Value =. 3 
	//r.Next().Next().Next () .NEXT (). the Value =. 4 
	r.Prev (). the Value =. 4 
	r.Prev (). Prev, (). the Value =. 3 

	// delete 
	r.Unlink (. 1) 

	r.Do (FUNC (I interface { }) { 
		fmt.Print (I, "") // 0 2. 3. 4 
	}) 
}

Remove two behind

//删除
	r.Unlink(2)

	r.Do(func(i interface{}) {
		fmt.Print(i, " ")    //0 3 4 
	})

r.Next () Delete

//删除
	r.Next().Unlink(2)

	r.Do(func(i interface{}) {
		fmt.Print(i, " ")    //0 1 4 
	})qu  

Out of range, take the remainder 5

//删除
	r.Unlink(6)

	r.Do(func(i interface{}) {
		fmt.Print(i, " ")    //0 2 3 4
	})

  

Guess you like

Origin www.cnblogs.com/derek1184405959/p/11298618.html