Swift 实现队列数据结构的程序
队列是一种遵循 FIFO(先进先出)原则的数据结构。队列的两端都是开放的,因此我们可以从一端(称为后端或尾部)添加新元素,此操作称为入队;从另一端(称为前端或头部)移除元素,此操作称为出队。
虽然 Swift 本身并不支持任何内置的队列数据结构,但我们仍然可以通过多种方式实现队列,例如链表、结构体、类、数组等。您可以根据需要选择任何一种方法来实现队列数据结构。
队列支持以下操作:
入队 (Enqueue) − 用于从队列的后端或尾部添加元素。在 Swift 中,我们通过 `append()` 方法实现入队操作。
语法
func Enqueue(_ items: T) {
item.append(items)
}
这里,`append()` 函数向队列中添加一个新元素。
出队 (Dequeue) − 用于从队列的头部或前端移除元素。在 Swift 中,我们通过 `removeFirst()` 方法实现出队操作。
语法
func Dequeue() -> T? {
if item.isEmpty{
return nil
} else {
return item.removeFirst()
}
}
这里,`removeFirst()` 函数用于从队列中移除元素。
顶部元素 (Top) − 用于获取队列的顶部元素。在 Swift 中,我们使用 `first` 属性实现顶部元素操作。
语法
func Top() -> T? {
return item.first
}
这里,`first` 属性用于查找栈的顶部元素。
是否为空 (isEmpty) − 用于检查队列是否为空。在 Swift 中,我们使用 `isEmpty` 属性实现是否为空操作。
语法
func IsEmpty() -> Bool {
return item.isEmpty
}
这里,`isEmpty` 属性用于检查队列是否为空。如果返回 true,则表示队列为空;否则为 false。
大小 (Size) − 用于检查队列的大小。在 Swift 中,我们使用 `count` 属性实现大小操作。
语法
func Size()->Int{
return item.count
}
这里,`count` 属性用于查找队列中存在的元素总数。
注意 − 使用数组实现队列并不是最有效的方法,因为频繁的入队和出队操作会影响效率。
示例 1
在下面的 Swift 程序中,我们将使用结构体实现队列数据结构。在这里,我们定义了一个包含入队、出队、顶部元素、大小和是否为空方法的队列结构体。然后,我们创建一个队列结构体的实例,使用 `Enqueue()` 函数向队列中添加一些元素,然后使用 `Dequeue()` 函数按 FIFO 顺序移除它们。最后,显示输出。
import Foundation
import Glibc
// Implementing stack using structure
struct queue<T> {
private var item = [T]()
mutating func Enqueue(_ items: T) {
item.append(items)
}
mutating func Dequeue() -> T? {
if item.isEmpty{
return nil
} else {
return item.removeFirst()
}
}
func IsEmpty() -> Bool {
return item.isEmpty
}
func Size()->Int{
return item.count
}
func Top() -> T? {
return item.first
}
}
var myQueue = queue<Int>()
myQueue.Enqueue(34)
myQueue.Enqueue(42)
myQueue.Enqueue(1)
myQueue.Enqueue(89)
myQueue.Enqueue(32)
myQueue.Enqueue(8)
print("Size of the queue is:", myQueue.Size())
print("Top Element of the queue is:", myQueue.Top()!)
print("Dequeue Elements are:")
while !myQueue.IsEmpty() {
if let c = myQueue.Dequeue()
{
print(c)
}
}
输出
Size of the queue is: 6 Top Element of the queue is: 34 Dequeue Elements are: 34 42 1 89 32 8
示例 2
在下面的 Swift 程序中,我们将使用类实现队列数据结构。在这里,我们定义了一个包含入队、出队、顶部元素、大小和是否为空方法的队列类。然后,我们创建一个队列类的实例,使用 `Enqueue()` 函数向队列中添加一些元素,然后使用 `Dequeue()` 函数按 FIFO 顺序移除它们。最后,显示输出。
import Foundation
import Glibc
// Implementing stack using class
class queue<T> {
private var item = [T]()
func Enqueue(_ items: T) {
item.append(items)
}
func Dequeue() -> T? {
if item.isEmpty{
return nil
} else {
return item.removeFirst()
}
}
func IsEmpty() -> Bool {
return item.isEmpty
}
func Size()->Int{
return item.count
}
func Top() -> T? {
return item.first
}
}
var myQueue = queue<Character>()
myQueue.Enqueue("Q")
myQueue.Enqueue("U")
myQueue.Enqueue("E")
myQueue.Enqueue("U")
myQueue.Enqueue("E")
print("Size of the queue is:", myQueue.Size())
print("Top Element of the queue is:", myQueue.Top()!)
print("Dequeue Elements are:")
while !myQueue.IsEmpty() {
if let c = myQueue.Dequeue()
{
print(c)
}
}
输出
Size of the queue is: 5 Top Element of the queue is: Q Dequeue Elements are: Q U E U E
结论
这就是我们如何实现队列数据结构的方法。队列数据结构可用于调度任务、执行广度优先搜索 (BFS) 算法、异步操作等。队列数据结构的灵活性和简单性使其更适合于有效地管理数据和执行某些特定算法。
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C 语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP