【golang】用container/list实现栈(Stack)

go语言中的container有heap、list、ring,没有stack。

其中heap是优先级队列,虽然有Push()/Pop()接口,但是使用heap要实现heap.Interface接口,不够简洁。

所以这里用list封装了一个简单的stack,留作他用。

 1 package stack
 2 
 3 import "container/list"
 4 
 5 type Stack struct {
 6     list *list.List
 7 }
 8 
 9 func NewStack() *Stack {
10     list := list.New()
11     return &Stack{list}
12 }
13 
14 func (stack *Stack) Push(value interface{}) {
15     stack.list.PushBack(value)
16 }
17 
18 func (stack *Stack) Pop() interface{} {
19     e := stack.list.Back()
20     if e != nil {
21         stack.list.Remove(e)
22         return e.Value
23     }
24     return nil
25 }
26 
27 func (stack *Stack) Peak() interface{} {
28     e := stack.list.Back()
29     if e != nil {
30         return e.Value
31     }
32 
33     return nil
34 }
35 
36 func (stack *Stack) Len() int {
37     return stack.list.Len()
38 }
39 
40 func (stack *Stack) Empty() bool {
41     return stack.list.Len() == 0
42 }

 

测试代码:

 1 package stack
 2 
 3 import "testing"
 4 
 5 func TestStack(t *testing.T) {
 6     stack := NewStack()
 7     stack.Push(1)
 8     stack.Push(2)
 9     stack.Push(3)
10     stack.Push(4)
11 
12     len := stack.Len()
13     if len != 4 {
14         t.Errorf("stack.Len() failed. Got %d, expected 4.", len)
15     }
16 
17     value := stack.Peak().(int)
18     if value != 4 {
19         t.Errorf("stack.Peak() failed. Got %d, expected 4.", value)
20     }
21 
22     value = stack.Pop().(int)
23     if value != 4 {
24         t.Errorf("stack.Pop() failed. Got %d, expected 4.", value)
25     }
26 
27     len = stack.Len()
28     if len != 3 {
29         t.Errorf("stack.Len() failed. Got %d, expected 3.", len)
30     }
31 
32     value = stack.Peak().(int)
33     if value != 3 {
34         t.Errorf("stack.Peak() failed. Got %d, expected 3.", value)
35     }
36 
37     value = stack.Pop().(int)
38     if value != 3 {
39         t.Errorf("stack.Pop() failed. Got %d, expected 3.", value)
40     }
41 
42     value = stack.Pop().(int)
43     if value != 2 {
44         t.Errorf("stack.Pop() failed. Got %d, expected 2.", value)
45     }
46 
47     empty := stack.Empty()
48     if empty {
49         t.Errorf("stack.Empty() failed. Got %d, expected false.", empty)
50     }
51 
52     value = stack.Pop().(int)
53     if value != 1 {
54         t.Errorf("stack.Pop() failed. Got %d, expected 1.", value)
55     }
56 
57     empty = stack.Empty()
58     if !empty {
59         t.Errorf("stack.Empty() failed. Got %d, expected true.", empty)
60     }
61 
62     nilValue := stack.Peak()
63     if nilValue != nil {
64         t.Errorf("stack.Peak() failed. Got %d, expected nil.", nilValue)
65     }
66 
67     nilValue = stack.Pop()
68     if nilValue != nil {
69         t.Errorf("stack.Pop() failed. Got %d, expected nil.", nilValue)
70     }
71 
72     len = stack.Len()
73     if len != 0 {
74         t.Errorf("stack.Len() failed. Got %d, expected 0.", len)
75     }
76 }

 下载stack.zip

posted @ 2014-02-09 22:38  davidli  阅读(6904)  评论(1编辑  收藏  举报