Skip to content

Instantly share code, notes, and snippets.

@moraes
Last active September 28, 2025 09:56
Show Gist options
  • Save moraes/2141121 to your computer and use it in GitHub Desktop.
Save moraes/2141121 to your computer and use it in GitHub Desktop.

Revisions

  1. moraes revised this gist Jan 13, 2013. 1 changed file with 1 addition and 2 deletions.
    3 changes: 1 addition & 2 deletions gistfile1.go
    Original file line number Diff line number Diff line change
    @@ -34,9 +34,8 @@ func (s *Stack) Pop() *Node {
    if s.count == 0 {
    return nil
    }
    node := s.nodes[s.count-1]
    s.count--
    return node
    return s.nodes[s.count]
    }

    // NewQueue returns a new queue with the given initial size.
  2. moraes revised this gist Jan 13, 2013. 1 changed file with 6 additions and 15 deletions.
    21 changes: 6 additions & 15 deletions gistfile1.go
    Original 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 stack with the given initial size.
    func NewStack(size int) *Stack {
    return &Stack{
    nodes: make([]*Node, size),
    size: size,
    }
    // 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
    size int
    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)+s.size)
    copy(nodes, s.nodes)
    s.nodes = nodes
    }
    s.nodes[s.count] = n
    s.nodes = append(s.nodes[:s.count], n)
    s.count++
    }

    @@ -48,7 +39,7 @@ func (s *Stack) Pop() *Node {
    return node
    }

    // NewQueue returns a queue with the given initial size.
    // 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(1)
    s := NewStack()
    s.Push(&Node{1})
    s.Push(&Node{2})
    s.Push(&Node{3})
  3. moraes revised this gist Jan 13, 2013. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions gistfile1.go
    Original 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, 3),
    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, 3),
    nodes: make([]*Node, size),
    size: size,
    }
    }
  4. moraes revised this gist Jan 13, 2013. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions gistfile1.go
    Original 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)*2)
    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)*2)
    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
  5. moraes revised this gist Jan 13, 2013. 1 changed file with 30 additions and 8 deletions.
    38 changes: 30 additions & 8 deletions gistfile1.go
    Original 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 := &Stack{nodes: make([]*Node, 3)}
    s := NewStack(1)
    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)
    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())
    }
  6. moraes revised this gist Mar 21, 2012. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions gistfile1.go
    Original file line number Diff line number Diff line change
    @@ -8,7 +8,7 @@ type Node struct {
    Value int
    }

    // A basic LIFO stack.
    // 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
    }

    // A basic FIFO queue based on a circular list.
    // Queue is a basic FIFO queue based on a circular list that resizes as needed.
    type Queue struct {
    nodes []*Node
    head int
  7. moraes created this gist Mar 20, 2012.
    84 changes: 84 additions & 0 deletions gistfile1.go
    Original 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)
    }