blob: 3042a671bfbfd3d2bbb0bb2d93d21ceee354b0c6 [file] [log] [blame]
Russ Coxd2cc9882012-02-16 23:50:37 -05001// run
Rob Pikeab34d152008-06-06 14:27:34 -07002
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
Rob Pikeeb37b5b2012-02-24 16:24:24 +11007// Test general operation using a list implementation.
8
Rob Pikeab34d152008-06-06 14:27:34 -07009package main
10
Robert Griesemer581530e2009-12-10 12:53:23 -080011type Item interface {
Ian Lance Taylorf2030932012-01-18 14:31:31 -080012 Print() string
Rob Pikeab34d152008-06-06 14:27:34 -070013}
14
Robert Griesemer581530e2009-12-10 12:53:23 -080015type ListItem struct {
Ian Lance Taylor6b346282012-01-18 13:20:55 -080016 item Item
17 next *ListItem
Rob Pikeab34d152008-06-06 14:27:34 -070018}
19
Robert Griesemer581530e2009-12-10 12:53:23 -080020type List struct {
Ian Lance Taylor6b346282012-01-18 13:20:55 -080021 head *ListItem
Rob Pikeab34d152008-06-06 14:27:34 -070022}
23
Robert Griesemer581530e2009-12-10 12:53:23 -080024func (list *List) Init() {
Ian Lance Taylor6b346282012-01-18 13:20:55 -080025 list.head = nil
Rob Pikeab34d152008-06-06 14:27:34 -070026}
27
Robert Griesemer581530e2009-12-10 12:53:23 -080028func (list *List) Insert(i Item) {
Ian Lance Taylor6b346282012-01-18 13:20:55 -080029 item := new(ListItem)
30 item.item = i
31 item.next = list.head
32 list.head = item
Rob Pikeab34d152008-06-06 14:27:34 -070033}
34
Ian Lance Taylorf2030932012-01-18 14:31:31 -080035func (list *List) Print() string {
36 r := ""
Ian Lance Taylor6b346282012-01-18 13:20:55 -080037 i := list.head
Rob Pikeab34d152008-06-06 14:27:34 -070038 for i != nil {
Ian Lance Taylorf2030932012-01-18 14:31:31 -080039 r += i.item.Print()
Ian Lance Taylor6b346282012-01-18 13:20:55 -080040 i = i.next
Rob Pikeab34d152008-06-06 14:27:34 -070041 }
Ian Lance Taylorf2030932012-01-18 14:31:31 -080042 return r
Rob Pikeab34d152008-06-06 14:27:34 -070043}
44
45// Something to put in a list
Robert Griesemer581530e2009-12-10 12:53:23 -080046type Integer struct {
Ian Lance Taylor6b346282012-01-18 13:20:55 -080047 val int
Rob Pikeab34d152008-06-06 14:27:34 -070048}
49
Robert Griesemer581530e2009-12-10 12:53:23 -080050func (this *Integer) Init(i int) *Integer {
Ian Lance Taylor6b346282012-01-18 13:20:55 -080051 this.val = i
52 return this
Rob Pikeab34d152008-06-06 14:27:34 -070053}
54
Ian Lance Taylorf2030932012-01-18 14:31:31 -080055func (this *Integer) Print() string {
56 return string(this.val + '0')
Rob Pikeab34d152008-06-06 14:27:34 -070057}
58
Ian Lance Taylor6b346282012-01-18 13:20:55 -080059func main() {
60 list := new(List)
61 list.Init()
Rob Pikeab34d152008-06-06 14:27:34 -070062 for i := 0; i < 10; i = i + 1 {
Ian Lance Taylor6b346282012-01-18 13:20:55 -080063 integer := new(Integer)
64 integer.Init(i)
65 list.Insert(integer)
Rob Pikeab34d152008-06-06 14:27:34 -070066 }
67
Ian Lance Taylorf2030932012-01-18 14:31:31 -080068 r := list.Print()
69 if r != "9876543210" {
70 panic(r)
71 }
Rob Pikeab34d152008-06-06 14:27:34 -070072}