dsm1998
2018-01-16 22:06
浏览 562
已采纳

golang比较实现不同的两个结构

I am new to go language, under learning. I have years OOP experience in C++. There is a stacker interface that is written in go and two implementations of it, one is slice base stack and another one is linkedlist base.

I find it is hard to compare two different structures and tell if they are containing the same data or not. The simple example code list below (notice a lot of functions/implementations are not listed because they are not relative with this question). The key function is stackEquals, I have tried different ways to approach it but they failed. Please see the comments in the code.

package main

import (
    "fmt"
    "errors"
)

// The interface is fixed, cannot be modified
type Stacker interface {
    isEmpty() bool
    size() int
    push(x int)
    peek() (int, error)
    pop() (int, error)
    copy() Stacker
} 

type StackSlice struct {
    slice []int
}

type StackLinked struct {
    next *StackLinked
    value int
    // possible with other variables that is not relative
}

// There are interface function/method implementations did not paste

func (s StackSlice) String() string {
    // return all the value inside the stack as string
    // like [5 4]
}

func (s StackLinked) String() string {
    // return all the value inside the stack as string
    // like [5 4]]
}

// Pre-condition:
//    none
// Post-condition:
//    returns true if s and t have the same elements in the same order;
//    both s and t have the same value after calling stackEquals as before
// Annoying constraint:
//    Use only Stackers in the body of this functions: don't use arrays,
//    slices, or any container other than a Stacker.
func stackEquals(s, t Stacker) bool {
    // This implementation below always return false unless they are the same thing
    return s == t 

    // I tried return s.String() == t.String() but gave an error said interface doesn't have String() method.
}

How can I compare two stacks that implemented in different way and tell if they are the same (same means same values in same order) in the stack.

图片转代码服务由CSDN问答提供 功能建议

我是初学者,正在学习中。 我有多年C ++ OOP经验。 有一个用go编写的堆栈器接口,它有两个实现,一个是切片基堆栈,另一个是链表基。

我发现很难比较两种不同的结构, 告诉他们是否包含相同的数据。 下面是简单的示例代码列表(注意,未列出许多功能/实现,因为它们与该问题无关)。 关键功能是 stackEquals ,我尝试了不同的方法来实现它,但是失败了。 请参阅代码中的注释。

 包main 
 
import(
“ fmt” 
“ errors” 
)
 
 // 是固定的,无法修改
type Stacker接口{
 isEmpty()bool 
 size()int 
 push(x int)
 peek()(int,error)
 pop()(int,error)  
 copy()Stacker 
} 
 
type StackSlice结构{
 slice [] int 
} 
 
type StackLinked结构{
 next * StackLinked 
 value int 
 //可以与其他变量一起使用 不是相对的
} 
 
 //某些接口函数/方法实现未粘贴
 
func(s StackSlice)String()string {
 //将堆栈内的所有值作为字符串返回
  //像[5 4] 
} 
 
func(s StackLinked)String()string {
 //返回堆栈中的所有值作为字符串
 //像[5 4]] 
} \  n 
 //前置条件:
 //无
 //后置条件:
 //如果s和t具有相同顺序的相同元素,则返回true; 
 // s和t都具有 调用stackE之后的值与之前相同\  n //烦人的约束:
 //在此函数的主体中仅使用堆栈器:请勿使用数组,
 //切片或堆栈器以外的任何容器。
func stackEquals(s,t Stacker)bool  {
 //下面的实现总是返回false,除非它们是相同的
 return s == t 
 
 //我尝试返回s.String()== t.String(),但给出了错误提示 接口没有String()方法。
} 
   
 
 

如何比较以不同方式实现的两个堆栈,并判断它们是否相同(相同 表示相同的值(顺序相同)。

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

  • doutui7955 2018-01-17 01:21
    最佳回答

    If you need to compare two interfaces, you can only use the methods in that interface, so in this case, String does not exist in the interface (even though both of your implementations have it, the interface itself does not).

    A possible implementation would be:

    func stackEquals(s, t Stacker) bool {
        // if they are the same object, return true
        if s == t {
            return true
        }
        // if they have different sizes or the next element is not the same,
        // then they are different
        if s.size() != t.size() || s.peek() != t.peek() {
            return false
        }
    
        // they could be the same, so let's copy them so that we don't mess up
        // the originals
        ss = s.copy()
        tt = t.copy()
    
        // iterate through the values and check if each one is
        // the same.  If not, return false
        for ; i, err := ss.pop(); err == nil {
            if j, err := tt.pop(); err != nil || i != j {
                return false
            }
        }
    
        return true
    }
    

    This assumes that the only error pop would get is when there are no more values, otherwise you will need to do some better error checking and use isEmpty.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题