You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

1.2 KiB

listpushback

Instructions

Write a function ListSort that sorts the linked list by ascending order.

  • This time you only will have the node structure.

  • Try to use recursive.

  • Use pointers when ever you can.

Expected function and structure

type node struct {
	data int
	next *node
}

func ListSort(l *node) *node {

}

Usage

Here is a possible program to test your function :

package main

import (
	"fmt"
	piscine ".."
)

//Prints the list
func PrintList(l *node) {
	m := l
	for m != nil {
		fmt.Print(m.data, " -> ")
		m = m.next
	}

	fmt.Print(nil)
	fmt.Println()
}

//insert elements
func listPushBack(l *node, data int) {

	n := &node{}
	n.data = data
	n.next = nil

	if l == nil {
		l = n
		return
	}

	iterator := l
	for iterator.next != nil {
		iterator = iterator.next
	}
	iterator.next = n
}

func main() {
	link := &node{}

	listPushBack(link, 5)
	listPushBack(link, 4)
	listPushBack(link, 3)
	listPushBack(link, 2)
	listPushBack(link, 1)

	PrintList(piscine.ListSort(link))

}

And its output :

student@ubuntu:~/piscine/test$ go build
student@ubuntu:~/piscine/test$ ./test
0 -> 1 -> 2 -> 3 -> 4 -> 5 -> <nil>
student@ubuntu:~/piscine/test$