双向链表 实现Lpop和Rpush功能
package main
import "fmt"
type ListNode struct {
pre_pointer *ListNode // 前置指针
next_pointer *ListNode //后置指针
value string //存储数据
}
type ListLink struct {
head *ListNode // 头节点
tail *ListNode // 尾节点
length int // 链表长度
}
// 为ListLink 添加Rinsert方法 接收类型为指针
func (l *ListLink) Rpush(data string) *ListLink {
newNode := &ListNode{value: data}
//fmt.Println(newNode)
if l.head == nil{
l.head = newNode
l.tail = newNode
l.length += 1 // 链表长度+1
return l
}
tail_node := l.tail
tail_node.next_pointer = newNode
newNode.pre_pointer = tail_node
l.tail = newNode
l.length += 1 // 链表长度+1
return l
}
// 从链表左侧删除
func (l *ListLink) Lpop() *ListLink {
l.length -= 1 // 链表长度-1
if l.length == 0 {
return l
}
if l.head.next_pointer == nil{
l.head = nil
return l
}
new_head := l.head.next_pointer
new_head.pre_pointer = nil
l.head = new_head
return l
}
func main() {
innodb_cache := ListLink{}
fmt.Println(innodb_cache)
innodb_cache.Rpush("1")
innodb_cache.Rpush("2")
innodb_cache.Rpush("3")
fmt.Println(innodb_cache)
innodb_cache.Lpop()
fmt.Println(innodb_cache)
}