Queues

A queue is a fundamental data structure in programming that follows the First In, First Out (FIFO) principle, meaning that the first element added to the queue is the first one to be removed. It supports two primary operations: enqueue, which adds an element to the back of the queue, and dequeue, which removes the front element from the queue. Queues are commonly used in programming for tasks such as job scheduling, process management, and implementing breadth-first search algorithms.

Select Languages

Examples

C

// no built in implementation

C#

using System.Collections.Generic;

Queue<string> cars = 
  new Queue<string>();

cars.Enqueue("Mazda");
cars.Enqueue("Toyota");
cars.Enqueue("Honda");
Console.WriteLine(
  cars.Contains("Mazda") + "\n"
);

cars.Dequeue();
Console.WriteLine(
  cars.Contains("Mazda")
);

C++

#include <iostream>
#include <queue>
using namespace std;

queue<string> cars;
cars.push("Mazda");
cars.push("Toyota");
cars.push("Honda");

cout << cars.front() << endl;

cars.pop();
cout << cars.front() << endl;

Go

import "fmt"

cars := []string{}
cars = append(cars, "Mazda")
cars = append(cars, "Toyota")
cars = append(cars, "Honda")

fmt.Println(cars[0])
cars = cars[1:]
fmt.Println(cars[0])

Java

import java.util.*;

ArrayList<String> cars = new ArrayList<>();
cars.add("Mazda");
cars.add("Toyota");
cars.add("Honda");

System.out.println(cars.get(0));

cars.remove(0);
System.out.println(cars.get(0));

JavaScript

const cars = [];

cars.push('Mazda');
cars.push('Toyota');
cars.push('Honda');

console.log(cars[0);

cars.shift();
console.log(cars[0]);

Kotlin

import java.util.Queue;
import java.util.LinkedList;

val cars: Queue<String> = 
  LinkedList<String>();
  
cars.add("Mazda");
cars.add("Toyota");
cars.add("Honda");
println(cars.peek());

cars.remove();
println(cars.peek());

MatLab

cars = [];

cars = [cars, "Mazda"];
cars = [cars, "Toyota"];
cars = [cars, "Honda"];

fprintf(cars(1));

cars(1)=[];
fprintf(cars(1));

PHP

$cars = array();
array_push($cars, "Mazda");
array_push($cars, "Toyota");
array_push($cars, "Honda");

echo $cars[0] . "\n";
array_shift($cars);
echo $cars[0];

Python

cars = []

cars.append('Mazda')
cars.append('Toyota')
cars.append('Honda')

print(cars[0])

cars.pop(0)
print(cars[0])

R

cars <- list()
cars <- append(cars, "Mazda")
cars <- append(cars, "Honda")
cars <- append(cars, "Toyota")

print(cars[1])

cars <- cars[-1]
print(cars[1])

Ruby

cars = []
cars << "Mazda"
cars << "Toyota"
cars << "Honda"

puts cars[0]

cars.shift()
puts cars[0]

Rust

use std::collections::VecDeque;

let mut cars = VecDeque::new();
cars.push_back("Mazda".to_string());
cars.push_back("Toyota".to_string());
cars.push_back("Honda".to_string());

println!("{}", cars[0]);

cars.remove(0);
println!("{}", cars[0]);

Scala

import scala.collection.mutable.Queue;

var cars = Queue[String]();

cars.enqueue("Mazda");
cars.enqueue("Toyota");
cars.enqueue("Honda");

println(cars.front);

cars.dequeue();
println(cars.front);

Swift

var cars:Array<String> = [];
cars.append("Mazda");
cars.append("Toyota");
cars.append("Honda");

print(cars[0]);

cars.removeFirst();
print(cars[0]);

TypeScript

const cars:string[] = [];

cars.push('Mazda');
cars.push('Toyota');
cars.push('Honda');

console.log(cars[0);

cars.shift();
console.log(cars[0]);

Copyright 2025. All Rights Reserved. IronCodeMan.