Brother Lian Blockchain training tutorial share go language Golang single link list implementation

Source: Internet
Author: User

At present, blockchain is a new frontier industry, but it is also a subject with complex complexity, and learning blockchain needs a certain learning ability and knowledge base. However, many offline training institutions are only charged with high registration fees, the actual situation of users ignored, do not set the threshold for registration, not screening screening, in fact, is an irresponsible attitude.

Brother even education go full stack and Blockchain training courses have changed from multi-level to traditional training institutions to operate thinking, the opening of the blockchain curriculum to a certain extent, increased public awareness of this area of expertise, and its construction of the blockchain world will be in the future for us to show a more efficient way of life.


Package Main


Linked list implementation

Import (

"FMT"

"OS"

)


Defining error Constants

Const (

ERROR =-1000000001

)


Defining element types

Type Element Int64


Defining nodes

Type Linknode struct {

Data Element//Field

Nest *linknode//Pointer field, pointing to the next node

}


Function interface

Type Linknoder Interface {

Add (Head *linknode, new *linknode)//Added later

Delete (head *linknode, index int)//deletes the specified index position element

Insert (head *linknode, index int, data Element)//Inserts an element at the specified index position

GetLength (head *linknode) int//Get length

Search (head *linknode, data Element)//Query element location

GetData (head *linknode, index int) element//Gets the elements at the specified index position

}


Add head node, data

Func Add (head *linknode, data Element) {

Point: = head//temporary pointer

For Point. Nest! = Nil {

Point = point. Nest//Shift

}

var node linknode//New node

Point. Nest = &node//Assignment

Node. data = Data


Head. data = Element (GetLength (head))//Print all


If GetLength (head) > 1 {

Traverse (head)

}


}


Delete Head node index position

Func Delete (head *linknode, index int) Element {

Judging the validity of index

If index < 0 | | Index > GetLength (head) {

Fmt. Println ("Please check index")

Return ERROR

} else {

Point: = Head

For I: = 0; i < index-1; i++ {

Point = point. Nest//Shift

}

Point. Nest = point. Nest.nest//Assignment

Data: = point. Nest.data

Return data

}

}


Insert Head node index position data element

Func Insert (head *linknode, index int, data Element) {

Verify index legitimacy

If index < 0 | | Index > GetLength (head) {

Fmt. Println ("Please check index")

} else {

Point: = Head

For I: = 0; i < index-1; i++ {

Point = point. Nest//Shift

}

var node linknode//New node, assignment

Node. data = Data

Node. Nest = point. Nest

Point. Nest = &node

}

}


Get the length head node

Func getlength (head *linknode) int {

Point: = Head

var length int

For Point. Nest! = Nil {

length++

Point = point. Nest

}

return length

}


Search head Node Data element

Func Search (head *linknode, data Element) {

Point: = Head

Index: = 0

For Point. Nest! = Nil {

If point. data = = Data {

Fmt. PRINTLN (data, "exist at", Index, "th")

Break

} else {

index++

Point = point. Nest

If Index > GetLength (Head)-1 {

Fmt. PRINTLN (data, "not exist at")

Break

}

Continue

}

}

}


Get Data Header node index position

Func GetData (head *linknode, index int) Element {

Point: = Head

If index < 0 | | Index > GetLength (head) {

Fmt. Println ("Please check index")

Return ERROR

} else {

For I: = 0; I < index; i++ {

Point = point. Nest

}

Return point. Data

}

}


Traverse the head node.

Func Traverse (head *linknode) {

Point: = head. Nest

For Point. Nest! = Nil {

Fmt. Println (point. Data)

Point = point. Nest

}

Fmt. Println ("Traverse ok!")

}


Main function test

Func Main () {

var head linknode = linknode{data:0, Nest:nil}

Head. Data = 0

var Nodearray []element

For I: = 0; I < 10; i++ {

Nodearray = Append (Nodearray, Element (i+1+i*100))

ADD (&head, Nodearray[i])


}


Delete (&head, 3)

Search (&head, 2032)

Insert (&head, 23, 10010)

Traverse (&head)

Fmt. PRINTLN ("Data is", GetData (&head, 6))

Fmt. Println ("Length:", GetLength (&head))

Os. Exit (0)

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.