Last active
July 24, 2019 22:54
-
-
Save grevych/0ad1d6a27ef94b1197e5a70453bf9cde to your computer and use it in GitHub Desktop.
Stack and Queue of integers implemented in go. Link: https://goplay.space/#nUwwilNoGs0
This file contains 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 characters
package main | |
import ( | |
"errors" | |
"fmt" | |
) | |
type node struct { | |
value int | |
next *node | |
prev *node | |
} | |
type list struct { | |
head *node | |
tail *node | |
length int | |
} | |
type Stack interface { | |
Push(int) | |
Pop() (int, error) | |
Peek() (int, error) | |
Length() int | |
Print() | |
} | |
type Queue interface { | |
Enqueue(int) | |
Dequeue() (int, error) | |
Length() int | |
Print() | |
} | |
var ( | |
_ Stack = &list{} | |
_ Queue = &list{} | |
) | |
func newNode(value int) *node { | |
return &node{ | |
value: value, | |
next: nil, | |
prev: nil, | |
} | |
} | |
func NewStack() Stack { | |
return &list{} | |
} | |
func NewQueue() Queue { | |
return &list{} | |
} | |
func (l *list) Push(value int) { | |
node := newNode(value) | |
node.next = l.head | |
l.head = node | |
l.length += 1 | |
} | |
func (l *list) Pop() (int, error) { | |
if l.length == 0 { | |
return -1, errors.New("Empty stack") | |
} | |
node := l.head | |
l.head = l.head.next | |
l.length -= 1 | |
return node.value, nil | |
} | |
func (l *list) Peek() (int, error) { | |
if l.length == 0 { | |
return -1, errors.New("Empty stack") | |
} | |
return l.head.value, nil | |
} | |
func (l *list) Enqueue(value int) { | |
node := newNode(value) | |
if l.length == 0 { | |
l.head = node | |
l.tail = node | |
} else { | |
l.tail.next = node | |
node.prev = l.tail | |
l.tail = node | |
} | |
l.length += 1 | |
} | |
func (l *list) Dequeue() (int, error) { | |
if l.length == 0 { | |
return -1, errors.New("Empty queue") | |
} | |
node := l.head | |
if l.tail == l.head { | |
l.tail = nil | |
l.head = nil | |
} else { | |
l.head = l.head.next | |
l.head.prev = nil | |
} | |
l.length -= 1 | |
return node.value, nil | |
} | |
func (l *list) Length() int { | |
return l.length | |
} | |
func (l *list) Print() { | |
for current := l.head; current != nil; current = current.next { | |
fmt.Printf("%d ", current.value) | |
} | |
fmt.Println() | |
} | |
func main() { | |
var ( | |
err error | |
value int | |
) | |
stack := NewStack() | |
// Stack push | |
stack.Push(1) | |
stack.Push(2) | |
stack.Push(3) | |
stack.Print() | |
// Stack peak | |
value, err = stack.Peek() | |
fmt.Println(value, err) | |
// Stack pop | |
value, err = stack.Pop() | |
fmt.Println(value, err) | |
stack.Print() | |
value, err = stack.Pop() | |
fmt.Println(value, err) | |
stack.Print() | |
value, err = stack.Pop() | |
fmt.Println(value, err) | |
stack.Print() | |
value, err = stack.Pop() | |
fmt.Println(value, err) | |
fmt.Println("\n--------\n") | |
queue := NewQueue() | |
// Queue enqueue | |
queue.Enqueue(1) | |
queue.Enqueue(2) | |
queue.Enqueue(3) | |
queue.Print() | |
// Queue dequeue | |
value, err = queue.Dequeue() | |
fmt.Println(value, err) | |
queue.Print() | |
value, err = queue.Dequeue() | |
fmt.Println(value, err) | |
queue.Print() | |
value, err = queue.Dequeue() | |
fmt.Println(value, err) | |
queue.Print() | |
value, err = queue.Dequeue() | |
fmt.Println(value, err) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment