package main
import (
"fmt"
"strconv"
)
type Node struct {
data interface{}
next *Node
}
type LinkedList struct {
length int
head *Node
tail *Node
}
func NewLinkedList() LinkedList {
node := Node{}
l := LinkedList{
length: 0,
head: &node,
tail: &node,
}
return l
}
func (l *LinkedList) Head() *Node {
return l.head
}
func (l *LinkedList) Tail() *Node {
return l.tail
}
func (l *LinkedList) Length() int {
return l.length
}
func (l *LinkedList) empty() bool {
return l.length == 0
}
func (l *LinkedList) exist(value interface{}) bool {
cur := l.head.next
for {
if cur == nil {
return false
}
if cur.data == value {
return true
}
cur = cur.next
}
}
func (l *LinkedList) Get(index int) interface{} {
n := 0
cur := l.head.next
if index >= l.length || cur == nil {
return -1
}
for {
if n > index || n > l.length {
return -1
}
if n == index {
return cur.data
}
n++
cur = cur.next
}
}
func (l *LinkedList) Add(value interface{}) {
cur := l.head
newNode := Node{
data: value,
next: cur.next,
}
if l.tail == l.head {
l.tail = &newNode
}
l.head.next = &newNode
l.length++
}
func (l *LinkedList) Append(value interface{}) {
newNode := Node{value, nil}
l.tail.next = &newNode
l.tail = &newNode
l.length++
}
func (l *LinkedList) Insert(index int, value interface{}) {
if index < 0 {
panic("位置插入不能为:" + strconv.Itoa(index))
}
newNode := Node{
data: value,
}
cur := l.head
n := 0
if index >= l.length {
index = l.length
}
for {
if cur == nil {
break
}
if n == index {
newNode.next = cur.next
cur.next = &newNode
l.length++
break
}
n++
cur = cur.next
}
}
func (l *LinkedList) Index(value interface{}) int {
cur := l.head.next
n := 0
for {
if cur == nil {
return -1
}
if cur.data == value {
return n
}
n++
cur = cur.next
}
}
func (l *LinkedList) Delete(index int) {
if index >= l.length {
panic("下标不存在" + strconv.Itoa(index))
}
cur := l.head
n := 0
for {
if cur == nil {
panic("下标不存在" + strconv.Itoa(index))
}
if n == index {
cur.next = cur.next.next
l.length--
break
}
n++
cur = cur.next
}
}
func (l *LinkedList) Remove(value interface{}) {
cur := l.head
next := cur.next
for {
if cur == nil || next == nil {
panic(fmt.Sprintf("数据不存在: %v", value))
}
if next.data == value {
cur.next = next.next
l.length--
break
}
cur = cur.next
next = next.next
}
}
func (l *LinkedList) Pop() interface{} {
if l.length == 0 {
panic("链表为空")
}
cur := l.head
next := cur.next
for {
if next.next == nil {
break
}
cur = cur.next
next = next.next
}
data := next.data
cur.next = nil
l.length--
return data
}
func (l *LinkedList) Show() {
cur := l.head.next
for {
if cur == nil {
break
}
fmt.Print(cur.data, " ")
cur = cur.next
}
fmt.Println()
}
func main() {
l := NewLinkedList()
l.Add(10)
l.Add(9)
l.Add(8)
l.Append(7)
l.Insert(0, 3)
l.Show()
l.Remove(9)
fmt.Println("下标为", l.Index(7))
fmt.Println("链表长度", l.Length())
fmt.Println("链表是否为空", l.empty())
fmt.Println("链表头节点", l.Head())
fmt.Println("链表尾节点", l.Tail())
l.Show()
fmt.Println("删除元素", l.Pop())
fmt.Println("删除元素", l.Pop())
fmt.Println("删除元素", l.Pop())
fmt.Println("删除元素", l.Pop())
fmt.Println("删除元素", l.Pop())
l.Show()
}
04-19
1936
03-10
1546
03-30
702
08-17
1万+