-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathllist-sum.go
98 lines (77 loc) · 1.25 KB
/
llist-sum.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package main
import "fmt"
import "math"
type List struct {
head *Node
tail *Node
}
type Node struct {
value float64
prev *Node
next *Node
}
var count int
var size int
var intlist float64
func (l *List) FindSize() int {
count = 0
for l.head != nil {
count = count + 1
l.head = l.head.next
}
return count
}
func (l *List) Show() {
n := l.head
for n != nil {
fmt.Println(n.value)
n = n.next
}
}
func (l *List) InsertFront(value float64) {
n := &Node{
value: value,
next: l.head,
}
if l.head != nil {
l.head.prev = n
}
l.head = n
if l.tail == nil {
l.tail = n
}
}
func (l *List) ConvertToInteger(size int) float64 {
n := l.tail
s := 0
intlist = 0
for n != nil && s <= size {
intlist = intlist + ((n.value) * (math.Pow10(s)))
n = n.prev
s = s + 1
}
return intlist
}
func main() {
l1 := List{}
l2 := List{}
l1.InsertFront(9)
l1.InsertFront(6)
l1.InsertFront(8)
l2.InsertFront(6)
l2.InsertFront(5)
l2.InsertFront(4)
l2.InsertFront(9)
l1.Show()
l2.Show()
s1 := l1.FindSize()
s2 := l2.FindSize()
if s1 >= s2 {
size = s1
}
size = s2
intlist1 := l1.ConvertToInteger(size)
intlist2 := l2.ConvertToInteger(size)
sum := intlist1 + intlist2
fmt.Printf("Sum of %v + %v = %v\n", intlist1,intlist2, sum)
}