顺序表
// Package sequentialList 线性表的顺序表示与实现
package sequentialList
import "errors"
const (
ListInitSize = 100
ListIncrement=10
)
// SequentialList 顺序表的表示结构,可以动态扩容
type SequentialList struct {
elem *[]interface{}
length int
listSize int
}
// Init 初始化顺序表
func (l *SequentialList) Init() {
data:=make([]interface{},ListInitSize)
l.elem=&data
l.listSize=ListInitSize
l.length=0
}
// Destroy 销毁顺序表
func (l *SequentialList) Destroy() {
l=nil
}
// Clear 清空顺序表
func (l *SequentialList) Clear() {
l.length=0
}
// Empty 判空
func (l *SequentialList) Empty() (result bool) {
if l.length==0{
return true
}else {
return false
}
}
// Length 求顺序表长度
func (l *SequentialList) Length() (length int) {
return l.length
}
// GetElem 取第index个元素
func (l *SequentialList) GetElem(index int) (element interface{}) {
return (*l.elem)[index]
}
// LocateElem 返回顺序表中第一个与e满足compare关系的元素位序,如果这样的元素不满足,返回为-1
func (l *SequentialList) LocateElem(e interface{}) (index int) {
for index, value := range *l.elem {
if value==e{
return index
}
}
return -1
}
// Prior 前驱
func (l *SequentialList) Prior(e interface{}) (previous interface{},err error) {
index:=l.LocateElem(e)
if index==-1{
return nil ,errors.New("顺序表中没有此元素")
}
if index==0{
return nil,errors.New("当前元素为第一个元素,没有前驱")
}
return (*l.elem)[index-1],nil
}
// Next 后继
func (l *SequentialList) Next(e interface{}) (next interface{},err error) {
index:=l.LocateElem(e)
if index==-1{
return nil ,errors.New("顺序表中没有此元素")
}
if index==l.length-1{
return nil,errors.New("当前元素为最后一个元素,没有后继")
}
return (*l.elem)[index+1],nil
}
// Insert 插入
func (l *SequentialList) Insert(index int, element interface{}) (err error) {
if index<0||index>l.length{
return errors.New("要插入元素的位置非法")
}
if l.length>=l.listSize{
newBase :=make([]interface{},l.listSize+ListIncrement)
copy(newBase,*l.elem)
l.elem=&newBase
l.listSize+=ListIncrement
}
for i := l.length-1; i >index-1 ; i-- {
(*l.elem)[i+1]=(*l.elem)[i]
}
(*l.elem)[index]=element
l.length++
return nil
}
// Delete 删除
func (l *SequentialList) Delete(index int) (deletedValue interface{} ,err error) {
if index<=0||index>=l.length-1{
return nil,errors.New("要删除位置非法")
}
deletedValue=(*l.elem)[index]
for i := index; i <l.length-1 ; i++ {
(*l.elem)[i]=(*l.elem)[i+1]
}
l.length--
return deletedValue,nil
}
// Transfer 遍历访问
func (l *SequentialList) Transfer(visit func(e interface{})(result bool,err error))(result bool,err error) {
for _, value := range *l.elem {
ok,err:=visit(value)
if !ok{
return false,err
}
}
return true,nil
}
// Union 求并集
func (l *SequentialList) Union(src *SequentialList) {
for i := 0; i < src.length; i++ {
e:=src.GetElem(i)
if l.LocateElem(e)==-1{
src.Insert(l.length,e)
}
}
}
// Merge 合并两个有序顺序表
func (l *SequentialList) Merge(src *SequentialList) (list *SequentialList) {
list=&SequentialList{}
list.Init()
pa,pb:=0,0
for pa<l.length&&pb<src.length{
if l.GetElem(pa).(int)<src.GetElem(pb).(int){
list.Insert(list.length,l.GetElem(pa))
pa++
}else {
list.Insert(list.length,src.GetElem(pb))
pb++
}
}
for pa<l.length{
list.Insert(list.length,l.GetElem(pa))
pa++
}
for pb<src.length{
list.Insert(list.length,src.GetElem(pb))
pb++
}
return list
}
单链表
// Package linkList 单链表
package linkList
// LinkNode 结点
type LinkNode struct {
data interface{}
next *LinkNode
}
type Link *LinkNode
type Position *LinkNode
// LinkList 单链表
type LinkList struct {
head,tail Link
length int
}
// MakeNode 新建结点
func (l *LinkNode) MakeNode(e interface{}) {
l=&LinkNode{
data: e,
next: nil,
}
}
// FreeNode 释放结点内存
func (l *LinkNode) FreeNode() {
l=nil
}
// InitList 初始化单链表
func (l *LinkList) InitList() {
l=&LinkList{}
l.head=nil
l.tail=l.head
l.length=0
}
// Destroy 销毁单链表
func (l *LinkList) Destroy() {
l=nil
}
// Clear 清空单链表
func (l *LinkList) Clear() {
l.head=nil
l.tail=l.head
l.length=0
}
// Append 追加元素
func (l *LinkList) Append(e interface{}) {
node:=&LinkNode{
data: e,
next: nil,
}
l.tail.next=node
l.tail=l.tail.next
}
// InsertAfter 在指定结点后面插入
func (l *LinkList) InsertAfter(p *LinkNode, node *LinkNode) {
node.next=p.next
p.next=node
}
// Prior 前驱
func (l *LinkList) Prior(p *LinkNode)(pre *LinkNode) {
ptr:=l.head
if ptr==p{
return nil
}else{
for ptr!=nil{
if ptr.next==p{
return ptr
}else {
ptr=ptr.next
}
}
return nil
}
}
// Next 后继
func (l *LinkList) Next(p *LinkNode)(next *LinkNode) {
ptr:=l.head
for ptr!=nil{
if ptr==p{
if ptr.next==nil{
return nil
}
return ptr.next
}else {
ptr=ptr.next
}
}
return nil
}
// GetElem 返回第i个元素
func (l *LinkList) GetElem(index int) (element *LinkNode) {
i:=0
ptr:=l.head
for i<index&&ptr!=nil{
ptr=ptr.next
i++
}
return ptr
}
双向链表
// Package doubleLinkList 双向链表
package doubleLinkList
// DoubleLinkList 双向链表的结构
type DoubleLinkList struct {
data interface{}
prior *DoubleLinkList
next *DoubleLinkList
}
静态链表
// Package staticLinkList 静态单链表
package staticLinkList
const (
MaxSize = 100
)
// Component 静态单链表结点
type Component struct {
data interface{}
cur int
}
// StaticLinkList 静态单链表
type StaticLinkList [MaxSize]Component
顺序栈
// Package stack 栈
package stack
const (
StackInitSize = 100
StackIncrement=10
)
// SequentialStack 顺序栈
type SequentialStack struct {
data []interface{}
top int
stackSize int
}
// Init 初始化栈
func (s *SequentialStack) Init() {
s.data=make([]interface{},StackInitSize)
s.top=0
s.stackSize=StackInitSize
}
// Push 入栈
func (s *SequentialStack) Push(e interface{}) {
if s.top>=s.stackSize-1{
newData:=make([]interface{},StackIncrement+s.stackSize)
copy(newData,s.data)
s.data=newData
s.stackSize+=StackIncrement
}
s.data[s.top]=e
s.top++
}
// Pop 出栈
func (s *SequentialStack) Pop(){
if s.top==0{
return
}
s.top--
}
// GetTop 取栈顶元素
func (s *SequentialStack) GetTop() (e interface{}) {
if s.top==0{
return nil
}
e=s.data[s.top-1]
return e
}
func (s *SequentialStack) Length() (len int) {
return s.top+1
}
链栈
package stack
type LinkStackNode struct {
data interface{}
next *LinkStackNode
}
// LinkStack 链栈
type LinkStack struct {
top *LinkStackNode
length int
}
// Push 入栈
func (s *LinkStack) Push(e interface{}) {
node:=&LinkStackNode{
data: e,
next: nil,
}
node.next= s.top
s.top=node.next
s.length++
}
// Pop 出栈
func (s *LinkStack) Pop() {
s.top=s.top.next
}
func (s LinkStack) GetTop() (e interface{}) {
if s.top==nil{
return nil
}else {
return s.top.data
}
}
链队列
package linkQueue
type QueueNode struct {
data interface{}
next *QueueNode
}
// LinkQueue 链式队列
type LinkQueue struct {
front *QueueNode
rear *QueueNode
}
func (q *LinkQueue) Init() {
q.front=nil
q.rear=q.front
}
// Enqueue 入队
func (q *LinkQueue) Enqueue(e interface{}) {
node:=&QueueNode{}
node.data=e
node.next=nil
q.rear.next=node
q.rear=node
}
// DeQueue 出队
func (q *LinkQueue) DeQueue() (e interface{}) {
if q.front==nil{
return nil
}
e=q.front.data
q.front=q.front.next
return e
}
循环队列
package queue
import "errors"
const (
MaxSize = 100
)
// CircularQueue 循环队列
type CircularQueue struct {
data *[]interface{}
front int
rear int
}
// Init 初始化队列
func (q *CircularQueue) Init() {
data:=make([]interface{},MaxSize)
q.data=&data
q.front=0
q.rear=0
}
// Length 求队列长度
func (q *CircularQueue) Length() (length int){
return (q.rear-q.front+MaxSize)%MaxSize
}
// Enqueue 入队
func (q *CircularQueue) Enqueue(e interface{}) (err error) {
if (q.rear+1)%MaxSize==q.front{
return errors.New("队列满了")
}
(*q.data)[q.rear]=e
q.rear=(q.rear+1)%MaxSize
return nil
}
// DeQueue 出队
func (q *CircularQueue) DeQueue() (e interface{}) {
if q.front==q.rear{
return errors.New("队列空")
}
e=(*q.data)[q.front]
q.front=(q.front+1)%MaxSize
return nil
}