Golang实现一个链表

I'm trying to implement a sorted linked list in golang. And I'm having a hard time coming up with a generic way to make the linked list work with any type that can by compared with itself. Since its a sorted list, I want the 'go compiler' to ensure that the values being inserted into the linked list can be compared.

For example,

import "linkedlist"

type Person struct {
  name string
}

func main() {
  l := linkedlist.New()
  p := Person{"Jay"}
  l.insert(p)
}

In the above example, how do I make the compiler ensure that the value 'p' which has type 'Person' can be compared with another value which also has type 'Person'. I want the compiler to catch the error in those situations where the value being inserted is not an appropriate value.

I can do something like this,

import "linkedlist"

type Element interface {
  func IsGreater(v Element{}) bool
}

type Person struct {
  name string
  age int
}

func (p *Person) IsGreater(p1 interface{}) bool {
  if ok, v := p1.(Person); ok && p.age > v.age {
    return true
  }
  return false
}

And then, within the "insert" function of the linked list I can use the IsGreater function to decide where to place the Element in the linked list.

My question is...

  1. Is there a better way to do this? Something that is a lot better than the solution above.

I've already gone through sort.Sort and seen how its done in that package. The way its done there is to create a new type for a slice of the type and then make that new type fulfill the sort interface by implementing Len, Less and Swap.

I can probably do the same thing here in my case as well. But having to create a new slice type and then implement a few functions to satisfy an interface, when I'll only be dealing with 2 values of the same type at a time.. seems a bit overkill to me.

duanen19871021
duanen19871021 你不可以做这个。至少不是以一种友好,简单和适当的方式。Go没有泛型。如果您的解决方案的这种混合类型的安全性足够好:请使用它。对于程序员来说,实现少量功能应该是可以的。
6 年多之前 回复

2个回答



由于Golang不支持泛型,因此所有容器都应使用interface {}并键入assert,我认为没有更好的解决方案可以满足您的要求 </ p>
</ div>

展开原文

原文

Because Golang do not support generics, So all of containers should use interface{} and type assert, I think no better solution for your requirement.



与此相关的库函数已经存在:</ p>

”> http://golang.org/pkg/container/list/ </ p>

http://golang.org/pkg/container/ring/ </ p>

< p>您可以将列表与 reflect.DeepEqual </ code>进行比较。</ p>

如果要实现使用类型检查的链接列表,请为列表创建嵌入式结构 type MyLinkedList struct {* list.List} </ code>和一个用于列表中项目的 type Element struct {* List.Element} </ code>。 然后,您可以实现 list.List </ code>的所有方法,并根据需要使用类型检查进行覆盖。 </ p>
</ div>

展开原文

原文

Library functions for this already exist:

http://golang.org/pkg/container/list/

http://golang.org/pkg/container/ring/

You can compare the lists with reflect.DeepEqual.

If you want to implement a linked list that uses type checking, make an embedded struct for the list type MyLinkedList struct { *list.List} and one for the items in the list type Element struct{ *List.Element }. You can then implement all the methods of list.List and over-ride as necessary with your type checks.

douxuzui4590
douxuzui4590 这不能回答问题-在容器/列表中,值的类型为interface {},这是不可比较的。
4 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