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
|
|
|
|
|
|
|
|
type Item interface
|
|
|
|
{
|
2008-08-07 14:27:58 -06:00
|
|
|
Print();
|
2008-06-06 15:27:34 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
func (list *List)
|
|
|
|
Print()
|
|
|
|
{
|
|
|
|
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
|
|
|
|
type Integer struct
|
|
|
|
{
|
|
|
|
val int;
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *Integer)
|
2008-08-07 14:27:58 -06:00
|
|
|
Init(i int) *Integer
|
2008-06-06 15:27:34 -06:00
|
|
|
{
|
|
|
|
this.val = i;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *Integer)
|
2008-08-07 14:27:58 -06:00
|
|
|
Print()
|
2008-06-06 15:27:34 -06:00
|
|
|
{
|
2008-08-11 23:07:49 -06:00
|
|
|
print(this.val);
|
2008-06-06 15:27:34 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func
|
2008-06-27 15:15:06 -06:00
|
|
|
main()
|
2008-06-06 15:27:34 -06:00
|
|
|
{
|
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
|
|
|
}
|