General and Web Programming Fundamentals
  • Introduction
  • Program creation and design
    • Program design
      • Algorithms
      • Pseudocode
    • Programming conventions
    • Writing programs
      • Source code editors
      • Integrated Development Environments
      • Code repositories/Version control
      • Compilers/Interpreters
  • Programming Fundamentals
    • Operators
      • Arithmetic
      • Logical
      • Assignment
    • Constants and Variables
    • Datatypes
      • Primitive Datatypes
        • Character
        • Integer
        • Boolean
        • Floating point
        • Nothing (Null)
      • Composite Datatypes
        • Arrays
        • Strings
        • Classes
        • Structs
      • Literals
    • Data structures
      • Lists
      • Queues
      • Stacks
      • Map/dictionary
      • Trees
      • Graphs
    • Control structures
      • Selection (Conditional)
        • If/Else
        • Ternary
        • Switch
      • Iteration (Loops)
        • For loops
        • While loops
        • Do-While loops
        • For-Each loops
    • Functions
      • Parameters and arguments
      • Lambda expressions
      • Higher Order Functions
    • Space and Time
    • Scope
    • Standard libraries
  • Programming Paradigms
    • Procedural (Imperative) Programming
    • Object-oriented programming
    • Functional Programming
    • Declarative Programming
    • Event Driven programming
  • Programming Languages
    • Short history of programming
    • Low-level programming languages
    • High-level programming languages
  • Web Development
    • What is the web?
      • Web browsers (clients)
      • Webservers (serving web pages)
      • W3C
    • Markup languages
      • HTML
        • HTML Tags
      • Cascading Style Sheets (CSS)
        • CSS Properties
      • XML
      • Markdown
    • Scripting Languages
      • JavaScript
      • TypeScript
    • JSON
    • JavaScript Frameworks
  • Acknowledgements
    • About the author(s)
  • License
Powered by GitBook
On this page

Was this helpful?

Export as PDF
  1. Programming Fundamentals
  2. Data structures

Queues

A queue is a fundamental data structure that follows a particular order in which operations are performed. The order is First In, First Out (FIFO). This means that the first element added to the queue will be the first one to be removed. Queues are used in various programming scenarios where items need to be managed and processed in the order they occur or are received, such as in buffering, resource scheduling, and breadth-first search algorithms.

A queue can be visualized as a line of elements similar to a queue of people waiting in line; the person who gets in line first will be the first one to be served and leave the line. Similarly, in a queue data structure, elements are added (enqueued) at the end of the line and removed (dequeued) from the front.

The basic operations associated with queues include:

  • Enqueue: Add an element to the end of the queue.

  • Dequeue: Remove an element from the front of the queue.

  • Peek/Front: Get the element at the front of the queue without removing it, allowing you to see what's next to be processed.

  • IsEmpty: Check if the queue is empty.

  • IsFull: Check if the queue is full (primarily in the context of a fixed-size queue implementation).

Queues can be implemented using various underlying data structures, such as linked lists, arrays, or even stacks. The choice of implementation depends on the requirements of the application, including complexity, speed of operations, and memory usage.

In addition to the simple linear queue described above, there are other variations of queues, including:

  • Circular Queue: A more efficient implementation where the end of the queue wraps around to the front when space is available, optimizing the use of space.

  • Priority Queue: Elements are enqueued with a priority, and the element with the highest priority is dequeued before elements with lower priority, regardless of their order in the queue.

  • Double-Ended Queue (Deque): Allows insertion and deletion of elements from both the front and the back of the queue.

Queues are a versatile data structure and are widely used in programming for managing data in scenarios where order matters.

Certainly! Below is an example of how to implement a basic queue in C#. This example includes a custom Queue class with methods to enqueue (add) an item to the back of the queue, dequeue (remove) the front item from the queue, peek at the front item, check if the queue is empty, and get the current size of the queue.

using System;
using System.Collections.Generic; // Required for using the List<T> class

public class Queue<T>
{
    private List<T> elements = new List<T>();

    // Method to add an item to the back of the queue
    public void Enqueue(T item)
    {
        elements.Add(item);
    }

    // Method to remove and return the front item of the queue
    public T Dequeue()
    {
        if (IsEmpty())
        {
            throw new InvalidOperationException("Queue is empty");
        }

        var item = elements[0];
        elements.RemoveAt(0);
        return item;
    }

    // Method to return the front item of the queue without removing it
    public T Peek()
    {
        if (IsEmpty())
        {
            throw new InvalidOperationException("Queue is empty");
        }

        return elements[0];
    }

    // Method to check if the queue is empty
    public bool IsEmpty()
    {
        return elements.Count == 0;
    }

    // Method to get the current size of the queue
    public int Size()
    {
        return elements.Count;
    }
}

class Program
{
    static void Main()
    {
        var queue = new Queue<string>();

        // Enqueue items into the queue
        queue.Enqueue("Apple");
        queue.Enqueue("Banana");
        queue.Enqueue("Cherry");

        // Peek at the front item
        Console.WriteLine("Front item: " + queue.Peek());

        // Dequeue an item from the queue
        Console.WriteLine("Dequeued item: " + queue.Dequeue());

        // Check the queue's current size
        Console.WriteLine("Current queue size: " + queue.Size());
    }
}

In this C# example, Queue<T> is a generic class, allowing it to store elements of any type (denoted by <T>). This makes the queue flexible and able to hold various data types, such as integers, strings, or objects. The queue uses a private List<T> to store its elements, where the first element in the list represents the front of the queue and the last element represents the back. This implementation provides methods for standard queue operations, such as enqueuing and dequeuing elements, as well as utility methods like checking if the queue is empty and getting the queue's size.

This example demonstrates how to define and manipulate a queue using a custom class in C#, showcasing basic queue functionality.

PreviousListsNextStacks

Last updated 1 year ago

Was this helpful?