Skip to content

Commit 4fa1257

Browse files
committed
Postfix calculator
1 parent e66c053 commit 4fa1257

File tree

3 files changed

+160
-2
lines changed

3 files changed

+160
-2
lines changed

3_double_linkedlists.go

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
// Implementing a DoubleLinkedList data structure in go
2+
// We'll implement Append, Prepend and Remove methods
3+
// of a typical DoubleLinkedList
4+
5+
6+
package main
7+
8+
import (
9+
"errors"
10+
)
11+
12+
// LinkedList represents our list with it's properties
13+
type DoubleLinkedList struct {
14+
Head *DoubleNode
15+
Tail *DoubleNode
16+
Length int
17+
}
18+
19+
// Append adds a node to the end of the list
20+
func (list *DoubleLinkedList) Append(newNode *DoubleNode) {
21+
22+
if list.Length == 0 {
23+
list.Head = newNode
24+
list.Tail = newNode
25+
} else {
26+
lastNode := list.Tail
27+
28+
lastNode.Next = newNode
29+
newNode.Before = lastNode
30+
31+
list.Tail = newNode
32+
}
33+
34+
list.Length++
35+
36+
}
37+
38+
// Prepend adds a node to the start of the list
39+
func (list *DoubleLinkedList) Prepend(newNode *DoubleNode) {
40+
41+
if list.Length == 0 {
42+
list.Head = newNode
43+
list.Tail = newNode
44+
} else {
45+
firstNode := list.Head
46+
list.Head = newNode
47+
newNode.Next = firstNode
48+
}
49+
50+
list.Length++
51+
52+
}
53+
54+
// Remove removes a node from the list
55+
// This has to iterate through the whole list
56+
// to try find the node to remove
57+
// Can be performance costly with large lists
58+
// You might choose to use Arrays in this case
59+
// where we can identify an element by Index
60+
61+
func (list *DoubleLinkedList) Remove(node *DoubleNode) {
62+
63+
if list.Length == 0 {
64+
panic(errors.New("cannot remove element on an empty list"))
65+
}
66+
67+
var previousPost *DoubleNode
68+
currentPost := list.Head
69+
70+
for currentPost.Value != node.Value {
71+
if currentPost.Next == nil {
72+
panic(errors.New("no such element found with value"))
73+
}
74+
75+
previousPost = currentPost
76+
currentPost = currentPost.Next
77+
}
78+
79+
previousPost.Next = currentPost.Next
80+
81+
list.Length--
82+
83+
}

3_postfix_calculator.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package main
2+
3+
import "C"
4+
import (
5+
"fmt"
6+
"time"
7+
)
8+
9+
// Implementing a PostFix calculator in Go
10+
11+
func PostFixCalculator(container Container) int {
12+
13+
var emptyStack Container
14+
15+
t1 := time.Now().Unix()
16+
17+
for _, item := range container.items {
18+
19+
switch v := item.(type) {
20+
default:
21+
fmt.Printf("unexpected type %T", v)
22+
case int:
23+
24+
//push it into the stack
25+
emptyStack.Push(item)
26+
27+
case string:
28+
29+
rightValue := emptyStack.Pop()
30+
leftValue := emptyStack.Pop()
31+
32+
result := 0
33+
34+
if item.(string) == "+" {
35+
result = leftValue.(int) + rightValue.(int)
36+
}
37+
38+
if item.(string) == "*" {
39+
result = leftValue.(int) * rightValue.(int)
40+
}
41+
42+
if item.(string) == "-" {
43+
result = leftValue.(int) - rightValue.(int)
44+
}
45+
46+
if item.(string) == "%" {
47+
result = leftValue.(int) % rightValue.(int)
48+
}
49+
50+
fmt.Println(leftValue, rightValue, item, result)
51+
52+
emptyStack.Push(result)
53+
}
54+
55+
}
56+
57+
t2 := time.Now().Unix()
58+
59+
elapsedTime := t2 - t1
60+
61+
fmt.Println("Finished in seconds", elapsedTime)
62+
63+
return emptyStack.Peek().(int)
64+
}

main.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,23 @@
11
// Run examples here
22
package main
33

4-
import "fmt"
4+
import (
5+
"fmt"
6+
)
57

68
func main() {
79

8-
stackExample()
10+
container := Container{}
11+
stack := container.NewContainer()
12+
stack.Push(5)
13+
stack.Push(6)
14+
stack.Push(7)
15+
stack.Push("*")
16+
stack.Push("+")
17+
stack.Push(1)
18+
stack.Push("-")
919

20+
fmt.Println(PostFixCalculator(container))
1021
}
1122

1223
func stackExample() {

0 commit comments

Comments
 (0)