package mainimport ("fmt"
)//双链表结构
type TwoLinkTable struct {no int name stringpre *TwoLinkTablenext *TwoLinkTable
}//直接在队尾插入
func InsertNode(head *TwoLinkTable, newNode *TwoLinkTable) {temp := headfor {if temp.next == nil {break}temp = temp.next}temp.next = newNodenewNode.pre = temp
}//按编号no从小到大顺序插入
func InsertNode2(head *TwoLinkTable, newNode *TwoLinkTable) {temp := headfor {if temp.next == nil {break}else if temp.next.no >= newNode.no {break}temp = temp.next}newNode.next = temp.nextnewNode.pre = tempif temp.next != nil {temp.next.pre = newNode}temp.next = newNode}//按no编号删除
func DelNode(head *TwoLinkTable, id int) {temp := headif temp.next == nil {fmt.Println("该队列为空!")return}for {if temp.next == nil {break} else if temp.next.no == id {if temp.next.next != nil {temp.next.next.pre = temp}temp.next = temp.next.nextreturn}temp = temp.next}fmt.Println("未找到该编号的对象")
}//正序显示
func ListNodeASC(head *TwoLinkTable) {temp := headif temp.next == nil {fmt.Println("该队列为空!")return}for {if temp.next == nil {break}fmt.Printf("%d:%s\t",temp.next.no,temp.next.name)temp = temp.next}
}//倒序显示
func ListNodeDESC(head *TwoLinkTable) {temp := headif temp.next == nil {fmt.Println("该队列为空!")return}for {if temp.next == nil {break}temp = temp.next}for {fmt.Printf("%d:%s\t",temp.no,temp.name)temp = temp.preif temp.pre == nil {break}}
}func main() {head := &TwoLinkTable{}newNode1 := &TwoLinkTable {no : 1,name : "a",}newNode2 := &TwoLinkTable {no : 2,name : "b",}newNode3 := &TwoLinkTable {no : 3,name : "c",}InsertNode2(head, newNode2)InsertNode2(head, newNode1)InsertNode2(head, newNode3)DelNode(head,3)fmt.Println("正序:")ListNodeASC(head)fmt.Println("\n逆序:")ListNodeDESC(head)
}