571
社区成员
发帖
与我相关
我的任务
分享
type LinkNode struct {
Next *LinkNode
Data int
}
type LinkedList struct {
head *LinkNode
length int
}
func Create() newList LinkedList {
newList = new(MyLinkedList)
newList -> length = 0
mewList -> head = NULL
return
}
func (this *LinkedList) Get(index int) int {
if index < 0 || index >= this.length {
return -1
}
p := this.head
for i := 0; i <= index; i++ {
p = p.next
}
return p.val
}
func (this *LinkedList)AddNewNode(Node *LinkTableNode, position int) bool{
if this == nil || Node == nil {
return false
}
if position > this.length {
fmt.Printf("out of range\n")
return false
}
if position == 0 {
if this.length != 0 {
Node.next = LinkTable.head.next
}
this.head = Node
} else {
var tmpNodepointer *LinkTableNode = this.head
for position != 1 {
tmpNodepointer = tmpNodepointer.next
position -= 1
}
Node.next = tmpNodepointer.next
tmpNodepointer.next = Node
}
this.length += 1
return true
}
func (this *LinkedList) DeleteAtIndex(index int) bool {
if index < 0 || index >= this.length {
return false
}
p := this.head
//找到第index-1个结点
for i := 0; i < index; i++ {
p = p.next
}
q := p.next
p.next = q.next
q.next = nil
this.length--
return true
}
func (this *MyLinkedList) PrintList() {
p := this.head.next
for p != nil {
fmt.Println(p.val)
fmt.Print(" ")
p = p.next
}
}
学号:279李昕泽