用隨機數生成結構體鍊錶,然後實現在前面插入,在後面插入,在中間插入,指定節點刪除並遍歷等。
package main
import
("fmt"
"math/rand"
)type student struct
func
trans
(head *student)
}func
listlen
(head *student)
intreturn lenth
}func
inserthead
(p *student)
*student
stu.next = p
p =&stu
}return p
}func
inserttail
(p *student)
*student
if head !=
nilelse
}return head
}func
delnode
(head *student, todel int
)*student
else
if todel ==
1else
pre.next = tmpnext.next
}return head
}func
insertmid
(head, toinsitem *student, toins int
)*student
else
else
}case0,
1:toinsitem.next = head
head = toinsitem
default
:for i :=
1; i < toins; i++
pre.next = toinsitem
toinsitem.next = p
}}return head
}func
main()
var stuadd2 = student
var stuadd3 = student
fmt.
println
("在鍊錶list2的第乙個位置增加乙個元素*****==="
) head2 =
insertmid
(head2,
&stuadd1,0)
trans
(head2)
fmt.
println
("after insert, the list2 lenth is: "
,listlen
(head2)
) fmt.
println
("在鍊錶list2的第十的位置增加乙個元素*****==="
) head2 =
insertmid
(head2,
&stuadd2,10)
trans
(head2)
fmt.
println
("after insert, the list2 lenth is: "
,listlen
(head2)
) fmt.
println
("在鍊錶list2的第三個位置增加乙個元素*****==="
) head2 =
insertmid
(head2,
&stuadd3,3)
trans
(head2)
fmt.
println
("after insert, the list2 lenth is: "
,listlen
(head2)
)}
單向鍊錶 建立 插入 刪除 遍歷
include include include using namespace std struct list create 新建鍊錶 struct list insert struct list head,struct list temp 插入 struct list deletes struct...
單向鍊錶的遍歷
鍊錶的遍歷和陣列類似,就是跑鍊錶 輸出單向鍊錶尾結點的值 include include define n 5 typedef struct nodeelemsn elemsn creatlink int a return h elemsn printlink elemsn h return p i...
單向鍊錶的遍歷 查詢
單向鍊錶的建立在這裡不在贅述,詳細請看另一篇文章 單向鍊錶的建立 在本篇文章中,主要通過舉例的方式來幫大家理解單向鍊錶的基本遍歷。本篇文章中建立節點用以下表示 typdef struct node elemsn 例一 輸出單向鍊錶尾節點的值 void printtail elemsn head 傳遞...