package main
import (
"fmt"
)
type ListNode struct{
data int
next *ListNode
}
func NewListNode(data int) *ListNode{
return &ListNode{data:data}
}
type List struct {
lenght int
root *ListNode
}
func (o *List)append(data int){
node := NewListNode(data)
if o.root == nil{
o.root = node
o.lenght++
return
}else {
cur := o.root
for {
if cur.next == nil {
cur.next = node
o.lenght++
break
}
cur = cur.next
}
}
}
func (o *List)insert(data,index int){
if index > o.lenght{
o.append(data)
return
}
node := NewListNode(data)
if index <= 1{
node.next = o.root
o.root = node
o.lenght++
return
}
var count int
cur := o.root
for {
if count == index -1 {
tmp := cur.next
node.next = tmp
cur.next = node
o.lenght++
break
}
cur = cur.next
count++
}
}
func (o *List)modify(data,index int) bool {
if o.root == nil && index > o.lenght{
return false
}
if index <= 1{
o.root.data = data
return true
}
var count int
cur := o.root
for {
if count == index -1 {
cur.data = data
break
}
if cur.next == nil{
break
}
cur = cur.next
count++
}
return true
}
func (o *List)del(index int) bool{
if o.root == nil && index > o.lenght{
return false
}
if index == 1{
o.root = o.root.next
o.lenght--
return true
}
var count int
cur := o.root
for {
if count == index - 1 {
cur.next = cur.next.next
o.lenght--
break
}
if cur.next == nil{
break
}
cur = cur.next
count++
}
return true
}
func (o *List)OrDateDel(data int) bool {
if o.root == nil{
return false
}
if o.root.data == data {
o.root = o.root.next
o.lenght--
return true
}
cur := o.root
for {
if cur.data == data{
cur.data = cur.next.data
cur.next = cur.next.next
o.lenght--
break
}
if cur.next == nil{
break
}
cur = cur.next
}
return true
}
func (o *List)Get(index int) *ListNode{
if o.root == nil && index > o.lenght {
return nil
}
var count int
cur := o.root
for {
if count == index -1 {
break
}
count++
cur = cur.next
}
return cur
}
func (o *List)GetInDate(data int) *ListNode{
if o.root == nil {
return nil
}
if o.root.data == data {
return o.root
}
cur := o.root
for {
if cur.data == data{
break
}
cur = cur.next
}
return cur
}
func (o *List)Show(){
if o.root == nil{
return
}
cur := o.root
for {
if cur == nil{
break
}
fmt.Println(cur.data)
cur = cur.next
}
}
func (o *List)Lenght() {
fmt.Println(o.lenght)
}