GO queue implementation language

 
Queue (Queue) is allowed only in the insertion end and at the other end of the linear table delete operation.

A queue is a FIFO t (First In First Out) linear form, referred to as FIFO. It allows the insertion of the tail end, allowing the team to remove the head end. Queue operation is not allowed in the middle of! Suppose queue is q = (a1, a2, ......, an), so that the head elements a1, and is an element of the tail. In this way we can delete, always start from a1, and when inserted, is always last in the queue. It is also more in line with our usual life habits, ranked in priority to the first column, and finally to the team, of course, came in last. As shown below:

 

package main

import (
	"fmt"
)

type QueueNode struct {
	Data interface{}
	Next *QueueNode
}

// Create chain columns (data)
func (queue *QueueNode) Create(Data ...interface{}) {
	if queue == nil {
		return
	}
	if len(Data) == 0 {
		return
	}

	// Create chain column
	for _, v := range Data {
		newNode := new(QueueNode)
		newNode.Data = v

		queue.Next = newNode
		queue = queue.Next
	}

}

// print column chain
func (queue *QueueNode) Print() {
	if queue == nil {
		return
	}
	for queue != nil {
		if queue.Data != nil {
			fmt.Print(queue.Data, " ")
		}
		queue = queue.Next
	}
	fmt.Println()
}

The number of chain column //
func (queue *QueueNode) Length() int {
	if queue == nil {
		return -1
	}

	i := 0
	for queue.Next != nil {
		i++
		queue = queue.Next
	}
	return i
}

// into the column (insert)
func (queue *QueueNode) Push(Data interface{}) {
	// at the end of the queue

	if queue == nil {
		return
	}
	if Data == nil {
		return
	}

	// find the end of the queue
	for queue.Next != nil {
		queue = queue.Next
	}

	// Create a new node to a new node is added at the end of the queue
	newNode := new(QueueNode)
	newNode.Data = Data

	queue.Next = newNode
}

// dequeue (delete)
func (queue *QueueNode) Pop() {
	// teams head out of the line
	if queue == nil {
		return
	}
	// record lined up the first node
	//node:=queue.Next
	//queue.Next=node.Next

	queue.Next = queue.Next.Next
}
 

 

Guess you like

Origin www.cnblogs.com/lurenq/p/12081861.html