Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!

Idiom #325 Create a queue

Create a new queue q, then enqueue two elements x and y, then dequeue an element into the variable z.

use std::collections::VecDeque;
let mut q = VecDeque::new();
q.push_back(x);
q.push_back(x);
let z = q.pop_front(); 
println!("1st item ~> {}",z.unwrap());
type Queue[T any] struct {
	items []T
}

func (q *Queue[T]) Enqueue(t T) {
	q.items = append(q.items, t)
}

func (q *Queue[T]) Dequeue() T {
	t := q.items[0]
	var zero T
	q.items[0] = zero
	q.items = q.items[1:]
	return t
}

q := new(Queue[string])
q.Enqueue(x)
q.Enqueue(y)
z := q.Dequeue()
let q = [];
q.push(x);
q.push(y);
let z = q.shift();
dgueue
type
  TQ = specialize TQueue<TSomeType>;
var
  x, y, z: TSomeType;
  Q: TQ;

begin
  Q := TQ.Create;
  Q.Push(x);
  Q.Push(y);
  z := Q.Front;
  Q.Pop;
  Q.Free;
end.
use strict;
my @q;
push @q, $x;
push @q, $y;
my $z = shift @q;
import queue
q = queue.Queue()
q.put(x)
q.put(y)
z = q.get()
q = Queue.new
q.enq(x)
q.enq(y)
z = q.deq

New implementation...
< >
programming-idioms.org