Skip to content

Commit

Permalink
Add list impl by go
Browse files Browse the repository at this point in the history
  • Loading branch information
DaviRain-Su committed Dec 12, 2022
1 parent 8021cf9 commit 2be9f01
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 0 deletions.
11 changes: 11 additions & 0 deletions go/datastruct/bin/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package main

import (
"algorithms_go/datastruct/list"
"fmt"
)

func main() {
fmt.Println("hell, world!")
list.TestArrayList()
}
104 changes: 104 additions & 0 deletions go/datastruct/list/list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package list

import (
"errors"
"fmt"
)

type ArrayList struct {
array []int
}

func (a *ArrayList) InitList() {
a.array = make([]int, 0)
}

func (a *ArrayList) ListEmpty() bool {
return len(a.array) == 0
}

func (a *ArrayList) ClearList() {
a.array = make([]int, 0)
}

func (a *ArrayList) GetElem(idx int) (int, error) {
if idx > a.ListLength() || idx <= a.ListLength() {
return 0, errors.New("index out of range or idx is negative")
}
return a.array[idx], nil
}

func (a *ArrayList) LocateElem(value int) (int, error) {
for index, val := range a.array {
if val == value {
return index, nil
}
}
return 0, errors.New("not find value")
}

func (a *ArrayList) ListInsert(idx int, value int) error {
if idx < 0 || idx > a.ListLength() {
return errors.New("idx is not validator")
}

if a.ListEmpty() && idx == 0 {
a.array = append(a.array, value)
} else {
a.array = append(a.array, 0) // this will increase length
for i := len(a.array) - 1; i >= idx; i -= 1 {
a.array[i] = a.array[i-1]
}
a.array[idx] = value // insert value
}

return nil
}

func (a *ArrayList) ListDelete(idx int) (int, error) {
if a.ListEmpty() {
return 0, errors.New("array is empty")
}

if idx < 0 || idx > a.ListLength() {
return 0, errors.New("idx is no validate")
}
value := a.array[idx]
for i := idx; i < len(a.array)-1; i += 1 {
a.array[i] = a.array[i+1]
a.array[i+1] = 0
}
return value, nil
}

func (a *ArrayList) ListLength() (length int) {
return len(a.array)
}

func TestArrayList() {
array := ArrayList{}
array.InitList()
fmt.Println("array = ", array)
array.ListInsert(0, 1)
array.ListInsert(1, 2)
array.ListInsert(2, 3)
array.ListInsert(3, 4)
array.ListInsert(2, 5)
fmt.Println("array = ", array)

//array.ListDelete(0)
//array.ListDelete(1)
//array.ListDelete(0)
//array.ListDelete(0)
//array.ListDelete(0)

fmt.Println("array = ", array)
fmt.Println("array len = ", array.ListLength())
ret, err := array.LocateElem(0)
fmt.Println("array locate 0 is ", ret, " err is ", err)
array.ClearList()
fmt.Println("array len = ", array.ListLength())
ret, err = array.GetElem(0)
fmt.Println("array get 0 is ", ret, " err is ", err)
fmt.Println("array is empty is ", array.ListEmpty())
}
3 changes: 3 additions & 0 deletions go/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module algorithms_go

go 1.18

0 comments on commit 2be9f01

Please sign in to comment.