数据结构—栈与队列
相关数据结构实现用go语言实现
相关代码做题合集:https://github.com/longpi1/algorithm-pattern
go 通过切片模拟栈和队列
栈 栈是一种后进先出(LIFO, Last-In-First-Out)的数据结构。Go 没有内置的栈类型,但可以使用切片或链表实现栈。
定义和特点
栈支持两种主要操作:
Push
(入栈):将元素添加到栈顶。
Pop
(出栈):从栈顶移除并返回元素。
其他辅助操作包括检查栈是否为空、获取栈长度等。
1 2 3 4 5 6 7 8 9 10 切片模拟栈 stack:=make ([]int ,0 ) stack=append (stack,10 ) v:=stack[len (stack)-1 ] stack=stack[:len (stack)-1 ]len (stack)==0
队列 队列是一种先进先出(FIFO, First-In-First-Out)的数据结构。Go 没有内置的队列类型,但可以使用切片或链表实现队列。
定义和特点
队列支持两种主要操作:
Enqueue
(入队):将元素添加到队列尾部。
Dequeue
(出队):从队列头部移除并返回元素。
其他辅助操作包括检查队列是否为空、获取队列长度等。
1 2 3 4 5 6 7 8 9 queue:=make ([]int ,0 ) queue=append (queue,10 ) v:=queue[0 ] queue=queue[1 :]len (queue)==0
相关练习题 20. 有效的括号
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 func isValid (s string ) bool { n := len (s) if n % 2 == 1 { return false } m := map [rune ]rune { '(' : ')' , '{' : '}' , '[' : ']' , } var stack []rune for _, char := range s { if _, ok := m[char]; ok { stack = append (stack, char) } else { if len (stack) == 0 { return false } lastOpening := stack[len (stack)-1 ] if m[lastOpening] != char { return false } stack = stack[:len (stack)-1 ] } } return len (stack) == 0 }
232. 用栈实现队列
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 type MyQueue struct { in, out []int }func Constructor () MyQueue { return MyQueue{in: make ([]int , 0 ), out: make ([]int , 0 )} }func (this *MyQueue) Push(x int ) { for len (this.out) != 0 { num := this.out[len (this.out)-1 ] this.in = append (this.in, num) this.out = this.out[0 : len (this.out)-1 ] } this.in = append (this.in, x) }func (this *MyQueue) Pop() int { for len (this.in) != 0 { num := this.in[len (this.in)-1 ] this.out = append (this.out, num) this.in = this.in[0 : len (this.in)-1 ] } result := this.out[len (this.out)-1 ] this.out = this.out[0 : len (this.out)-1 ] return result }func (this *MyQueue) Peek() int { for len (this.in) != 0 { num := this.in[len (this.in)-1 ] this.out = append (this.out, num) this.in = this.in[0 : len (this.in)-1 ] } if len (this.out) == 0 { return -1 } return this.out[len (this.out)-1 ] }func (this *MyQueue) Empty() bool { return len (this.in) == 0 && len (this.out) == 0 }
225. 用队列实现栈
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 type MyStack struct { queue1, queue2 []int }func Constructor () MyStack { return MyStack{queue1: make ([]int , 0 ), queue2: make ([]int , 0 )} }func (this *MyStack) Push(x int ) { this.queue2 = append (this.queue2, x) for len (this.queue1) != 0 { tmp := this.queue1[0 ] this.queue1 = this.queue1[1 :] this.queue2 = append (this.queue2, tmp) } this.queue1, this.queue2 = this.queue2, this.queue1 }func (this *MyStack) Pop() int { tmp := this.queue1[0 ] this.queue1 = this.queue1[1 :] return tmp }func (this *MyStack) Top() int { return this.queue1[0 ] }func (this *MyStack) Empty() bool { return len (this.queue1) == 0 }