Queue (abstract data type)

Queue
Representation of a FIFO (first in, first out) queue
Time complexity in big O notation
Operation Average Worst case
Search O(n) O(n)
Insert O(1) O(1)
Delete O(1) O(1)
Space complexity
Space O(n) O(n)

In computer science, a queue is a collection of entities that are maintained in a sequence and can be modified by the addition of entities at one end of the sequence and the removal of entities from the other end of the sequence. By convention, the end of the sequence at which elements are added is called the back, tail, or rear of the queue, and the end at which elements are removed is called the head or front of the queue, analogously to the words used when people line up to wait for goods or services.

The operation of adding an element to the rear of the queue is known as enqueue, and the operation of removing an element from the front is known as dequeue. Other operations may also be allowed, often including a peek or front operation that returns the value of the next element to be dequeued without dequeuing it.

The operations of a queue make it a first-in-first-out (FIFO) data structure. In a FIFO data structure, the first element added to the queue will be the first one to be removed. This is equivalent to the requirement that once a new element is added, all elements that were added before have to be removed before the new element can be removed. A queue is an example of a linear data structure, or more abstractly a sequential collection. Queues are common in computer programs, where they are implemented as data structures coupled with access routines, as an abstract data structure or in object-oriented languages as classes.

A queue has two ends, the top, which is the only position at which the push operation may occur, and the bottom, which is the only position at which the pop operation may occur. A queue may be implemented as circular buffers and linked lists, or by using both the stack pointer and the base pointer.

Queues provide services in computer science, transport, and operations research where various entities such as data, objects, persons, or events are stored and held to be processed later. In these contexts, the queue performs the function of a buffer. Another usage of queues is in the implementation of breadth-first search.

Queue implementation

Theoretically, one characteristic of a queue is that it does not have a specific capacity. Regardless of how many elements are already contained, a new element can always be added. It can also be empty, at which point removing an element will be impossible until a new element has been added again.

Fixed-length arrays are limited in capacity, but it is not true that items need to be copied towards the head of the queue. The simple trick of turning the array into a closed circle and letting the head and tail drift around endlessly in that circle makes it unnecessary to ever move items stored in the array. If n is the size of the array, then computing indices modulo n will turn the array into a circle. This is still the conceptually simplest way to construct a queue in a high-level language, but it does admittedly slow things down a little, because the array indices must be compared to zero and the array size, which is comparable to the time taken to check whether an array index is out of bounds, which some languages do, but this will certainly be the method of choice for a quick and dirty implementation, or for any high-level language that does not have pointer syntax. The array size must be declared ahead of time, but some implementations simply double the declared array size when overflow occurs. Most modern languages with objects or pointers can implement or come with libraries for dynamic lists. Such data structures may have not specified a fixed capacity limit besides memory constraints. Queue overflow results from trying to add an element onto a full queue and queue underflow happens when trying to remove an element from an empty queue.

A bounded queue is a queue limited to a fixed number of items.[1]

There are several efficient implementations of FIFO queues. An efficient implementation is one that can perform the operations—en-queuing and de-queuing—in O(1) time.

  • Linked list
    • A doubly linked list has O(1) insertion and deletion at both ends, so it is a natural choice for queues.
    • A regular singly linked list only has efficient insertion and deletion at one end. However, a small modification—keeping a pointer to the last node in addition to the first one—will enable it to implement an efficient queue.
  • A deque implemented using a modified dynamic array

Queues and programming languages

Queues may be implemented as a separate data type, or maybe considered a special case of a double-ended queue (deque) and not implemented separately. For example, Perl and Ruby allow pushing and popping an array from both ends, so one can use push and shift functions to enqueue and dequeue a list (or, in reverse, one can use unshift and pop),[2] although in some cases these operations are not efficient.

C++'s Standard Template Library provides a "queue" templated class which is restricted to only push/pop operations. Since J2SE5.0, Java's library contains a Queue interface that specifies queue operations; implementing classes include LinkedList and (since J2SE 1.6) ArrayDeque. PHP has an SplQueue class and third party libraries like beanstalk'd and Gearman.

UML queue class.svg

Example

A simple queue implemented in JavaScript:

class Queue {
    constructor() {
        this.items = [];
    }

    enqueue(element) {
        this.items.push(element);
    }

    dequeue() {
        return this.items.shift();
    }
}

Purely functional implementation

