Tuesday, April 30, 2024
HomeGolangWant suggestion for which code is healthier - Code Overview

Want suggestion for which code is healthier – Code Overview


I need to create linked checklist code in Go however unsure which type ought to I exploit.

*** First Code Begin****

package deal most important

import "fmt"

sort Listing struct {
	head *Node
}

sort Node struct {
	subsequent    *Node
	factor int
}

func (checklist *Listing) append(factor int) {
	if checklist.head == nil {
		checklist.head = &Node{nil, factor}
	} else {
		node := checklist.head
		for node.subsequent != nil {
			node = node.subsequent
		}
		node.subsequent = &Node{nil, factor}
	}
}

func (checklist *Listing) appendFirst(factor int) {
	checklist.head = &Node{checklist.head, factor}
}

func (checklist *Listing) Print() {
	node := checklist.head
	for node != nil {
		fmt.Print(node.factor, " ")
		node = node.subsequent
	}
	fmt.Println("")
}

func most important() {
	ll := Listing{}
	for i := 0; i < 10; i++ {
		ll.appendFirst(i)
		// ll.append(i)
	}
	ll.Print()
}

*** First Code Cease****

*** Second Code Begin****

package deal most important

import "fmt"

sort Listing *Node

sort Node struct {
	subsequent    Listing
	factor int
}

func append(checklist *Listing, factor int) {
	if *checklist == nil {
		*checklist = &Node{nil, factor}
	} else {
		append(&((*checklist).subsequent), factor)
	}
}

func appendFirst(checklist *Listing, factor int) {
	*checklist = &Node{*checklist, factor}
}

func Print(checklist Listing) {
	node := checklist
	for node != nil {
		fmt.Print(node.factor, " ")
		node = node.subsequent
	}
	fmt.Println("")
}

func most important() {
	var checklist Listing = nil
	for i := 0; i < 10; i++ {
		appendFirst(&checklist, i)
		//append(&checklist, i)
	}
	Print(checklist)
}

*** Second Code Cease****

Please let me know which code type needs to be used.

strategies and features are each legitimate; a way is only a perform with a particular receiver argument. Virtually it doesn’t make a lot of a distinction. A rule of thumb that some individuals observe, is to make use of strategies when managing state, because it flows naturally. Be aware it’s also possible to write pointer receivers to change the worth that the receiver is pointing at.

Thanks for the reply.

However this reply doesn’t resolve my drawback. Take into account If you’re a Go developer how you’ll write a linked checklist program. Is there another a lot better program.

If I used to be a go developer, I’d use the linked checklist from the stdlib.

2 Likes

Ahh… we don’t write Linked Listing with Go. In reality, we don’t use it in any respect. It’s very very uncommon we encounter a use case for it.

We obtained rubbish collector, hashmap (map) and slice (don’t confuse with array). Linked Listing solely is smart in languages with out them like C.

In reality, you already put us in a tough scenario for writing a code with out correct use case, a profession suicide to anybody of us. :rofl:

It’s name prepend

She’s proper. The individual to find out is your reviewer, interviewer, boss, and buyer. Each are effective.

Watch out together with your naming conference. In Go, start with Capital letter means exported perform. Please undergo Efficient Go – The Go Programming Language

I desire double pointer implementation. Refer: Double Pointers and Linked Listing in C | Dev Notes

HOWEVER, remember that no matter you’re doing (each samples) in correct Go is:

func most important() {
    var checklist []int

	// append
	for i := 0; i < 10; i++ {
		checklist = append(checklist, i)
	}

	// prepend
	for i := 0; i < 10; i++ {
		checklist = append([]int{i}, checklist...)
	}
}

Fallacious language bro.

Thanks for the reply.

You might be proper we will use built-in information constructions straight and completed. However some professor is instructing linked checklist utilizing go. I had given OOPs based mostly reply and he’s not pleased with my answer. So I suppose, I ought to go together with double pointer based mostly procedural answer.

Naming conference i’ll repair. However the query is what needs to be the method to put in writing code procedural or oops based mostly.

Thanks for the reply.

You might be proper we will use built-in information constructions straight and completed. However some professor is instructing linked checklist utilizing go. I had given OOPs based mostly reply and he’s not pleased with my answer. So I suppose, I ought to go together with double pointer based mostly procedural answer.

Are you completely positive your lecturer actually anticipating a LL in Go or one thing outdoors the field (it’s fairly apparent)?

I had 1 lecturer as soon as secretly anticipated me to elevate a 5+3kg weights with only a bag of uncooked spaghetti in a structural integrity project earlier than whereas my friends had been utilizing different supplies like metallic wires. The hidden expectation was the enterprise acumen of issues like costing and product life-cycle planning (the constructing solely must have 10 minutes lifetime) on high of engineering technicalities.


Website-note:

I’ll give my deepest sigh in case your lecturer is actually anticipating a LL from Go. Deal with it like a simulation in life. Generally, it throws you lemon. Attempt make it a lemonade. Good luck. NOTE: the double pointer is completely doable in Go.

Ahhh… Go will not be OOP. It doesn’t polymorph or inherit an information sort. Technique 1 seems like OOP nevertheless it’s not.

The strategies are duck-typing interfaces. You’ll be able to’t change any already outlined technique and you may’t outline a brand new information sort to based mostly on an present definition together with all its strategies with out rewriting totally – solely composition.

Perhaps he/she was dissatisfied due to this?

Additionally, this perform in pattern 2 is conflicting with the built-in perform append. Did you take a look at your program?

Might this be inflicting annoyance to your lecturer?

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments