From afe6c313cb1a7e4dc8c6becc96c193089b63c930 Mon Sep 17 00:00:00 2001 From: Henrik Christensen Date: Mon, 1 Nov 2021 12:35:06 +0100 Subject: [PATCH] implemented stack and queue in Go --- .../stacks_and_queues/code/go/stack-queue.go | 138 ++++++++++++++++++ .../stacks_and_queues/stacks_and_queues.md | 4 + 2 files changed, 142 insertions(+) create mode 100644 contents/stacks_and_queues/code/go/stack-queue.go diff --git a/contents/stacks_and_queues/code/go/stack-queue.go b/contents/stacks_and_queues/code/go/stack-queue.go new file mode 100644 index 000000000..260919e01 --- /dev/null +++ b/contents/stacks_and_queues/code/go/stack-queue.go @@ -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()) +} diff --git a/contents/stacks_and_queues/stacks_and_queues.md b/contents/stacks_and_queues/stacks_and_queues.md index 89a77be9a..57c16e4d2 100644 --- a/contents/stacks_and_queues/stacks_and_queues.md +++ b/contents/stacks_and_queues/stacks_and_queues.md @@ -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: @@ -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 %}