~john-koepi/ubuntu/trusty/golang/default

« back to all changes in this revision

Viewing changes to test/ken/rob1.go

  • Committer: Bazaar Package Importer
  • Author(s): Ondřej Surý
  • Date: 2011-04-20 17:36:48 UTC
  • Revision ID: james.westby@ubuntu.com-20110420173648-ifergoxyrm832trd
Tags: upstream-2011.03.07.1
Import upstream version 2011.03.07.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// $G $D/$F.go && $L $F.$A && ./$A.out
 
2
 
 
3
// Copyright 2009 The Go Authors. All rights reserved.
 
4
// Use of this source code is governed by a BSD-style
 
5
// license that can be found in the LICENSE file.
 
6
 
 
7
package main
 
8
 
 
9
type Item interface {
 
10
        Print();
 
11
}
 
12
 
 
13
type ListItem struct {
 
14
        item    Item;
 
15
        next    *ListItem;
 
16
}
 
17
 
 
18
type List struct {
 
19
        head    *ListItem;
 
20
}
 
21
 
 
22
func (list *List) Init() {
 
23
        list.head = nil;
 
24
}
 
25
 
 
26
func (list *List) Insert(i Item) {
 
27
        item := new(ListItem);
 
28
        item.item = i;
 
29
        item.next = list.head;
 
30
        list.head = item;
 
31
}
 
32
 
 
33
func (list *List) Print() {
 
34
        i := list.head;
 
35
        for i != nil {
 
36
                i.item.Print();
 
37
                i = i.next;
 
38
        }
 
39
}
 
40
 
 
41
// Something to put in a list
 
42
type Integer struct {
 
43
        val             int;
 
44
}
 
45
 
 
46
func (this *Integer) Init(i int) *Integer {
 
47
        this.val = i;
 
48
        return this;
 
49
}
 
50
 
 
51
func (this *Integer) Print() {
 
52
        print(this.val);
 
53
}
 
54
 
 
55
func
 
56
main() {
 
57
        list := new(List);
 
58
        list.Init();
 
59
        for i := 0; i < 10; i = i + 1 {
 
60
                integer := new(Integer);
 
61
                integer.Init(i);
 
62
                list.Insert(integer);
 
63
        }
 
64
 
 
65
        list.Print();
 
66
        print("\n");
 
67
}