The queue is a fundamental data container in which are the stored  elements removed (polled) in the same order as they were inserted into the structure (enqueued). This principle is called FIFO – first in, first out. The opposite approach LIFO – last in, fist out – is used in the stack data structure.

A special type of a queue is a priority queue, in which are the elements polled in the order defined by their priority.

Typical operations

Queue
Queue
  • addLast (enqueue) – Put the element into the queue.
  • deleteFirst (poll, dequeue) – Get and remove the first element (head) of the queue.
  • getFirst (peek) – Get the first element of the queue.
  • isEmpty – Query whether the queue does not contain any element.
  • size – Return the number of contained elements.

Usage

In computer science the queue is widely used, for example in these applications:

  • Synchronization primitive in multi-threaded applications
  • Unix pipe operator used for interprocess communication
  • Circular buffer – buffer for data streams
  • Heapsort – a sorting algorithm based on a priority queue

Code

 

/**
 * Queue - implemented as a linked list
 */
public class Queue {
    private Node first;
    private Node last;
    private int size;
    public Queue() {
        this.size = 0;
    }

    /**
     * Insert the element at the end of the queue
     * @param i element
     */
    public void addLast(int i) {
        Node n = new Node(i);
        if(getSize() == 0) {
            this.first = n;
            this.last = n;
        } else {
            this.last.next = n;
            this.last = n;
        }
        size++;     
    }

    /**
     * Remove the first element
     * @return element
     */
    public int deteteFirst() {
        if(getSize() == 0) throw new IllegalStateException("Queue is empty");
        int value = first.value;
        first = first.next;
        size--;
        return value;
    }
    /**
     * Return the first element
     * @return element
     */
    public int getFirst() {
        if(getSize() == 0) throw new IllegalStateException("Queue is empty");
        return first.value;
    }
    /**
     * Get number of contained elements
     * @return lenght of the queue
     */
    public int getSize() {
        return size;
    }
    
    /**
     * Query, whether is the queue empty
     * @return true if the queue is empty, false otherwise
     */
    public boolean isEmpty() {
        return this.size == 0;
    }

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        Node curr = first;
        for(int i = 0;  i < this.size; i++) {
            builder.append(curr.value).append(" ");
            curr = curr.next;
        }
        return builder.toString();
    }  

    /**
     * Inner representation of the element (node of the linked list)
     */
    private class Node {
        private int value;
        private Node next;
        private Node(int value) {
            this.value = value;
        }
    }    
}


Sex v Praze

Priváty, escort i erotické masáže na jednom místě

Hrajte nejlepší hry jako je GoodGame Empire.
" data-numposts="5" width="100%" data-version="v2.3">





Zajímavé články: Jak najít práci snů? Zvolte kariéru v IT!, Češi mají rádi hrací automaty online, Jak funguje algoritmické obchodování Casino, Online výuka Algoritmus a online marketing mají svá pravidla, Automaty, Matematický vliv, Ratings, Jak fungují algoritmy hazardních her online: více znalostí, více peněz, SYPWAI - nástroj pro vědecký vývoj, Vynikají na globálním trhu: Nejlepší vývojáři softwaru pro online výherní automaty, Jak si vybrat nejlepší české online casino, Proč byste měli hrát online casino VPN revoluce, Kde najdeme algoritmy v každodenním životě?, Čeká vás pracovní pohovor mimo město? Podívejte se, jak dokonale zvládnout včasný příchod, iPhone 18 Pro a skládací iPhone: co Apple ukáže 9. září, 5 úžasných technologií ze světa hazardních her, Mirror and access to Mostbet, Svou kancelář můžete mít stále po ruce, Jaké výhody má digitalizovaná firma oproti off-line konkurenci?, Jaký systém vybrat pro snadné řízení výroby?, Nahradí umělá inteligence ajťáky?, Důvody, proč používat SnapTik ke stahování videí TikTok, Dokonalý den na pláži: Co si vzít s sebou, aby byl výlet zábavný a bezpečný?, Jak přežít dlouhý let?, Go pay GoodGame Empire, Blockchain, Rozhovor, Umělá inteligence, Ochranná známka pre softvér: Prečo ju registrovať?, Role kryptografických algoritmů v zabezpečení online kasin, Jaké jsou náklady na nákup 3D tiskárny?, Jak algoritmy vylepšují online zážitky v roce 2025, Epilace laserem a péče o pokožku před a po ní, Byty k pronájmu Sokolov - výhody a rizika pronájmu bytu bez realitky, Filmy a seriály plné hádanek: kryptografie jako hlavní téma, Kdy obnovit data z disku běžně dostupným softwarem a kdy už se obrátit na profesionály?, Jak se zbavit jizev po akné? Co z domácí péče opravdu funguje? Na co všechno se zaměřit při výběru internetového připojení pro firmy?, Promo kód casino: mezi matematikou bonusů a českou realitou, Pet Pinco >Jak technologie CoreXY zlepšuje rychlost a přesnost 3D tisku?


  • Home
  • Data structures
  • Doporučujeme

    Internet pro vaši firmu na míru

    https://www.algoritmy.net