先進先出演算法(英語:first in, first out,簡稱 FIFO)是一種計算機科學的排程演算法。它描述了一個佇列所使用的先到先得服務方式:先進入佇列的工作將先被完成,之後進來的則必須稍候。
範例
一個C++語言的範例
#include <iostream>
#include <stdexcept>
template <typename T>
class FIFO
{
private:
struct Node {
T value;
Node *next;
Node(T _value) : value(_value), next(NULL) {}
};
Node *front;
Node *back;
public:
FIFO() : front(NULL), back(NULL) {}
~FIFO() {
while (front != NULL)
dequeue();
}
void enqueue(T _value) {
Node *newNode = new Node(_value);
if (front == NULL)
front = newNode;
else
back->next = newNode;
back = newNode;
}
T dequeue() {
if (front == NULL)
throw std::underflow_error("Nothing to dequeue");
Node *temp = front;
T result = front->value;
front = front->next;
delete temp;
return result;
}
};
Wikiwand in your browser!
Seamless Wikipedia browsing. On steroids.
Every time you click a link to Wikipedia, Wiktionary or Wikiquote in your browser's search results, it will show the modern Wikiwand interface.
Wikiwand extension is a five stars, simple, with minimum permission required to keep your browsing private, safe and transparent.