Skip to content

Commit

Permalink
单链表实现 by golang
Browse files Browse the repository at this point in the history
  • Loading branch information
scissorsfeet committed Oct 5, 2018
1 parent ca069a7 commit 2a8570d
Show file tree
Hide file tree
Showing 2 changed files with 179 additions and 0 deletions.
135 changes: 135 additions & 0 deletions go/06_linkedlist/singlelinkedlist.go
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)
}
44 changes: 44 additions & 0 deletions go/06_linkedlist/singlelinkedlist_test.go
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()
}

0 comments on commit 2a8570d

Please sign in to comment.