Logo

Programming-Idioms

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

Be concise.

Be useful.

All contributions dictatorially edited by webmasters to match personal tastes.

Please do not paste any copyright violating material.

Please try to avoid dependencies to third-party libraries and frameworks.

Other implementations
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();
import java.util.ArrayDeque;
import java.util.Deque;
Deque<T> q = new ArrayDeque<>();
q.offer(x);
q.offer(y);
T z = q.poll();
import java.util.ArrayDeque;
import java.util.Deque;
Deque<T> q = new ArrayDeque<>();
q.addLast(x);
q.addLast(y);
T z = q.removeFirst();
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
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());