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

📅  最后修改于: 2021-06-28 17:41:26             🧑  作者: Mango

FIFO先入先出的缩写。这是一种用于处理数据结构的方法,其中首先处理第一个元素,最后处理最新的元素

现实生活中的例子:

在此示例中,应考虑以下事项:

  • 有一个售票处,人们可以去买票,走票。
  • 人们进入排队(队列)以有组织的方式到达售票处。
  • 首先进入队列的人,将首先获得票证并离开队列。
  • 接下来进入队列的人将在他面前的人之后获得票证
  • 这样,最后进入队列的人将使门票持续

  • 因此,第一个进入队列的人首先获得票证,最后一个进入队列的人最后获得票证。

这称为先进先出方法或FIFO。

FIFO在哪里使用:

  1. 数据结构

    某些数据结构(例如Queue和其他Queue变体)使用FIFO方法来处理数据。

  2. 磁盘调度

    磁盘控制器可以使用FIFO作为磁盘调度算法来确定服务磁盘I / O请求的顺序。

  3. 通讯和网络

    计算机网络中使用的通信网络桥,交换机和路由器使用FIFO来保存到达其下一个目的地的数据包。

FIFO的程序示例

程序1:排队

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


输出:

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