【数据结构连载一线性表】【双向循环链表】golang

package main

import (
	"fmt"
	"strconv"
)

type Node struct {
	data interface{}
	pre  *Node
	next *Node
}

type DoubleLinkedList struct {
	length int
	head   *Node
}

func NewDoubleLinkedList() DoubleLinkedList {
	node := Node{}
	l := DoubleLinkedList{
		length: 0,
		head:   &node,
	}
	l.head.next = l.head
	l.head.pre = l.head
	return l
}

func (l *DoubleLinkedList) Head() *Node {
	return l.head
}

func (l *DoubleLinkedList) Tail() *Node {
	return l.head.pre
}

func (l *DoubleLinkedList) Length() int {
	return l.length
}

func (l *DoubleLinkedList) empty() bool {
	return l.head.next == l.head
}

func (l *DoubleLinkedList) exist(value interface{}) bool {
	cur := l.head.next
	for {
		if cur == l.head {
			return false
		}
		if cur.data == value {
			return true
		}
		cur = cur.next
	}
}

func (l *DoubleLinkedList) Get(index int) interface{} {
	n := 0
	cur := l.head.next
	if index >= l.length || index < 0 {
		return -1
	}
	for {
		if cur == l.head {
			return -1
		}
		if n == index {
			return cur.data
		}
		cur = cur.next
		n++
	}
}

func (l *DoubleLinkedList) Add(value interface{}) {
	cur := l.head
	newNode := Node{
		data: value,
	}
	newNode.next = cur.next
	newNode.pre = cur
	cur.next.pre = &newNode
	cur.next = &newNode
	l.length++

}

func (l *DoubleLinkedList) Append(value interface{}) {
	newNode := Node{data: value}
	newNode.pre = l.head.pre
	newNode.next = l.head
	l.head.pre.next = &newNode
	l.head.pre = &newNode
	l.length++
}

func (l *DoubleLinkedList) Insert(index int, value interface{}) {
	if index < 0 {
		panic("位置插入不能为:" + strconv.Itoa(index))
	}
	newNode := Node{data: value}
	cur := l.head
	n := 0
	for {
		if cur.next == l.head {
			break
		}
		if n == index {
			break
		}
		n++
		cur = cur.next
	}
	newNode.pre = cur
	newNode.next = cur.next
	cur.next.pre = &newNode
	cur.next = &newNode
	l.length++
}

func (l *DoubleLinkedList) Index(value interface{}) int {
	cur := l.head.next
	n := 0
	for {
		if cur == l.head {
			return -1
		}
		if cur.data == value {
			return n
		}
		n++
		cur = cur.next
	}
}

func (l *DoubleLinkedList) Delete(index int) {
	cur := l.head
	n := 0
	for {
		if cur.next != l.head {
			if n == index {
				cur.next.pre = cur
				cur.next = cur.next.next
				l.length--
				return
			}
			n++
			cur = cur.next
			continue
		}
		panic(fmt.Sprintf("删除下标不存在, %v", index))
	}
}

func (l *DoubleLinkedList) Remove(value interface{}) {
	cur := l.head
	for {
		if cur.next != l.head {
			if cur.next.data == value {
				cur.next.pre = cur
				cur.next = cur.next.next
				l.length--
				return
			}
			cur = cur.next
			continue
		}
		panic(fmt.Sprintf("删除数据不存在, %v", value))
	}
}
func (l *DoubleLinkedList) Pop() interface{} {
	if l.head.next == nil {
		panic("链表为空")
	}
	cur := l.head.pre
	data := cur.data
	cur.pre.next = l.head
	l.head.pre = cur.pre
	l.length--
	return data
}

func (l *DoubleLinkedList) Show() {
	cur := l.head.next
	for {
		if cur == l.head {
			break
		}
		fmt.Print(cur.data, " ")
		cur = cur.next
	}
	fmt.Println()
}
func main() {
	l := NewDoubleLinkedList()
	fmt.Println(l.empty())
	l.Add(1)
	l.Add(2)
	l.Add(3)
	l.Add(4)
	l.Append(5)
	l.Append(6)
	l.Insert(2, 10)
	l.Show()
	//l.Delete(5)
	//l.Remove(2)
	//l.Show()
	fmt.Println(l.Pop())
	fmt.Println(l.Pop())
	fmt.Println(l.Tail().data)
	l.Show()
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

辛勤的搬砖者

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值