1.提示错误
rsc.io/quote: module rsc.io/quote: Get "https://proxy.golang.org/rsc.io/quote/@v/list": dial tcp 172.217.163.49:443: i/o timeout
2.解决方式为:
go env -w GOPROXY=https://goproxy.cn,direct
再进行go mod tidy 环境已经下载依赖包
go mod tidy
go: finding module for package rsc.io/quote
go: downloading rsc.io/quote v1.5.2
go: found rsc.io/quote in rsc.io/quote v1.5.2
go: downloading rsc.io/sampler v1.3.0
go: downloading golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c
go 添加链表的Lnode 成功,还有其他的,先跑例子先
go mod tidy
go: finding module for package github.com/isdamir/gotype
go: downloading github.com/isdamir/gotype v0.0.0-20200101084212-6aa1591106b2
go: found github.com/isdamir/gotype in github.com/isdamir/gotype v0.0.0-20200101084212-6aa1591106b2
go: downloading github.com/isdamir/assert v0.0.0-20180331014554-0b0d0ef4fa54
go: downloading github.com/kr/pretty v0.1.0
go: downloading github.com/kr/text v0.1.0
3反序和插入
package main
import (
"fmt"
. "github.com/isdamir/gotype"
)
func Reverse(node *LNode) {
if node == nil || node.Next == nil {return }
p := node.Next //保存原链表的后继
node.Next = nil //将原链表的后继置为空
for p != nil { //循环进行头插法的过程
q := p
p = p.Next //指向后面的位置,用作下一轮使用
//下面两句是用来实现头插法的
q.Next = node.Next
node.Next = q
}
}
func InsertReverse(node *LNode){
if node == nil || node.Next == nil {return }
var cur *LNode
var next *LNode
cur = node.Next.Next
node.Next.Next=nil
for cur!=nil {
next =cur.Next
cur.Next=node.Next
node.Next=cur
cur=next
}
}
func Merge(head1 *LNode, head2 *LNode) *LNode{
if head1 == nil || head1.Next == nil {
return head1
}
if head2 == nil || head2.Next == nil {
return head2
}
cur1:= head1.Next
cur2:= head2.Next
var head *LNode //合并后的头指针
var cur *LNode //合并后的链表的尾指针
if cur1.Data.(int) > cur2.Data.(int){
head=head2
cur=cur2
cur2=cur2.Next
}else{
head=head1
cur=cur1
cur1=cur1.Next
}
for cur1!=nil && cur2!=nil{
if cur1.Data.(int)<cur2.Data.(int){
cur.Next=cur1
cur=cur1
cur1=cur1.Next
}else{
cur.Next=cur2
cur=cur2
cur2=cur2.Next
}
}
if cur1!=nil{
cur.Next=cur1
}
if cur2!=nil{
cur.Next=cur2
}
return head
}
func RemoveNode(node *LNode)bool{
if node == nil || node.Next==nil {
return false
}
node.Data=node.Next.Data
tmp:=node.Next
node.Next=tmp.Next
return true
}
func getNum(arr []int){
if arr == nil || len(arr)<1 {
return
}
data:=map[int]int{}
for _,v:=range arr{
if vv,ok:=data[v];ok{
if vv==1{
data[v]=0
}else{
data[v]=1
}
}else{
data[v]=1
}
}
for _,v:=range arr{
if data[v]==1{
fmt.Println(v)
}
}
}
func CreateNodeT(node *LNode,start int){
cur:=node
for i:=start; i<10; i+=2{
cur.Next=&LNode{}
cur.Next.Data=i
cur=cur.Next
}
}
//建单链表,并且找到get这个链表的指针,并且返回
func CreateNodeT2(node *LNode,get int)(retNode *LNode){
cur:=node
for i:=1; i<10; i+=1{
cur.Next=&LNode{}
cur.Next.Data=i
cur=cur.Next
if i==get{
retNode =cur
}
}
return retNode
}
func main() {
head := &LNode{}
fmt.Println("逆序")
CreateNode(head, 8)
PrintNode("逆序前1:", head)
Reverse(head)
PrintNode("用逆序算法逆序后1:", head)
CreateNode(head, 8)
PrintNode("逆序前2:", head)
InsertReverse(head)
PrintNode("用插入逆序算法逆序后2:", head)
fmt.Println("合并2个有序链条--------------")
head1:=&LNode{}
head2:=&LNode{}
CreateNodeT(head1,1)
CreateNodeT(head2,2)
PrintNode("Head1 链条: ",head1)
PrintNode("Head2 链条: ",head2)
mergehead:=Merge(head1,head2)
PrintNode("合并的链条:",mergehead)
head3:=&LNode{}
retNode:=CreateNodeT2(head3,6)
PrintNode("Head3 链条: ",head3)
RemoveNode(retNode)
PrintNode("Head3 删除6 链条: ",head3)
fmt.Println("Hash Methon")
arr:=[]int{3,5,6,6,5,7,2,2,10}
getNum(arr)
}
显示的结果为:
逆序
逆序前1:1 2 3 4 5 6 7
用逆序算法逆序后1:7 6 5 4 3 2 1
逆序前2:1 2 3 4 5 6 7
用插入逆序算法逆序后2:7 6 5 4 3 2 1