File tree Expand file tree Collapse file tree 1 file changed +6
-8
lines changed
Expand file tree Collapse file tree 1 file changed +6
-8
lines changed Original file line number Diff line number Diff line change @@ -2,6 +2,7 @@ package stack
22
33type LinkedStack [T any ] struct {
44 top * Node [T ]
5+ ln int
56}
67
78var _ Stack [any ] = & LinkedStack [any ]{}
@@ -23,6 +24,7 @@ func (x *LinkedStack[T]) Push(values ...T) error {
2324 node .next = x .top
2425 x .top = node
2526 }
27+ x .ln ++
2628 }
2729 return nil
2830}
@@ -38,9 +40,10 @@ func (x *LinkedStack[T]) PopE() (T, error) {
3840 }
3941 top := x .top
4042 x .top = x .top .next
43+ value := top .value
4144 top .next = nil
4245 top .value = nil
43- value := top . value
46+ x . ln --
4447 return value , nil
4548}
4649
@@ -65,17 +68,12 @@ func (x *LinkedStack[T]) IsNotEmpty() bool {
6568}
6669
6770func (x * LinkedStack [T ]) Len () int {
68- count := 0
69- node := x .top
70- for node != nil {
71- count ++
72- node = node .next
73- }
74- return count
71+ return x .ln
7572}
7673
7774func (x * LinkedStack [T ]) Clear () error {
7875 x .top = nil
76+ x .ln = 0
7977 return nil
8078}
8179
You can’t perform that action at this time.
0 commit comments