Queues can also be implemented as a purely functional data structure.[3] There are two implementations. The first one only achieves per operation on average. That is, the amortized time is , but individual operations can take where n is the number of elements in the queue. The second implementation is called a real-time queue[4] and it allows the queue to be persistent with operations in O(1) worst-case time. It is a more complex implementation and requires lazy lists with memoization.

Amortized queue

This queue's data is stored in two singly-linked lists named and . The list holds the front part of the queue. The list holds the remaining elements (a.k.a., the rear of the queue) in reverse order. It is easy to insert into the front of the queue by adding a node at the head of . And, if is not empty, it is easy to remove from the end of the queue by removing the node at the head of . When is empty, the list is reversed and assigned to and then the head of is removed.

The insert ("enqueue") always takes time. The removal ("dequeue") takes when the list is not empty. When is empty, the reverse takes where is the number of elements in . But, we can say it is amortized time, because every element in had to be inserted and we can assign a constant cost for each element in the reverse to when it was inserted.

Real-time queue

The real-time queue achieves time for all operations, without amortization. This discussion will be technical, so recall that, for a list, denotes its length, that NIL represents an empty list and represents the list whose head is h and whose tail is t.

The data structure used to implement our queues consists of three singly-linked lists where f is the front of the queue and r is the rear of the queue in reverse order. The invariant of the structure is that s is the rear of f without its first elements, that is . The tail of the queue is then almost and inserting an element x to is almost . It is said almost, because in both of those results, . An auxiliary function must then be called for the invariant to be satisfied. Two cases must be considered, depending on whether is the empty list, in which case , or not. The formal definition is and where is f followed by r reversed.

Let us call the function which returns f followed by r reversed. Let us furthermore assume that , since it is the case when this function is called. More precisely, we define a lazy function which takes as input three lists such that , and return the concatenation of f, of r reversed and of a. Then . The inductive definition of rotate is and . Its running time is , but, since lazy evaluation is used, the computation is delayed until the results are forced by the computation.

The list s in the data structure has two purposes. This list serves as a counter for , indeed, if and only if s is the empty list. This counter allows us to ensure that the rear is never longer than the front list. Furthermore, using s, which is a tail of f, forces the computation of a part of the (lazy) list f during each tail and insert operation. Therefore, when , the list f is totally forced. If it was not the case, the internal representation of f could be some append of append of... of append, and forcing would not be a constant time operation anymore.

See also

References

  1. ^ "Queue (Java Platform SE 7)". Docs.oracle.com. 2014-03-26. Retrieved 2014-05-22.
  2. ^ "Array (Ruby 3.1)". 2021-12-25. Retrieved 2022-05-11.
  3. ^ Okasaki, Chris. "Purely Functional Data Structures" (PDF).
  4. ^ Hood, Robert; Melville, Robert (November 1981). "Real-time queue operations in pure Lisp". Information Processing Letters. 13 (2): 50–54. doi:10.1016/0020-0190(81)90030-2. hdl:1813/6273.

General references

Further reading

Read other articles:

هذه المقالة يتيمة إذ تصل إليها مقالات أخرى قليلة جدًا. فضلًا، ساعد بإضافة وصلة إليها في مقالات متعلقة بها. (أبريل 2019) سلطان محمد (بالفارسية: سلطان محمد)‏  معلومات شخصية الميلاد العقد 1470  تبريز  الوفاة سنة 1555  تبريز  مواطنة الدولة الصفوية  الحياة العملية المهنة...

 

