顺序表
package main
import (
"errors"
"fmt"
)
type SequenceList struct {
data *[]interface{}
length, capacity int
}
func (s *SequenceList) InitList(InitListSize int) {
data := make([]interface{}, InitListSize)
s.data = &data
s.length = 0
s.capacity = InitListSize
}
func (s SequenceList) GetListLength() int {
return s.length
}
func (s SequenceList) isNull() bool {
if s.length != 0 {
return false
}
return true
}
func (s SequenceList) isFull() bool {
if s.length >= s.capacity {
return true
}
return false
}
func (s *SequenceList) ListInsert(index int, Element interface{}) error {
if index < 1 || index > s.length+1 || s.isFull() {
return errors.New("illegal index")
}
for i := s.length; i >= index; i-- {
(*s.data)[i+1] = (*s.data)[i]
}
(*s.data)[index-1] = Element
s.length++
return nil
}
func (s SequenceList) FindEleByValue(Ele interface{}) interface{} {
for i := 0; i < s.length; i++ {
if (*s.data)[i] == Ele {
return i + 1
}
}
return nil
}
func (s SequenceList) FindEleByIndex(index int) (interface{}, error) {
if index > s.length || index < 1 {
return nil, errors.New("illegal index")
}
return (*s.data)[index-1], nil
}
func (s *SequenceList) ListDelete(index int) (interface{}, error) {
if index > s.length || index < 1 {
return nil, errors.New("illegal index")
}
e := (*s.data)[index-1]
for i := index; i < s.length; i++ {
(*s.data)[i-1] = (*s.data)[i]
if i + 1 >= s.length{
(*s.data)[i] = nil
}
}
s.length--
return e, nil
}
func (s SequenceList) PrintList() {
fmt.Printf("List Show[data:%v, length:%v, capacity:%v]\n", *s.data, s.length, s.capacity)
}
func (s *SequenceList) DestroyList() {
s.data = nil
s.length = 0
s.capacity = 0
}