用单向链表模拟队列

示意图

入队列:

让 rear 指针指向的节点的 Next 指针指向新节点,然后让 rear 指针指向下一个节点。

出队列:

每次出队列时,都让 font 指针指向 head 节点的下一个节点。

然后让 head 指针的 Next 指向 front 指针指向的节点的下一个节点,如图所示:

最后从队列中弹出 front 节点。

 

完整代码(Golang)

/*
	数据结构
	队列 链表
	创建一个链表模拟队列
	实现:数据入队列、数据出队列、显示队列
*/
package main

import (
	"errors"
	"fmt"
)

type Node struct {
	Num   int
	Next  *Node
	front *Node
	rear  *Node
}

// 数据入队列
func (this *Node) Insert(head *Node, num int) {

	newNode := &Node{
		Num: num,
	}

	// 如果一开始为空链表
	if head.Next == nil {
		head.Next = newNode
		this.rear = head
		this.rear = this.rear.Next
		return
	}

	// 如果不是空链表
	this.rear.Next = newNode
	this.rear = this.rear.Next

}

// 数据出队列
func (this *Node) Pop(head *Node) (num int, err error) {

	if head.Next == nil {
		return -1, errors.New("queue is empty")
	}

	this.front = head.Next

	// 如果只有一个节点
	if this.front == this.rear {
		head.Next = nil
		return this.front.Num, nil
	}

	// 不止一个节点
	head.Next = this.front.Next
	return this.front.Num, nil

}

// 显示数据队列
func (this *Node) Show(head *Node) {

	fmt.Println("\n队列中的数据如下:")
	
    // 每次都让 temp 指针指向 head 节点的下一个节点
    temp := head.Next
    
    // 如果队列中没有数据
	if temp == nil {
		fmt.Println("queue is empty")
		return
	}
    
    // 循环显示队列中的每个节点
	for {

		fmt.Printf("%d -> ", temp.Num)
        
        // 如果 temp 指针移动到了和尾部指针 rear 重合
        // 说明整个队列已经扫描完毕,退出循环 
		if temp == this.rear {
			break
		}
        
        // 继续扫描队列中的下一个节点
		temp = temp.Next

	}

}

func main() {

	head := &Node{}
	queue := &Node{}

	// 入队列
	queue.Insert(head, 1)
	queue.Insert(head, 2)
	queue.Insert(head, 3)
	queue.Insert(head, 4)
	queue.Insert(head, 5)

	// 显示队列
	queue.Show(head)

	// 出队列
	var num int
	num, _ = queue.Pop(head)
	fmt.Println("\n出队列的数据为:", num)
	num, _ = queue.Pop(head)
	fmt.Println("\n出队列的数据为:", num)

	// 显示队列
	queue.Show(head)

}

执行结果


队列中的数据如下:
1 -> 2 -> 3 -> 4 -> 5 -> 
出队列的数据为: 1

出队列的数据为: 2

队列中的数据如下:
3 -> 4 -> 5 -> 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Unity中使用单向链表制作流程可以按照以下步骤进行: 1. 创建一个表示链表节点的类,可以称为"Node"。这个类包含一个数据成员用于存储节点的数据,以及一个指向下一个节点的引用。 ```csharppublic class Node<T> { public T data; public Node<T> next; } ``` 2. 创建一个表示链表的类,可以称为"LinkedList"。这个类包含一个指向链表头节点的引用。 ```csharppublic class LinkedList<T> { public Node<T> head; } ``` 3. 在"LinkedList"类中添加方法来操作链表,例如添加节点、删除节点、遍历节点等。 ```csharppublic class LinkedList<T> { public Node<T> head; // 添加节点 public void AddNode(T data) { Node<T> newNode = new Node<T>(); newNode.data = data; if (head == null) { head = newNode; } else { Node<T> currentNode = head; while (currentNode.next != null) { currentNode = currentNode.next; } currentNode.next = newNode; } } // 遍历节点 public void TraverseNodes() { Node<T> currentNode = head; while (currentNode != null) { Debug.Log(currentNode.data); currentNode = currentNode.next; } } // 其他操作方法... } ``` 4. 在Unity中使用链表来表示流程。你可以在脚本中创建一个"LinkedList"对象,并添加需要的节点。 ```csharppublic class ProcessManager : MonoBehaviour{ LinkedList<string> processList; void Start() { processList = new LinkedList<string>(); processList.AddNode("Step1"); processList.AddNode("Step2"); processList.AddNode("Step3"); processList.TraverseNodes(); // 遍历节点并输流程步骤 } } ``` 这样,你就可以使用单向链表来制作流程,并在Unity中进行操作和展示了。你可以根据实际需求扩展链表的功能,比如插节点、删除节点等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值