📜  编程中的 FIFO(先进先出)方法

📅  最后修改于: 2021-09-08 12:48:54             🧑  作者: Mango

FIFO先入先出的缩写。它是一种处理数据结构的方法,首先处理第一个元素,最后处理最新的元素
现实生活中的例子:

在本例中,需要考虑以下事项:

  • 有售票柜台,人来人往,取票就走。
  • 人们排队(队列)以有组织的方式到达售票柜台。
  • 最先进入队列的人,会先拿到票离开队列。
  • 下一个进入队列的人会在他前面的人之后拿到票
  • 这样,最后进入队列的人将最后获得门票
  • 因此,第一个进入队列的人首先获得票,最后进入队列的人最后获得票。

这称为先进先出方法或 FIFO。
FIFO在哪里使用:

  1. 数据结构
    某些数据结构(如 Queue 和 Queue 的其他变体)使用 FIFO 方法处理数据。
  2. 磁盘调度
    磁盘控制器可以使用 FIFO 作为磁盘调度算法来确定为磁盘 I/O 请求提供服务的顺序。
  3. 通信和网络
    计算机网络中使用的通信网络桥接器、交换机和路由器使用 FIFO 来保存通往下一个目的地的数据包。

FIFO 的程序示例
方案一:队列

C++
// C++ program to demonstrate
// working of FIFO
// using Queue interface in C++
 
#include
using namespace std;
 
// print the elements of queue
void print_queue(queue q)
{
    while (!q.empty())
    {
        cout << q.front() << " ";
        q.pop();
    }
    cout << endl;
}
 
// Driver code
int main()
{
    queue q ;
 
    // Adds elements {0, 1, 2, 3, 4} to queue
    for (int i = 0; i < 5; i++)
        q.push(i);
 
    // Display contents of the queue.
    cout << "Elements of queue-";
         
    print_queue(q);
 
    // To remove the head of queue.
    // In this the oldest element '0' will be removed
    int removedele = q.front();
    q.pop();
    cout << "removed element-" << removedele << endl;
 
    print_queue(q);
 
    // To view the head of queue
    int head = q.front();
    cout << "head of queue-" << head << endl;
 
    // Rest all methods of collection interface,
    // Like size and contains can be used with this
    // implementation.
    int size = q.size();
    cout << "Size of queue-" << size;
         
    return 0;
}
 
// This code is contributed by Arnab Kundu


Java
// Java program to demonstrate
// working of FIFO
// using Queue interface in Java
 
import java.util.LinkedList;
import java.util.Queue;
 
public class QueueExample {
    public static void main(String[] args)
    {
        Queue q = new LinkedList<>();
 
        // Adds elements {0, 1, 2, 3, 4} to queue
        for (int i = 0; i < 5; i++)
            q.add(i);
 
        // Display contents of the queue.
        System.out.println("Elements of queue-" + q);
 
        // To remove the head of queue.
        // In this the oldest element '0' will be removed
        int removedele = q.remove();
        System.out.println("removed element-" + removedele);
 
        System.out.println(q);
 
        // To view the head of queue
        int head = q.peek();
        System.out.println("head of queue-" + head);
 
        // Rest all methods of collection interface,
        // Like size and contains can be used with this
        // implementation.
        int size = q.size();
        System.out.println("Size of queue-" + size);
    }
}


C#
// C# program to demonstrate
// working of FIFO
using System;
using System.Collections.Generic;
 
public class QueueExample
{
    public static void Main(String[] args)
    {
        Queue q = new Queue();
 
        // Adds elements {0, 1, 2, 3, 4} to queue
        for (int i = 0; i < 5; i++)
            q.Enqueue(i);
 
        // Display contents of the queue.
        Console.Write("Elements of queue-");
        foreach(int s in q)
                Console.Write(s + " ");
 
        // To remove the head of queue.
        // In this the oldest element '0' will be removed
        int removedele = q.Dequeue();
        Console.Write("\nremoved element-" + removedele + "\n");
        foreach(int s in q)
                Console.Write(s + " ");
 
        // To view the head of queue
        int head = q.Peek();
        Console.Write("\nhead of queue-" + head);
 
        // Rest all methods of collection interface,
        // Like size and contains can be used with this
        // implementation.
        int size = q.Count;
        Console.WriteLine("\nSize of queue-" + size);
    }
}
 
// This code has been contributed by 29AjayKumar


Javascript


输出:

Elements of queue-[0, 1, 2, 3, 4]
removed element-0
[1, 2, 3, 4]
head of queue-1
Size of queue-4

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程