2008-06-06 15:27:34 -06:00
|
|
|
// $G $D/$F.go && $L $F.$A && ./$A.out
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
2009-12-10 13:53:23 -07:00
|
|
|
type Item interface {
|
2008-08-07 14:27:58 -06:00
|
|
|
Print();
|
2008-06-06 15:27:34 -06:00
|
|
|
}
|
|
|
|
|
2009-12-10 13:53:23 -07:00
|
|
|
type ListItem struct {
|
2008-06-06 15:27:34 -06:00
|
|
|
item Item;
|
|
|
|
next *ListItem;
|
|
|
|
}
|
|
|
|
|
2009-12-10 13:53:23 -07:00
|
|
|
type List struct {
|
2008-06-06 15:27:34 -06:00
|
|
|
head *ListItem;
|
|
|
|
}
|
|
|
|
|
2009-12-10 13:53:23 -07:00
|
|
|
func (list *List) Init() {
|
2008-06-06 15:27:34 -06:00
|
|
|
list.head = nil;
|
|
|
|
}
|
|
|
|
|
2009-12-10 13:53:23 -07:00
|
|
|
func (list *List) Insert(i Item) {
|
2009-01-06 16:19:02 -07:00
|
|
|
item := new(ListItem);
|
2008-06-06 15:27:34 -06:00
|
|
|
item.item = i;
|
|
|
|
item.next = list.head;
|
|
|
|
list.head = item;
|
|
|
|
}
|
|
|
|
|
2009-12-10 13:53:23 -07:00
|
|
|
func (list *List) Print() {
|
2008-06-06 15:27:34 -06:00
|
|
|
i := list.head;
|
|
|
|
for i != nil {
|
2008-08-07 14:27:58 -06:00
|
|
|
i.item.Print();
|
2008-06-06 15:27:34 -06:00
|
|
|
i = i.next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Something to put in a list
|
2009-12-10 13:53:23 -07:00
|
|
|
type Integer struct {
|
2008-06-06 15:27:34 -06:00
|
|
|
val int;
|
|
|
|
}
|
|
|
|
|
2009-12-10 13:53:23 -07:00
|
|
|
func (this *Integer) Init(i int) *Integer {
|
2008-06-06 15:27:34 -06:00
|
|
|
this.val = i;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2009-12-10 13:53:23 -07:00
|
|
|
func (this *Integer) Print() {
|
2008-08-11 23:07:49 -06:00
|
|
|
print(this.val);
|
2008-06-06 15:27:34 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func
|
2009-12-10 13:53:23 -07:00
|
|
|
main() {
|
2009-01-06 16:19:02 -07:00
|
|
|
list := new(List);
|
2008-06-06 15:27:34 -06:00
|
|
|
list.Init();
|
|
|
|
for i := 0; i < 10; i = i + 1 {
|
2009-01-06 16:19:02 -07:00
|
|
|
integer := new(Integer);
|
2008-08-07 14:27:58 -06:00
|
|
|
integer.Init(i);
|
|
|
|
list.Insert(integer);
|
2008-06-06 15:27:34 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
list.Print();
|
2008-08-11 23:07:49 -06:00
|
|
|
print("\n");
|
2008-06-06 15:27:34 -06:00
|
|
|
}
|