summaryrefslogtreecommitdiffstats
path: root/test/ken/rob1.go
blob: 3042a671bfbfd3d2bbb0bb2d93d21ceee354b0c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// run

// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// Test general operation using a list implementation.

package main

type Item interface {
	Print() string
}

type ListItem struct {
	item Item
	next *ListItem
}

type List struct {
	head *ListItem
}

func (list *List) Init() {
	list.head = nil
}

func (list *List) Insert(i Item) {
	item := new(ListItem)
	item.item = i
	item.next = list.head
	list.head = item
}

func (list *List) Print() string {
	r := ""
	i := list.head
	for i != nil {
		r += i.item.Print()
		i = i.next
	}
	return r
}

// Something to put in a list
type Integer struct {
	val int
}

func (this *Integer) Init(i int) *Integer {
	this.val = i
	return this
}

func (this *Integer) Print() string {
	return string(this.val + '0')
}

func main() {
	list := new(List)
	list.Init()
	for i := 0; i < 10; i = i + 1 {
		integer := new(Integer)
		integer.Init(i)
		list.Insert(integer)
	}

	r := list.Print()
	if r != "9876543210" {
		panic(r)
	}
}