Stack is an ordered list where insertion happens at the rear end and deletion happens at the front end of the queue. Hence it is known as First in First out(FIFO)

Insertion and deletion in stacks are called ENQUEUE and DEQUEUE respectively. A Peek operation returns the front most element in a queue.

Data_Queue.png

SRC : commons.wikimedia.org/w/index.php?curid=758..

A Queue can only be accessed only from the front end of the queue, so random access is not possible as in an array.

Queue Implementation using LinkedList

public class Queue<T>
{
    LinkedList<T> _list = new LinkedList<T>();


    public void Enqueue(T item)
    {
        _list.AddLast(new LinkedListNode<T>(item));
    }

    public T Dequeue()
    {
        if (_list.Count is 0) throw new InvalidOperationException("Queue is Empty");

        var value = _list.First.Value;
        _list.RemoveFirst();
        return value;

    }
}

Few application of Queues are

  • Queues are used to manage multiple requests for a single shared resource.
  • A playlist in media player is a queue
  • Queues are used in networking, during the transmission of data from the source machine to the destination.