Computer Science

Queue data structure

A queue is a linear data structure that follows the First In First Out (FIFO) principle. It is used to store a collection of elements and supports two main operations: enqueue, which adds an element to the back of the queue, and dequeue, which removes the element at the front of the queue.

Written by Perlego with AI-assistance

8 Key excerpts on "Queue data structure"

  • Data Structures and Program Design Using Java
    eBook - ePub
    5

    QUEUES

    5.1  Introduction

    A queue is an important data structure which is widely used in many computer applications. A queue can be visualized with many examples from our day-to-day life with which we are already familiar. A very simple illustration of a queue is a line of people standing outside to enter a movie theatre. The first person standing in the line will enter the movie theatre first. Similarly, there are many daily life examples in which we can see the queue being implemented. Hence, we observe that whenever we talk about a queue, we see that that the element at the first position will be served first. Thus, a queue can be described as a FIFO (first in, first out) data structure; that is, the element which is inserted first will be the first one to be taken out. Now, let us discuss more about queues in detail.

    5.2  Definition of a Queue

    A queue is a linear collection of data elements in which the element inserted first will be the element taken out first (i.e., a queue is a FIFO data structure).A queue is an abstract data structure, somewhat similar to stacks. Unlike stacks, a queue is open on both ends. A queue is a linear data structure in which the first element is inserted on one end called the REAR end (also called the tail end), and the deletion of the element takes place from the other end called the FRONT end (also called the head).One end is always used to insert data and the other end is used to remove data.
    Queues can be implemented by using arrays or linked lists. We will discuss the implementation of queues using arrays and linked lists in this section.  
    Practical Application
    • A real-life example of a queue is people moving on an escalator. The people who got on the escalator first will be the first ones to step off of it.
    • Another illustration of a queue is a line of people standing at the bus stop waiting for the bus. Therefore, the first person standing in the line will get into the bus first.
  • Data Structures and Program Design Using Python
    eBook - ePub
    CHAPTER 5
    QUEUES
    5.1 INTRODUCTION
    A queue is an important data structure that is widely used in many computer applications. A queue can be visualized with many examples from everyday life. A very simple illustration of a queue is a line of people standing outside a movie theater. The first person standing in the line will enter the movie theatre first. We observe that whenever we talk about a queue, we see that that the element in the first position is served first. Thus, a queue can be described as a FIFO (First-In, First-Out) data structure; that is, the element that is inserted first will be the first one to be taken out.
    5.2 DEFINITION OF A QUEUE
    A queue is a linear collection of data elements in which the element inserted first is the element taken out first (i.e., a queue is a FIFO data structure). A queue is an abstract data structure, somewhat similar to stacks. Unlike stacks, a queue is open on both ends. A queue is a linear data structure in which the first element is inserted on one end, called the REAR end (also called the tail end), and the deletion of the element takes place from the other end, called the FRONT end (also called the head). One end is always used to insert data and the other end is used to remove data.
    Queues can be implemented by using arrays or linked lists. We discuss the implementation of queues using arrays and linked lists in this section.
    Practical Application:
    A real-life example of a queue is people moving on an escalator. The people who got on the escalator first will be the first ones to step off of it.
    Another illustration of a queue is a line of people standing at the bus stop waiting for the bus. Therefore, the first person standing in the line will get into the bus first.
    5.3 IMPLEMENTATION OF A QUEUE
    Queues can be implemented using two data structures:
    1. arrays/lists
    2. linked lists
    5.3.1 Implementation of Queues Using Arrays
    Queues can be easily implemented using arrays. Initially, the front end (head) and the rear end (tail) of the queue point at the first position or location of the array. As we insert new elements into the queue, the rear keeps on incrementing, always pointing to the position where the next element will be inserted, while the front remains in the first position.
  • Data Structures and Program Design Using C++
    5

    QUEUES

    In This Chapter
    Introduction
    Definition of a queue
    Implementation of a queue
    Operations on queues
    Types of queues
    Applications of queues
    Summary
    Exercises

    5.1Introduction

    A queue is an important data structure which is widely used in many computer applications. A queue can be visualized with many examples from our day-to-day life with which we are already familiar. A very simple illustration of queue is a line of people standing outside to enter a movie theater. The first person standing in the line will enter the movie theater first. Similarly, there are many daily life examples in which we can see the queue being implemented. Hence, we observe that whenever we talk about a queue, we see that that the element at the first position will be served first. Thus, a queue can be described as a FIFO (first in, first out) data structure; that is, the element which is inserted first will be the first one to be taken out. Now, let us discuss more about queues in detail.

    5.2Definition of a Queue

    A queue is a linear collection of data elements in which the element inserted first will be the element taken out first (i.e., a queue is a FIFO data structure). A queue is an abstract data structure, somewhat similar to stacks. Unlike stacks, a queue is open on both ends.
    A queue is a linear data structure in which the first element is inserted on one end called the REAR end (also called the tail end), and the deletion of the element takes place from the other end called the FRONT end (also called the head)
    . One end is always used to insert data and the other end is used to remove data.
    Queues can be implemented by using arrays or linked lists. We will discuss the implementation of queues using arrays and linked lists in this section.
    Practical Application: •A real-life example of a queue is people moving on an escalator. The people who got on the escalator first will be the first ones to step off of it.
  • C# Data Structures and Algorithms
    n is the number of discs.
    That's all! In this section, you have learned the first limited access data structure, namely a stack. Now, it is high time that you get to know more about queues. Let's start! Passage contains an image

    Queues

    A queue is a data structure that can be presented using the example of a line of people waiting in a shop at the checkout. New people stand at the end of the line, and the next person is taken to the checkout from the beginning of the line. You are not allowed to choose a person from the middle and serve him or her in a different order.
    The Queue data structure operates in exactly the same way. You can only add new elements at the end of the queue (the enqueue operation) and remove an element from the queue only from the beginning of the queue (the dequeue operation). For this reason, this data structure is consistent with the FIFO principle, which stands for First-In First-Out . In the example regarding a line of people waiting in a shop at the checkout, people who come first (first-in) will be served before those who come later (first-out).
    The operation of a queue is presented in the following diagram:
    It is worth mentioning that a queue is a recursive data structure , similarly as a stack. This means that a queue can be either empty or consists of the first element and the rest of the queue, which also forms a queue, as shown in the following diagram (the beginning of the queue is marked in gray):
    The Queue data structure seems to be very easy to understand, as well as similar to a stack, apart from the way of removing an element. Does this mean that you can also use a built-in class to use a queue in your programs? Fortunately, yes! The available generic class is named Queue and is defined in the System.Collections.Generic namespace.
  • Essential Algorithms
    eBook - ePub

    Essential Algorithms

    A Practical Approach to Computer Algorithms Using Python and C#

    • Rod Stephens(Author)
    • 2019(Publication Date)
    • Wiley
      (Publisher)
    .

    Queues

    A queue is a data structure where items are added and removed in first-in-first-out order. Because of this first-in-first-out (FIFO, usually pronounced “fife-oh”) behavior, stacks are sometimes called FIFO lists or FIFOs.
    A queue is similar to a store's checkout queue. You join the end of the queue and wait your turn. When you get to the front of the queue, the cashier takes your money and gives you a receipt.
    Usually, the method that adds an item to a queue is called Enqueue , and the item that removes an item from a queue is called Dequeue .
    The following sections describe a few of the more common methods for implementing a queue.

    Linked-List Queues

    Implementing a queue is easy using a linked list. To make removing the last item from the queue easy, the queue should use a doubly linked list.
  • Quick Reference to Data Structures and Computer Algorithms, A
    eBook - ePub
    HAPTER 3

    Stacks and Queues

    3.1 Definition and Concepts

    It may be possible that there is a need for a data structure which takes operation on only one end. i.e., beginning or end of the list. In linked list and arrays, we can perform operations at any place of the list. Stack and Queue, are data structures which fulfill the requirements to perform operation on only at one end.

    3.2 Stack

    A stack is a special purpose data structure, containing a list, where data are arranged in order, in which all operations such as insertions and deletions are carried out in only one place, called the TOP of the stack.
    Given a stack, S = (a1 , …., an ) then we say that a1 is the bottom most element and element ai is on top of element ai -1, 1 < i ≤ n.
    Definition: A stack is a collection of similar data, which is having some order, where all the operations like insertion and deletion, carried out, only at one end.
    Stack is also a linear data structure, the insertion and deletion operations in case of stack is specially termed as PUSH and POP respectively, and the position of the stack where such operations are carried out, is known as the TOP of the stack. An element in a stack is commonly termed as an ITEM. The SIZE of a stack defines the maximum number of elements that a stack can hold at once (figure 3.1).
    Figure 3.1 Schematic diagram of a stack
    In stack, the element, which is inserted the last, is the first to be removed from the stack. For this reason, stacks are also known as Last In First Out(LIFO) lists.
    One natural example of stacks which arises in Computer Programming is the processing of subroutine calls and their returns. Suppose we have a main procedure and 3 subroutines as follows:
    Figure 3.2 Sequence of subroutines being called
    The MAIN program calls subroutine X. On completion of X, execution of MAIN will resume at location b. The address b is passed to X, which saves it in some location for later procession. X then invokes Y, which in turn calls Z. In every case the calling procedure passes the return address to the called procedure. If we examine the memory which Z is computing there will be an implicit stack which looks like (a, b, c, d).
  • IT Interview Guide for Freshers
    eBook - ePub

    IT Interview Guide for Freshers

    Crack your IT interview with confidence

    Stack can be implemented using arrays and linked lists. The following diagram depicts the stack and queue operations for adding and removing elements:
    Figure 4.3: Stack vs Queue data structure operations

    How much minimum number of queues is needed to implement a priority queue?

    To implement a priority queue, two queues are required in the application. One queue is leveraged for storing of data and the other queue is required for storing.

    Collections

    What is the objective for collection classes?

    Collection classes provide different functions to operate on collections, maps, and datastructures, enabling synchronization and making the classes read-only.

    Compare a LinkedList and an ArrayList?

    • An ArrayList leverages a dynamic array, whereas LinkedList leverages a doubly linked list.
    • Data manipulation functional is faster with a LinkedList compared to an ArrayList .
    • An ArrayList can only be leveraged as a list, but a LinkedList can be leveraged as a list and a queue.
    The following diagram depicts how data elements are transversed and stored in the linked list and array list:
    Figure 4.4: Linked list vs array list

    What is a Vector class?

    The Vector class is comparable to ArrayList with regards to accessing the items in a collection. Once an object of the vector is created, its size can increase or decrease whenever we add or remove items in a Vector . It is synchronized by default.

    What is a Stack class?

    A Stack class represents Last In First Out (LIFO) queue of objects. The data items in the stack can only be accessed from the top of the stack. One can insert, remove or retrieve a data item from only the top of the stack. The following diagram depicts the push and pop operations on the Stack
  • PHP 7 Data Structures and Algorithms

    Constructing Stacks and Queues

    In everyday life, we use two of the most common data structures. We can assume that these data structures are inspired by the real-world, but they have very important effects in the computing world. We are talking about stack and Queue data structures. We stack our books, files, plates, and clothes on a daily basis, whereas we maintain queues for ticket counters, bus stops, and shopping checkouts. Also, we have heard about message queue in PHP, one of the most used features in high-end applications. In this chapter, we are going to explore the different implementations of popular stack and Queue data structures. We are going to learn about queues, priority queues, circular queues, and double-ended queues in PHP.
    Passage contains an image

    Understanding stack

    The stack is a linear data structure that follows the Last-In , First-Out (LIFO ) principle. This means that there is only one end for the stack, which is used to add items and remove items from the structure. The addition of new items in the stack is known as push, and push whilst removing an item is known as pop. Since we only have one end to operate, we are always going to push an item at that end, and when we pop, the last item from that end will be popped up. The top most elements in the stack that are also at the very beginning of the stack end are known as the top. If we consider the following image, we can see that after each pop and push operation, the top changes. Also, we are performing the operation at the top of the stack, not at the beginning or middle of the stack. We have to be careful about popping an element when the stack is empty, as well as pushing an element when the stack is full. We might have a stack overflow if we want to push more elements than its capacity.
    From our earlier discussion, we now know that we have four basic operations in a stack:
    • Push : add an item at the top of the stack.
    • Pop : remove the top item of the stack.
    • Top
Index pages curate the most relevant extracts from our library of academic textbooks. They’ve been created using an in-house natural language model (NLM), each adding context and meaning to key research topics.