队列
队列-数组实现
type Queue struct {
Queue []int
front int
rear int
}
func (this *Queue) MakeQueue(size int) error {
if size <= 0 {
return errors.New("队列大小至少为1")
}
this.Queue = make([]int, size)
return nil
}
func (this *Queue) Push(num int) error {
if this.size() == len(this.Queue) {
return errors.New("队列已满")
}
this.Queue[this.rear] = num
this.rear++
return nil
}
func (this *Queue) Pop() (int, error) {
if this.isEmpty() {
return -1, errors.New("队列为空")
}
res := this.Queue[this.front]
this.front++
return res, nil
}
func (this *Queue) isEmpty() bool {
return this.front == this.rear
}
func (this *Queue) size() int {
return this.rear - this.front
}
队列-链表实现
type Node struct {
val int
next *Node
}
type NodeQueue struct {
node Node
front *Node
rear *Node
}
func (this *NodeQueue) MakeQueue() {
this.node.val = -1
this.node.next = nil
this.front = &this.node //都指向头结点
this.rear = &this.node
}
func (this *NodeQueue) Push(num int) {
this.rear.val = num
this.rear.next = &Node{}
this.rear = this.rear.next
}
func (this *NodeQueue) Pop() (int, error) {
if this.IsEmpty() {
return -1, errors.New("队列为空")
}
res := this.front.val
heap := this.front
this.front = this.front.next
heap.next = nil
return res, nil
}
func (this *NodeQueue) IsEmpty() bool {
return this.rear == this.front
}
func (this *NodeQueue) size() (res int) {
res = 0
for heap := this.front; heap != this.rear; heap = heap.next {
res++
}
return res
}
func (this *NodeQueue) peek() (res int, err error) {
if this.IsEmpty() {
return -1, errors.New("队列为空")
}
return this.front.val, nil
}
队列-切片实现
type Queue struct {
Queue []int
front int
rear int
}
func (this *Queue) InitQueue() {
this.Queue = nil
this.front = 0
this.rear = 0
}
func (this *Queue) Push(num int) {
this.Queue = append(this.Queue, num)
this.rear++
}
func (this *Queue) Pop() (int, error) {
if this.isEmpty() {
return -1, errors.New("队列为空")
}
res := this.Queue[this.front]
this.rear--
this.Queue = this.Queue[this.front+1:]
return res, nil
}
func (this *Queue) isEmpty() bool {
return this.front == this.rear
}
func (this *Queue) size() int {
return this.rear - this.front
}
循环队列-数组实现
package main
type MyCircularQueue struct {
queue []int
front int
rear int
size int
}
func Constructor(k int) MyCircularQueue {
return MyCircularQueue{
queue: make([]int, k),
front: 0,
rear: 0,
size: 0,
}
}
func (this *MyCircularQueue) EnQueue(value int) bool {
if this.IsFull() {
return false
}
this.queue[this.rear] = value
this.rear = (this.rear + 1) % len(this.queue)
this.size++
return true
}
func (this *MyCircularQueue) DeQueue() bool {
if this.IsEmpty() {
return false
}
this.front = (this.front + 1) % len(this.queue)
this.size--
return true
}
func (this *MyCircularQueue) Front() int {
if this.IsEmpty() {
return -1
}
return this.queue[this.front]
}
func (this *MyCircularQueue) Rear() int {
if this.IsEmpty() {
return -1
}
return this.queue[(this.rear+len(this.queue)-1)%len(this.queue)]
}
func (this *MyCircularQueue) IsEmpty() bool {
if this.size == 0 {
return true
}
return false
}
func (this *MyCircularQueue) IsFull() bool {
if this.size == len(this.queue) {
return true
}
return false
}
栈
栈-数组实现
type Stack struct {
top int
stack []int
limit int
}
func Constructor(size int) Stack {
return Stack{
top: 0,
stack: make([]int, size),
limit: size,
}
}
func (this *Stack) Push(num int) {
if this.IsFull() {
fmt.Println("Stack is full")
return //满了,无法push
}
this.stack[this.top] = num
this.top++
}
func (this *Stack) Pop() (res int) {
if this.IsEmpty() {
fmt.Println("Stack is empty")
return -1
}
this.top--
res = this.stack[this.top]
return res
}
func (this *Stack) IsEmpty() bool {
if this.top == 0 {
return true
}
return false
}
func (this *Stack) IsFull() bool {
if this.top == this.limit {
return true
}
return false
}
栈-链表实现
type Node struct {
val int
next *Node
}
type Stack struct {
top *Node
size int
}
func Constructor() Stack {
return Stack{
top: nil,
size: 0,
}
}
func (this *Stack) Push(num int) {
node := Node{
val: num,
next: this.top,
}
this.top = &node
this.size++
}
func (this *Stack) Pop() (res int) {
if this.IsEmpty() {
fmt.Println("Stack is empty")
return -1
}
res = this.top.val
this.top = this.top.next
this.size--
return res
}
func (this *Stack) IsEmpty() bool {
if this.size == 0 {
return true
}
return false
}
func (this *Stack) Size() int {
return this.size
}
栈-切片实现
type Stack struct {
top int
stack []int
size int
}
func Constructor() Stack {
return Stack{
top: 0,
stack: nil,
size: 0,
}
}
func (this *Stack) Push(num int) {
this.stack = append(this.stack, num)
this.top++
}
func (this *Stack) Pop() (res int) {
if this.IsEmpty() {
fmt.Println("Stack is empty")
return -1
}
this.top--
res = this.stack[this.top]
this.stack = this.stack[:this.top]
return res
}
func (this *Stack) IsEmpty() bool {
if this.top == 0 {
return true
}
return false
}