Regering-De Broqueville V Regeringsleider Charles de Broqueville Coalitie ​ Katholieke Unie ​ Liberale Partij Zetels Kamer 104 van 187 (27 november 1932) Premier Charles de Broqueville Aantreden 12 juni 1934 Ontslagnemend 20 november 1934 Einddatum 20 november 1934 Voorganger De Broqueville IV Opvolger Theunis IV Portaal    België De regering-De Broqueville V (12 juni - 20 november 1934) was een Belgische regering. Het was een coalitie tussen de Katholieke Unie (80 zete...

 

يفتقر محتوى هذه المقالة إلى الاستشهاد بمصادر. فضلاً، ساهم في تطوير هذه المقالة من خلال إضافة مصادر موثوق بها. أي معلومات غير موثقة يمكن التشكيك بها وإزالتها. (نوفمبر 2019) الدوري الإيطالي الدرجة الثانية 1967–68 تفاصيل الموسم الدوري الإيطالي الدرجة الثانية  النسخة 36  البلد إ

Illustration von Arthur Rackham, 1917 Die drei Feldscherer ist ein Märchen (ATU 660). Es steht in den Kinder- und Hausmärchen der Brüder Grimm an Stelle 118 (KHM 118). Inhaltsverzeichnis 1 Inhalt 2 Herkunft 3 Literatur 4 Weblinks Inhalt Drei Feldscherer (Ärzte) auf der Durchreise wollen ihrem Wirt zeigen, dass sie sich Hand, Herz und Auge herausschneiden und wieder einsetzen können. Ein Mädchen soll die Teile über Nacht verwahren, doch die Katze klaut sie, als sie wegen ihres Liebhaber...

 

Artikel ini tidak memiliki referensi atau sumber tepercaya sehingga isinya tidak bisa dipastikan. Tolong bantu perbaiki artikel ini dengan menambahkan referensi yang layak. Tulisan tanpa sumber dapat dipertanyakan dan dihapus sewaktu-waktu.Cari sumber: I Can (not) Hear: Perjalanan Seorang Anak Tuna Rungu Menuju Dunia Mendengar – berita · surat kabar · buku · cendekiawan · JSTOR I Can (not) Hear: Perjalanan Seorang Anak Tuna Rungu Menuju Dunia Mendengar...

 

NFL lists Quarterbacks Career passing touchdowns leaders Career passing yards leaders Career passer rating leaders Career completions/attempts Career wins Playoff records Annual passing touchdowns leaders Annual passing yards leaders Annual passer rating leaders Annual completion percentage leaders 5,000-yard seasons Consecutive starts Consecutive games with TD pass Starting quarterbacks by team: BUFMIANENYJ BALCINCLEPIT HOUINDJAXTEN DENKCLVLAC DALNYGPHIWAS CHIDETGBMIN ATLCARNOTB ARILARSFSEA ...

2014 South Korean drama film Ode to My FatherTheatrical release posterHangul국제시장Hanja國際市場Literal meaningGukje MarketRevised RomanizationGukjesijang Directed byYoon Je-kyoonWritten byPark Su-jinProduced byYoon Je-kyoon Park Ji-seongStarringHwang Jung-min Yunjin KimCinematographyChoi Young-hwanEdited byLee JinMusic byLee Byung-wooDistributed byCJ Entertainment[1]Release date 17 December 2014 (2014-12-17) (South Korea)Running time130 minutesCountrySouth Kor...

 

この記事は英語版の対応するページを翻訳することにより充実させることができます。(2023年9月)翻訳前に重要な指示を読むには右にある[表示]をクリックしてください。 英語版記事を日本語へ機械翻訳したバージョン(Google翻訳)。 万が一翻訳の手がかりとして機械翻訳を用いた場合、翻訳者は必ず翻訳元原文を参照して機械翻訳の誤りを訂正し、正確な翻訳にしな

 

School in Toronto, OntarioWhitefield Christian SchoolsLocation5808 Finch Ave. East, Scarborough OntarioToronto, Ontario, M1B 4Y6InformationEnrollment300-350 K-12WebsiteWhitefieldChristianSchools.ca Whitefield Christian Schools is a private educational institution registered with the Ontario Ministry of Education, established in 1989.[1] Influential people include: Dr. Larry Saunders - administrator and pastor of Toronto Free Presbyterian Church. Dr. Frank McClelland - president and pa...

Ludwig IIIRaja BavariaBerkuasa5 November 1913 – 13 November 1918PendahuluOttoInformasi pribadiKelahiran(1845-01-07)7 Januari 1845MunichKematian18 Oktober 1921(1921-10-18) (umur 76)Sárvár, HungariaPemakamanFrauenkirche, MunichWangsaWangsa WittelsbachAyahLuitpold, Pangeran Regent dari BavariaIbuAdipati Agung Wanita Augusta dari AustriaPasanganMaria Theresia dari Austria-EsteAnakRupprecht, Pangeran Mahkota BavariaAdelgunde, Putri HohenzollernMaria, Adipati Wanita CalabriaPangeran Karl d...

 

Untuk kecamatan yang bernama sama di Jakarta, lihat Jatinegara, Jakarta Timur. JatinegaraKecamatanPeta lokasi Kecamatan JatinegaraNegara IndonesiaProvinsiJawa TengahKabupatenTegalPemerintahan • CamatSuwatno, S.IPPopulasi • Total54,133 jiwa jiwaKode Kemendagri33.28.07 Kode BPS3328070 Luas79,62 km²Desa/kelurahan17 Kecamatan Jatinegara adalah sebuah kecamatan di Kabupaten Tegal, Provinsi Jawa Tengah, Indonesia. Kecamatan ini berjarak sekitar 22 Km dari ibu kota Kabu...

 

La FranceFormer headquarters of La France, in the rue Montmartre, ParisTypeDaily financialFounder(s)Arthur de La GuéronnièreFounded1863LanguageFrenchHeadquartersParis La France was a daily financial newspaper in the 19th century. Founded in August 1862 by Arthur de La Guéronnière, the newspaper originally followed an editorial line that reconciled loyalty to Napoleon III with the reaffirmation of the temporal power of the Pope.[1] It was bought in 1874 by Émile de Girardin, found...

Defunct airline of Japan and Taiwan (1975—2008) Japan Asia Airways日本アジア航空日本亞細亞航空 IATA ICAO Callsign EG JAA ASIA Founded8 August 1975 (1975-08-08)Ceased operations31 March 2008 (2008-03-31)(re-integrated into Japan Airlines)Focus citiesNagoya–CentrairOsaka–KansaiTaipei–TaoyuanTokyo–NaritaAllianceOneworld (affiliate; 2007—2008)Parent companyJapan Airlines Corp.HeadquartersShinagawa, Tokyo, Japan Japan Asia Airways, Co., Ltd. ...

 

This article includes a list of general references, but it lacks sufficient corresponding inline citations. Please help to improve this article by introducing more precise citations. (February 2016) (Learn how and when to remove this template message) FUJIC FUJIC was the first electronic digital computer in operation in Japan. It was finished in March 1956, the project having been effectively started in 1949, and was built almost entirely by Dr. Okazaki Bunji.[1] Originally desig...

 

Teen Beauty pageant based in Ecuador Not to be confused with Miss Teenager World. Miss Teen WorldEstablished2001; 22 years ago (2001)FounderCésar Montecé, Queen of Ecuador Inc.TypeBeauty pageantHeadquartersEcuadorLocationGuayaquilOfficial language EnglishPresidentRodrigo Moreira (2012-present)Key peopleDiego JaramilloStaff 30WebsiteOfficial website Miss Teen World is an annual international teen beauty pageant based in Ecuador.[1][2][3][4]&#...

South African pharmaceutical professor This article is an orphan, as no other articles link to it. Please introduce links to this page from related articles; try the Find link tool for suggestions. (November 2022) ProfessorViness PillayFAASBorn1970Died24 July 2020Citizenship South AfricaOccupationPharmacistChildren1AwardsNational Research Foundation (NRF)Academic backgroundAlma materUniversity of the Witwatersrand, Johannesburg Viness Pillay FAAS (1970–2020) was a South African profess...

 

This article relies excessively on references to primary sources. Please improve this article by adding secondary or tertiary sources. Find sources: Tamatsukuri Station – news · newspapers · books · scholar · JSTOR (December 2022) (Learn how and when to remove this template message)Railway station in Osaka, Japan Tamatsukuri Station玉造駅North entrance of the JR stationGeneral informationLocationOsakaJapanOperated by JR West Osaka Metro Line(s) Osak...

 

Indian television actor (born 1991) A major contributor to this article appears to have a close connection with its subject. It may require cleanup to comply with Wikipedia's content policies, particularly neutral point of view. Please discuss further on the talk page. (July 2018) (Learn how and when to remove this template message) Mayur VermaMayur Verma in 2018Born (1991-10-06) 6 October 1991 (age 32)[1]Ludhiana, Punjab, IndiaCitizenshipIndianOccupationActorYears active201...

Памятникпавильон «Холодная баня» 59°42′54″ с. ш. 30°23′42″ в. д.HGЯO Страна  Россия Санкт-Петербург, Екатерининский парк Царского Села,город Пушкин Архитектурный стиль Классицизм Автор проекта Чарльз Камерон Строитель Чарльз Камерон Статус  Объект культурног...

 

Painting by Gustav Klimt Portrait of Adele Bloch-Bauer IIArtistGustav KlimtYear1912MediumOil on canvasDimensions190 cm × 120 cm (75 in × 47 in)LocationPrivate collection Portrait of Adele Bloch-Bauer II is a 1912 painting by Gustav Klimt. The work is a portrait of Adele Bloch-Bauer (1881–1925), a Vienna socialite who was a patron and close friend of Klimt.[1] In 1907, Klimt completed an earlier portrait of Bloch-Bauer. During World War...

 

Strategi Solo vs Squad di Free Fire: Cara Menang Mudah!