Table of content

Queue in Rust

A queue is a simple data structure that allows elements to be inserted from one end, called the rear(also called tail), and deleted from the other end, called the front (also called head).


In order to understand queue in rust, consider a group of people climbing a ladder, the person to climb first will be the first one to get down, which is similar to a queue of line, where the first person to get in the queue is the first one to get out of it.

So, a queue is a linear data structure, A very important point is that a queue deletes only the oldest added element.

The most recently added item in the queue must wait at the end of the collection. The item that has been in the collection the longest is at the front. This ordering principle is called FIFO, first-in first-out. It is also known as first-come first-served.

Queues are a very prevalent model for data flow in real life. Consider an office with 30 computers networked with a single printer. When somebody wants to print, their print tasks "get in line" with all the other printing tasks that are waiting. The first task is the next to be completed. If you are last in line, you must wait for all the other tasks to print ahead of you.

Methods in Queue :

It is an abstract data type, which means that we can create our own function in the Queue, but mostly the queues will contain below functions.

  • enqueue() : add (store) an item to the queue.
  • dequeue() : remove (access) an item from the queue.
  • peek() : Gets the element at the front of the queue without removing it
  • size : Get number of elements in the Queue
  • isEmpty() : Checks if the queue is empty
  • You can add more functions if you need as the Queue is just an abstract/concept

Usage of Queues :

  • When your ticket is in the waiting list. (Mostly railways and buses)
  • CPU task scheduling
  • When you call a service center and told to wait(when all support people are busy), your call enters a queue. etc [end of thinking capacity]
  • A queue of airplanes waiting for landing instructions
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions