-
-
Save MelvinTo/2224734 to your computer and use it in GitHub Desktop.
try to answer question gist: 2216546, 刚学习Go,写得比较粗糙,简单测试一下没发现问题
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
package main | |
import ( | |
"fmt" | |
"strconv" | |
"time" | |
) | |
type Node struct { | |
prev, next *Node | |
val string | |
} | |
type HashNode struct { | |
prev, next *HashNode | |
val *Node | |
} | |
type HashLinklist struct { | |
head *HashNode | |
tail *HashNode | |
count int | |
} | |
func (l *HashLinklist) Init() { | |
l.head = nil | |
l.tail = nil | |
l.count = 0 | |
} | |
func (l *HashLinklist) AddFirst(node *Node) { | |
n := &HashNode {} | |
n.val = node | |
n.next = l.head | |
n.prev = nil | |
l.head = n | |
l.count ++ | |
} | |
func (l *HashLinklist) AddLast(node *Node) { | |
n := &HashNode {} | |
n.val = node | |
n.next = nil | |
n.prev = l.tail | |
l.tail = n | |
l.count ++ | |
} | |
func (l *HashLinklist) RemoveFirst() { | |
if l.head != nil && l.tail != nil { | |
l.head = l.head.next | |
} | |
} | |
func (l *HashLinklist) RemoveLast() { | |
if l.head != nil && l.tail != nil { | |
l.tail = l.tail.prev | |
} | |
} | |
func (l *HashLinklist) Reverse() { | |
tmp := l.head | |
l.head = l.tail | |
l.tail = tmp | |
} | |
type Linklist struct { | |
hash map[string] *HashLinklist | |
head *Node | |
tail *Node | |
count int64 | |
reverseFlag bool | |
} | |
func (l *Linklist) Init() { | |
l.hash = make(map[string] *HashLinklist) | |
l.head = nil | |
l.tail = nil | |
l.count = 0 | |
l.reverseFlag = false | |
} | |
func (l *Linklist) AddFirst(item string) { | |
n := &Node {} | |
n.val = item | |
n.next = l.head | |
n.prev = nil | |
l.head = n | |
l.count ++ | |
if _,ok := l.hash[item]; ! ok { | |
ll := &HashLinklist {} | |
ll.Init() | |
l.hash[item] = ll | |
} | |
l.hash[item].AddFirst(n) | |
} | |
func (l *Linklist) AddLast(item string) { | |
n := &Node {} | |
n.val = item | |
n.next = nil | |
n.prev = l.tail | |
l.tail = n | |
l.count ++ | |
if _,ok := l.hash[item]; ! ok { | |
ll := &HashLinklist {} | |
ll.Init() | |
l.hash[item] = ll | |
} | |
l.hash[item].AddLast(n) | |
} | |
func (l *Linklist) Remove(item string) { | |
node := l.hash[item].head.val | |
if node.next != nil { | |
node.next.prev = node.prev | |
} | |
if node.prev != nil { | |
node.prev.next = node.next | |
} | |
if _,ok := l.hash[item]; ok { | |
if l.reverseFlag { | |
l.hash[item].RemoveLast() | |
} else { | |
l.hash[item].RemoveFirst() | |
} | |
} | |
} | |
func (l *Linklist) Reverse() { | |
tmp := l.head | |
l.head = l.tail | |
l.tail = tmp | |
l.reverseFlag = ! l.reverseFlag | |
} | |
func (l *Linklist) TestAddFirst() { | |
for j := 0; j < 100; j++ { | |
before := time.Now() | |
beforeInNano := before.UnixNano() | |
for i := 0; i < 100000; i++ { | |
l.AddFirst(strconv.Itoa(j*100000 + i)) | |
} | |
after := time.Now() | |
afterInNano := after.UnixNano() | |
fmt.Println((afterInNano - beforeInNano) / l.count) | |
} | |
} | |
func (l *Linklist) TestAddLast() { | |
for j := 0; j < 100; j++ { | |
before := time.Now() | |
beforeInNano := before.UnixNano() | |
for i := 0; i < 100000; i++ { | |
l.AddLast(strconv.Itoa(j*100000 + i)) | |
} | |
after := time.Now() | |
afterInNano := after.UnixNano() | |
fmt.Println((afterInNano - beforeInNano) / l.count) | |
} | |
} | |
func (l *Linklist) TestRemove() { | |
for j := 50; j < 100; j++ { | |
before := time.Now() | |
beforeInNano := before.UnixNano() | |
for i := 0; i < 100000; i++ { | |
l.Remove(strconv.Itoa(j*100000 + i)) | |
} | |
after := time.Now() | |
afterInNano := after.UnixNano() | |
fmt.Println((afterInNano - beforeInNano) / l.count) | |
} | |
} | |
func (l *Linklist) TestReverse() { | |
l.Reverse() | |
} | |
func main() { | |
l := &Linklist {} | |
l.Init() | |
l.TestAddFirst() | |
l.TestReverse() | |
l.TestRemove() | |
l.TestReverse() | |
l.TestAddLast() | |
fmt.Println("Count: ", l.count) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment