forked from wangzheng0822/algo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ca069a7
commit 2a8570d
Showing
2 changed files
with
179 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
package _6_linkedlist | ||
|
||
import "fmt" | ||
|
||
/* | ||
单链表基本操作 | ||
author:leo | ||
*/ | ||
|
||
type ListNode struct { | ||
next *ListNode | ||
value interface{} | ||
} | ||
|
||
type LinkedList struct { | ||
head *ListNode | ||
length uint | ||
} | ||
|
||
func NewListNode(v interface{}) *ListNode { | ||
return &ListNode{nil, v} | ||
} | ||
|
||
func (this *ListNode) GetNext() *ListNode { | ||
return this.next | ||
} | ||
|
||
func (this *ListNode) GetValue() interface{} { | ||
return this.value | ||
} | ||
|
||
func NewLinkedList() *LinkedList { | ||
return &LinkedList{NewListNode(0), 0} | ||
} | ||
|
||
//在某个节点后面插入节点 | ||
func (this *LinkedList) InsertAfter(p *ListNode, v interface{}) bool { | ||
if nil == p { | ||
return false | ||
} | ||
newNode := NewListNode(v) | ||
oldNext := p.next | ||
p.next = newNode | ||
newNode.next = oldNext | ||
this.length++ | ||
return true | ||
} | ||
|
||
//在某个节点前面插入节点 | ||
func (this *LinkedList) InsertBefore(p *ListNode, v interface{}) bool { | ||
if nil == p || p == this.head { | ||
return false | ||
} | ||
cur := this.head.next | ||
pre := this.head | ||
for nil != cur { | ||
if cur == p { | ||
break | ||
} | ||
pre = cur | ||
cur = cur.next | ||
} | ||
if nil == cur { | ||
return false | ||
} | ||
newNode := NewListNode(v) | ||
pre.next = newNode | ||
newNode.next = cur | ||
this.length++ | ||
return true | ||
} | ||
|
||
//在链表头部插入节点 | ||
func (this *LinkedList) InsertToHead(v interface{}) bool { | ||
return this.InsertAfter(this.head, v) | ||
} | ||
|
||
//在链表尾部插入节点 | ||
func (this *LinkedList) InsertToTail(v interface{}) bool { | ||
cur := this.head | ||
for nil != cur.next { | ||
cur = cur.next | ||
} | ||
return this.InsertAfter(cur, v) | ||
} | ||
|
||
//通过索引查找节点 | ||
func (this *LinkedList) FindByIndex(index uint) *ListNode { | ||
if index >= this.length { | ||
return nil | ||
} | ||
cur := this.head.next | ||
var i uint = 0 | ||
for ; i < index; i++ { | ||
cur = cur.next | ||
} | ||
return cur | ||
} | ||
|
||
//删除传入的节点 | ||
func (this *LinkedList) DeleteNode(p *ListNode) bool { | ||
if nil == p { | ||
return false | ||
} | ||
cur := this.head.next | ||
pre := this.head | ||
for nil != cur { | ||
if cur == p { | ||
break | ||
} | ||
pre = cur | ||
cur = cur.next | ||
} | ||
if nil == cur { | ||
return false | ||
} | ||
pre.next = p.next | ||
p = nil | ||
this.length-- | ||
return true | ||
} | ||
|
||
//打印链表 | ||
func (this *LinkedList) Print() { | ||
cur := this.head.next | ||
format := "" | ||
for nil != cur { | ||
format += fmt.Sprintf("%+v", cur.GetValue()) | ||
cur = cur.next | ||
if nil != cur { | ||
format += "->" | ||
} | ||
} | ||
fmt.Println(format) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package _6_linkedlist | ||
|
||
import "testing" | ||
|
||
func TestInsertToHead(t *testing.T) { | ||
l := NewLinkedList() | ||
for i := 0; i < 10; i++ { | ||
l.InsertToHead(i + 1) | ||
} | ||
l.Print() | ||
} | ||
|
||
func TestInsertToTail(t *testing.T) { | ||
l := NewLinkedList() | ||
for i := 0; i < 10; i++ { | ||
l.InsertToTail(i + 1) | ||
} | ||
l.Print() | ||
} | ||
|
||
func TestFindByIndex(t *testing.T) { | ||
l := NewLinkedList() | ||
for i := 0; i < 10; i++ { | ||
l.InsertToTail(i + 1) | ||
} | ||
t.Log(l.FindByIndex(0)) | ||
t.Log(l.FindByIndex(9)) | ||
t.Log(l.FindByIndex(5)) | ||
t.Log(l.FindByIndex(11)) | ||
} | ||
|
||
func TestDeleteNode(t *testing.T) { | ||
l := NewLinkedList() | ||
for i := 0; i < 3; i++ { | ||
l.InsertToTail(i + 1) | ||
} | ||
l.Print() | ||
|
||
t.Log(l.DeleteNode(l.head.next)) | ||
l.Print() | ||
|
||
t.Log(l.DeleteNode(l.head.next.next)) | ||
l.Print() | ||
} |