golang实现一个链表 [英] golang implementing a linked list

查看:337
本文介绍了golang实现一个链表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在golang中实现一个排序的链表。而且我很难想出一种通用的方法来使链表与任何可以与其自身进行比较的类型一起工作。由于它是一个排序列表,我希望'go编译器'能够确保插入链表的值可以进行比较。



例如,

  importlinkedlist

类型Person结构{
名称字符串
}

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

在上面的例子中,我如何让编译器确保'Person'的值为'Person' '可以与另一个也有类型'Person'的值相比较。我希望编译器在插入值不合适的情况下捕捉错误。



我可以这样做,

  importlinkedlist

类型元素接口{
func IsGreater(v元素{})bool
}

类型Person结构{
名称字符串
年龄int
}

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

然后,在链接列表的insert函数中,我可以使用 IsGreater 函数来决定将元素放置在链表中的位置。



我的问题是...


  1. 有没有更好的方法可以做这个?比上述解决方案好得多。

我已经经历了sort.Sort并看到它如何完成包。它完成的方式是为该类型的片段创建一个新类型,然后通过实现Len,Less和Swap使该新类型实现排序接口。

我也可以在这里做同样的事情。但是不得不创建一个新的切片类型,然后实现一些函数来满足一个接口,当我一次只处理同一类型的两个值时......对我来说似乎有点矫枉过正。

$因为Golang不支持泛型,所以所有容器都应该使用interface {}并键入assert,我认为没有更好的解决方案满足您的需求。

/ p>

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.

解决方案

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

这篇关于golang实现一个链表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