Last active
September 28, 2025 09:56
-
Star
(125)
You must be signed in to star a gist -
Fork
(29)
You must be signed in to fork a gist
-
-
Save moraes/2141121 to your computer and use it in GitHub Desktop.
Revisions
-
moraes revised this gist
Jan 13, 2013 . 1 changed file with 1 addition and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -34,9 +34,8 @@ func (s *Stack) Pop() *Node { if s.count == 0 { return nil } s.count-- return s.nodes[s.count] } // NewQueue returns a new queue with the given initial size. -
moraes revised this gist
Jan 13, 2013 . 1 changed file with 6 additions and 15 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -12,29 +12,20 @@ func (n *Node) String() string { return fmt.Sprint(n.Value) } // NewStack returns a new stack. func NewStack() *Stack { return &Stack{} } // Stack is a basic LIFO stack that resizes as needed. type Stack struct { nodes []*Node count int } // Push adds a node to the stack. func (s *Stack) Push(n *Node) { s.nodes = append(s.nodes[:s.count], n) s.count++ } @@ -48,7 +39,7 @@ func (s *Stack) Pop() *Node { return node } // NewQueue returns a new queue with the given initial size. func NewQueue(size int) *Queue { return &Queue{ nodes: make([]*Node, size), @@ -92,7 +83,7 @@ func (q *Queue) Pop() *Node { } func main() { s := NewStack() s.Push(&Node{1}) s.Push(&Node{2}) s.Push(&Node{3}) -
moraes revised this gist
Jan 13, 2013 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -15,7 +15,7 @@ func (n *Node) String() string { // NewStack returns a stack with the given initial size. func NewStack(size int) *Stack { return &Stack{ nodes: make([]*Node, size), size: size, } } @@ -51,7 +51,7 @@ func (s *Stack) Pop() *Node { // NewQueue returns a queue with the given initial size. func NewQueue(size int) *Queue { return &Queue{ nodes: make([]*Node, size), size: size, } } -
moraes revised this gist
Jan 13, 2013 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -30,7 +30,7 @@ type Stack struct { // Push adds a node to the stack. func (s *Stack) Push(n *Node) { if s.count >= len(s.nodes) { nodes := make([]*Node, len(s.nodes)+s.size) copy(nodes, s.nodes) s.nodes = nodes } @@ -68,7 +68,7 @@ type Queue struct { // Push adds a node to the queue. func (q *Queue) Push(n *Node) { if q.head == q.tail && q.count > 0 { nodes := make([]*Node, len(q.nodes)+q.size) copy(nodes, q.nodes[q.head:]) copy(nodes[len(q.nodes)-q.head:], q.nodes[:q.head]) q.head = 0 -
moraes revised this gist
Jan 13, 2013 . 1 changed file with 30 additions and 8 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -8,9 +8,22 @@ type Node struct { Value int } func (n *Node) String() string { return fmt.Sprint(n.Value) } // NewStack returns a stack with the given initial size. func NewStack(size int) *Stack { return &Stack{ nodes: make([]*Node, 3), size: size, } } // Stack is a basic LIFO stack that resizes as needed. type Stack struct { nodes []*Node size int count int } @@ -35,9 +48,18 @@ func (s *Stack) Pop() *Node { return node } // NewQueue returns a queue with the given initial size. func NewQueue(size int) *Queue { return &Queue{ nodes: make([]*Node, 3), size: size, } } // Queue is a basic FIFO queue based on a circular list that resizes as needed. type Queue struct { nodes []*Node size int head int tail int count int @@ -70,15 +92,15 @@ func (q *Queue) Pop() *Node { } func main() { s := NewStack(1) s.Push(&Node{1}) s.Push(&Node{2}) s.Push(&Node{3}) fmt.Println(s.Pop(), s.Pop(), s.Pop()) q := NewQueue(1) q.Push(&Node{4}) q.Push(&Node{5}) q.Push(&Node{6}) fmt.Println(q.Pop(), q.Pop(), q.Pop()) } -
moraes revised this gist
Mar 21, 2012 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -8,7 +8,7 @@ type Node struct { Value int } // Stack is a basic LIFO stack that resizes as needed. type Stack struct { nodes []*Node count int @@ -35,7 +35,7 @@ func (s *Stack) Pop() *Node { return node } // Queue is a basic FIFO queue based on a circular list that resizes as needed. type Queue struct { nodes []*Node head int -
moraes created this gist
Mar 20, 2012 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,84 @@ package main import ( "fmt" ) type Node struct { Value int } // A basic LIFO stack. type Stack struct { nodes []*Node count int } // Push adds a node to the stack. func (s *Stack) Push(n *Node) { if s.count >= len(s.nodes) { nodes := make([]*Node, len(s.nodes)*2) copy(nodes, s.nodes) s.nodes = nodes } s.nodes[s.count] = n s.count++ } // Pop removes and returns a node from the stack in last to first order. func (s *Stack) Pop() *Node { if s.count == 0 { return nil } node := s.nodes[s.count-1] s.count-- return node } // A basic FIFO queue based on a circular list. type Queue struct { nodes []*Node head int tail int count int } // Push adds a node to the queue. func (q *Queue) Push(n *Node) { if q.head == q.tail && q.count > 0 { nodes := make([]*Node, len(q.nodes)*2) copy(nodes, q.nodes[q.head:]) copy(nodes[len(q.nodes)-q.head:], q.nodes[:q.head]) q.head = 0 q.tail = len(q.nodes) q.nodes = nodes } q.nodes[q.tail] = n q.tail = (q.tail + 1) % len(q.nodes) q.count++ } // Pop removes and returns a node from the queue in first to last order. func (q *Queue) Pop() *Node { if q.count == 0 { return nil } node := q.nodes[q.head] q.head = (q.head + 1) % len(q.nodes) q.count-- return node } func main() { s := &Stack{nodes: make([]*Node, 3)} s.Push(&Node{1}) s.Push(&Node{2}) s.Push(&Node{3}) fmt.Printf("%v, %v, %v\n", s.Pop().Value, s.Pop().Value, s.Pop().Value) q := &Queue{nodes: make([]*Node, 3)} q.Push(&Node{1}) q.Push(&Node{2}) q.Push(&Node{3}) fmt.Printf("%v, %v, %v\n", q.Pop().Value, q.Pop().Value, q.Pop().Value) }