Often test data structures: arrays, strings, linked lists, trees, stacks, queues
Array and string are the most common data structures: a continuous storing memory numbers and characters.
Lists and trees are very common data structures, lists and trees since the operation requires a lot of pointers, program robustness of attention.
Recursive stack is a data structure associated with the same breadth-first algorithm will be related to the queue. Both deep understanding of data structures.
1. Array
Continuous memory, for all the pre-allocated memory array.
In order to solve the space problem is not high efficiency of the array, and the emergence of a variety of dynamic arrays.
package main
import (
"fmt"
"github.com/pkg/errors"
)
//链表
type link struct {
id int32
next *link
}
func(head *link)addToTail(val int32) error{
pNew := link{id:val}
if head == nil {
return errors.New("operate nil link")
}
for {
if head.next != nil{
head = head.next
}else {
head.next = &pNew
break
}
}
return nil
}
func main1(){
node0 := link{id:0}
node1 := link{id:1}
node2 := link{id:2}
node3 := link{id:3}
node0.next = &node1
node1.next = &node2
node2.next = &node3
node0.addToTail(4)
fmt.Println(node3.next)
}
//栈
type stack struct {
array []int
indexNow int
}
//push
func(s *stack) pushStack(val int) int{
index := s.indexNow
index++
s.array[index] = val
return index
}
func(s *stack) pop() int{
index := s.indexNow
val := s.array[index]
index--
return val
}
//tree 未测试呢
type treeNode struct {
id int64
left *treeNode
right *treeNode
}
func(rootNode *treeNode)traverse(){
//理想情况
fmt.Println(rootNode.id)
if rootNode.left !=nil && rootNode.right != nil {
fmt.Println(rootNode.id)
rootNode.right.traverse()
rootNode.left.traverse()
}
}