Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use linked list in golang basic data structure and algorithm

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces "golang basic data structure and algorithm how to use linked list". In daily operation, I believe many people have doubts about how to use linked list in golang basic data structure and algorithm. Xiaobian consulted all kinds of information and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the doubt of "how to use linked list in golang basic data structure and algorithm". Next, please follow the editor to study!

Linked list is one of the data structures, in which the data is arranged linearly. Each data node has a "pointer" that points to the memory address of the next data. When accessing the data, we need to start with the head of the linked list (linear lookup), and if the target data is at the end of the linked list, the time required is O (n). In addition, adding data only needs to change the pointing of the two pointers, so the time spent has nothing to do with n. If you have reached the location where the data was added, the add operation only takes O (1) time. It also takes only O (1) to delete the data. Excerpt from ([Japan] Ishida Baohui; Miyazaki Shoichi) goal

Implement a linked list that provides linear access interfaces similar to arrays

Design

ILinkedList: linked list interface

IListIterator: linked list iterator interface

TLinkedList: linked list, implementing ILinkedList interface

TListIterator: list iterator to implement IListIterator interface

Unit testing

Linked_list_test.go

Package data_structureimport ("fmt"learning/gooop/data_structure/linked_list"strings"testing") func Test_LinkedList (t * testing.T) {fnAssertTrue: = func (b bool) Msg string) {if! b {panic (msg)} list: = linked_list.NewLinkedList () state: = list.String () t.Log (state) fnAssertTrue (state = = "hagnilmeng tinci niljournal 0" "expecting empty list") list.Append (0) state = list.String () t.Log (state) fnAssertTrue (state = "expecting [0]") list.Append (1) state = list.String () t.Log (state) fnAssertTrue (state) It: = list.Get (0) t.Log (it) fnAssertTrue (e = = nil, "expecting e = = nil") fnAssertTrue (it = = 0, "expecting 0") epeny it = list.Get (1) t.Log (it) fnAssertTrue (e = = nil, "expecting e = = nil") fnAssertTrue (it = = 1, "expecting 1") e = list.Set (0) 10) state = list.String () t.Log (state) fnAssertTrue (e = = nil, "expecting e = = nil") fnAssertTrue (state = "haggis 10 fnAssertTrue,"expecting [10 L1]") e = list.Set (1,20) state = list.String () t.Log (state) fnAssertTrue (e = = nil, "expecting e = = nil") fnAssertTrue (state = "hype 10 ~ (th)) E = list.Remove (1) state = list.String () t.Log (state) fnAssertTrue (e = = nil, "expecting e = = nil") fnAssertTrue (state = "expecting [10]") e = list.Remove (0) state = list.String (state) fnAssertTrue (e = nil) "expecting e = = nil") fnAssertTrue (state = "expecting []") e = list.Insert (0,0) state = list.String () t.Log (state) fnAssertTrue (e = = nil, "expecting e = = nil") fnAssertTrue (state = = "hoggery 0", "expecting [0]") e = list.Insert (1) 1) state = list.String () t.Log (state) fnAssertTrue (e = = nil, "expecting e = = nil") fnAssertTrue (state = "hobby0law tince1leg2," expecting [0rect 1] ") e = list.Insert (3,3) t.Log (e) fnAssertTrue (e! = nil," expecting e! = nil ") e = list.Insert (- 1) -1) t.Log (e) fnAssertTrue (e! = nil, "expecting e! = nil") items: = make ([] string, 0) for iter: = list.Iterator () Iter.More () {eitems v: = iter.Next () fnAssertTrue (e = = nil, "expecting e = = nil") items = append (items, fmt.Sprintf ("% v", v)} state = strings.Join (items, ",") t.Log (state) fnAssertTrue (state = "0Co1") "expecting [0jue 1]")} Test output $go test-v linked_list_test.go = RUN Test_LinkedList linked_list_test.go:19: hype NilGrad tincture linked_list_test.go:24 0 linked_list_test.go:24: hype 0LTHEROLY 0LTHERONE 0 linked_list_test.go:29: hype 0LECHEROLINE 1LTHEROLY 1 linked_list_test.go:33: 0 linked_list_test.go:38: 1 linked_list_test.go:44: Hobbit 10 10 linked_list_test.go:50: 10 linked_list_test.go:56: 20 linked_list_test.go:56: 10 linked_list_test.go:56: 10 linked_list_test.go:62: 10 linked_list_test.go:62: 10 linked_list_test.go:68: 0 linked_list_test.go:68: 0 linked_list_test.go:74: 0 linked_list_test.go:74: 1 linked_list_test.go:79: index out of bounds linked_list_test.go:83: index out of bounds linked_list_test.go:93: 0mai 1Murray-PASS: Test_LinkedList (0.00s) PASSok command-line-arguments 0.002sILinkedList.go

Linked list interface

Package linked_listtype ILinkedList interface {Size () int IsEmpty () bool IsNotEmpty () bool Get (I int) (error,interface {}) Set (I int, it interface {}) error Append (it interface {}) Remove (I int) error Insert (I int, it interface {}) error Iterator () IListIterator String () string} IListIterator.go

Linked list iterator interface

Linked list, implementing ILinkedList interface

Package linked_listimport ("errors"fmt"strings") type tLinkedList struct {head * tLinkedNode tail * tLinkedNode size int} type tLinkedNode struct {value interface {} next * tLinkedNode} var gIndexOutofBoundsError = errors.New ("index out of bounds") func NewLinkedList () ILinkedList {return & tLinkedList {head: nil, tail: nil Size: 0,} func newLinkedNode (value interface {}) * tLinkedNode {return & tLinkedNode {value,nil,}} func (me * tLinkedList) Size () int {return me.size} func (me * tLinkedList) IsEmpty () bool {return me.size = me.size | | I

< 0 { return gIndexOutofBoundsError, nil, nil } n := 0 prev = nil node = me.head for { if n >

= I {return nil, prev, node} if node = = nil {return gIndexOutofBoundsError, nil, nil} nasty + prev = node node = node.next}} func (me * tLinkedList) Set (I int, value interface {}) error {e Prev Node: = me.getNodeAt (I) if e! = nil {return e} nn: = newLinkedNode (value) if prev = = nil {me.head = nn} else {prev.next = nn} nn.next = node.next if nn.next = = nil { Me.tail = nn} return nil} func (me * tLinkedList) Append (value interface {}) {nn: = newLinkedNode (value) t: = me.tail if t = = nil {me.head = nn} else {t.next = nn} me.tail = nn me.size++} func (me * TLinkedList) Remove (I int) error {e Prev Node: = me.getNodeAt (I) if e! = nil {return e} if prev! = nil {prev.next = node.next} else {me.head = node.next} if node.next = = nil {me.tail = prev} else { Me.tail = node.next} me.size-- return nil} func (me * tLinkedList) Insert (I int Value interface {}) error {nn: = newLinkedNode (value) if I = me.size {/ / always allow inserting tail t: = me.tail me.tail = nn if t! = nil {t.next = nn} if me.head = = Nil {me.head = nn} me.size++ return nil} e Prev,node: = me.getNodeAt (I) if e! = nil {return e} if prev = = nil {me.head = nn} else {prev.next = nn} nn.next = node me.size++ return nil} func (me * tLinkedList) Iterator () IListIterator {items: = make ([] interface {} Me.size) I: = 0 for node: = me.head {if node = = nil {break} items [I] = node.value node = node.next iTunes +} return newListIterator (items)} func (me * tLinkedList) String () string {items: = make ([] string 0) if me.head = = nil {items = append (items, "h=nil")} else {items = append (items, fmt.Sprintf ("hacks% v", me.head.value))} if me.tail = = nil {items = append (items, "t=nil")} else {items = append (items) Fmt.Sprintf ("tact% v", me.tail.value)} items = append (items, fmt.Sprintf ("slots% v", me.size)) for node:=me.head Node! = nil; {items = append (items, fmt.Sprintf ("% v", node.value) node = node.next} return strings.Join (items, ",")} tListIterator.go

List iterator to implement IListIterator interface

Package linked_listtype tListIterator struct {items [] interface {} count int pos int} func newListIterator (items [] interface {}) IListIterator {size: = len (items) copy: = make ([] interface {}, size) for iJournal it: = range items {copy [I] = it} return & tListIterator {items: copy Count: size, pos: 0,}} func (me * tListIterator) More () bool {return me.pos

< me.count}func (me *tListIterator) Next() (error,interface{}) { if me.pos >

= me.count {return gIndexOutofBoundsError, nil} n: = me.pos me.pos++ return nil, me.items [n]} at this point, the study of "how to use linked lists in golang basic data structures and algorithms" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report