Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implemented stack and queue in Go #909

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
138 changes: 138 additions & 0 deletions contents/stacks_and_queues/code/go/stack-queue.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
package main

import (
"fmt"
"os"
)

type stackNode struct {
value interface{}
prev *stackNode
}

type Stack struct {
top *stackNode
length int
}

// NewStack initializes a new stack.
func NewStack() *Stack {
return &Stack{top: nil, length: 0}
}

// Push adds an element to the stack and returns the length of the stack.
func (s *Stack) Push(v interface{}) int {
n := &stackNode{value: v, prev: s.top}
s.top = n
s.length++
return s.length
}

// Pop removes and returns the last item from the stack.
func (s *Stack) Pop() interface{} {
if s.length == 0 {
fmt.Fprint(os.Stderr, "cannot pop items of an empty stack")
os.Exit(1)
}
item := s.top
s.top = item.prev
s.length--
return item.value
}

// Size returns the size of the stack.
func (s Stack) Size() int {
return s.length
}

// Top returns the last item in the stack.
func (s Stack) Top() interface{} {
if s.length == 0 {
fmt.Fprint(os.Stderr, "cannot peek an empty stack")
os.Exit(1)
}
return s.top.value
}

type queueNode struct {
value interface{}
next *queueNode
}

type Queue struct {
front *queueNode
length int
}

// NewQueue initializes a new queue.
func NewQueue() *Queue {
return &Queue{front: nil, length: 0}
}

// Enqueue adds an item to the queue and returns the length of the queue.
func (q *Queue) Enqueue(v interface{}) int {
n := &queueNode{value: v, next: nil}
if q.front == nil {
q.front = n
} else {
last := q.front
for last.next != nil {
last = last.next
}
last.next = n
}
q.length++
return q.length
}

// Dequeue removes and returns an item from the queue.
func (q *Queue) Dequeue() interface{} {
if q.length == 0 {
fmt.Fprint(os.Stderr, "cannot dequeue an empty queue")
os.Exit(1)
}
item := q.front
if q.length == 1 {
q.front = nil
} else {
q.front = q.front.next
}
q.length--
return item.value
}

// Size returns the size of the queue.
func (q Queue) Size() int {
return q.length
}

// Front returns the first element in the queue.
func (q Queue) Front() interface{} {
if q.length == 0 {
fmt.Fprint(os.Stderr, "cannot peek an empty queue")
os.Exit(1)
}
return q.front.value
}

func main() {
stack := NewStack()

stack.Push(4)
stack.Push(5)
stack.Push(9)

fmt.Println(stack.Pop())
fmt.Println(stack.Size())
fmt.Println(stack.Top())

queue := NewQueue()

queue.Enqueue(4)
queue.Enqueue(5)
queue.Enqueue(9)

fmt.Println(queue.Dequeue())
fmt.Println(queue.Size())
fmt.Println(queue.Front())
}
4 changes: 4 additions & 0 deletions contents/stacks_and_queues/stacks_and_queues.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ Here is a simple implementation of a stack:
[import, lang:"typescript"](code/typescript/stack.ts)
{% sample lang="java" %}
[import, lang:"java"](code/java/Stack.java)
{% sample lang="go" %}
[import:8-55, lang:"go"](code/go/stack-queue.go)
{% endmethod %}

Here is a simple implementation of a queue:
Expand All @@ -28,6 +30,8 @@ Here is a simple implementation of a queue:
[import, lang:"typescript"](code/typescript/queue.ts)
{% sample lang="java" %}
[import, lang:"java" ](code/java/Queue.java)
{% sample lang="go" %}
[import:57-116, lang:"go"](code/go/stack-queue.go)
{% endmethod %}


Expand Down