Task: Sorting algorithms_Tree sort on a linked list

C: #include <stdio.h>
#include <stdlib.h>
#include <time.h>

void fatal(const char* message) {
    fprintf(stderr, "%s\n", message);
    exit(1);
}

void* xmalloc(size_t n) {
    void* ptr = malloc(n);
    if (ptr == NULL)
        fatal("Out of memory");
    return ptr;
}

typedef struct node_tag {
    int item;
    struct node_tag* prev;
    struct node_tag* next;
} node_t;

void list_initialize(node_t* list) {
    list->prev = list;
    list->next = list;
}

void list_destroy(node_t* list) {
    node_t* n = list->next;
    while (n != list) {
        node_t* tmp = n->next;
        free(n);
        n = tmp;
    }
}

void list_append_node(node_t* list, node_t* node) {
    node_t* prev = list->prev;
    prev->next = node;
    list->prev = node;
    node->prev = prev;
    node->next = list;
}

void list_append_item(node_t* list, int item) {
    node_t* node = xmalloc(sizeof(node_t));
    node->item = item;
    list_append_node(list, node);
}

void list_print(node_t* list) {
    printf("[");
    node_t* n = list->next;
    if (n != list) {
        printf("%d", n->item);
        n = n->next;
    }
    for (; n != list; n = n->next)
        printf(", %d", n->item);
    printf("]\n");
}

void tree_insert(node_t** p, node_t* n) {
    while (*p != NULL) {
        if (n->item < (*p)->item)
            p = &(*p)->prev;
        else
            p = &(*p)->next;
    }
    *p = n;
}

void tree_to_list(node_t* list, node_t* node) {
    if (node == NULL)
        return;
    node_t* prev = node->prev;
    node_t* next = node->next;
    tree_to_list(list, prev);
    list_append_node(list, node);
    tree_to_list(list, next);
}

void tree_sort(node_t* list) {
    node_t* n = list->next;
    if (n == list)
        return;
    node_t* root = NULL;
    while (n != list) {
        node_t* next = n->next;
        n->next = n->prev = NULL;
        tree_insert(&root, n);
        n = next;
    }
    list_initialize(list);
    tree_to_list(list, root);
}

int main() {
    srand(time(0));
    node_t list;
    list_initialize(&list);
    for (int i = 0; i < 16; ++i)
        list_append_item(&list, rand() % 100);
    printf("before sort: ");
    list_print(&list);
    tree_sort(&list);
    printf(" after sort: ");
    list_print(&list);
    list_destroy(&list);
    return 0;
}




Go: package main

import (
    "container/list"
    "fmt"
)

type BinaryTree struct {
    node         int
    leftSubTree  *BinaryTree
    rightSubTree *BinaryTree
}

func (bt *BinaryTree) insert(item int) {
    if bt.node == 0 {
        bt.node = item
        bt.leftSubTree = &BinaryTree{}
        bt.rightSubTree = &BinaryTree{}
    } else if item < bt.node {
        bt.leftSubTree.insert(item)
    } else {
        bt.rightSubTree.insert(item)
    }
}

func (bt *BinaryTree) inOrder(ll *list.List) {
    if bt.node == 0 {
        return
    }
    bt.leftSubTree.inOrder(ll)
    ll.PushBack(bt.node)
    bt.rightSubTree.inOrder(ll)
}
func treeSort(ll *list.List) *list.List {
    searchTree := &BinaryTree{}
    for e := ll.Front(); e != nil; e = e.Next() {
        i := e.Value.(int)
        searchTree.insert(i)
    }
    ll2 := list.New()
    searchTree.inOrder(ll2)
    return ll2
}

func printLinkedList(ll *list.List, f string, sorted bool) {
    for e := ll.Front(); e != nil; e = e.Next() {
        i := e.Value.(int)
        fmt.Printf(f+" ", i)
    }
    if !sorted {
        fmt.Print("-> ")
    } else {
        fmt.Println()
    }
}

func main() {
    sl := []int{5, 3, 7, 9, 1}
    ll := list.New()
    for _, i := range sl {
        ll.PushBack(i)
    }
    printLinkedList(ll, "%d", false)
    lls := treeSort(ll)
    printLinkedList(lls, "%d", true)

    sl2 := []int{'d', 'c', 'e', 'b', 'a'}
    ll2 := list.New()
    for _, c := range sl2 {
        ll2.PushBack(c)
    }
    printLinkedList(ll2, "%c", false)
    lls2 := treeSort(ll2)
    printLinkedList(lls2, "%c", true)
}