Computer Science

Java Queue Interface

Java Queue Interface is a collection interface that represents a queue data structure. It extends the Collection interface and provides methods for adding, removing, and examining elements in a queue. The Queue interface is implemented by several classes in the Java Collections Framework, including LinkedList and PriorityQueue.

Written by Perlego with AI-assistance

10 Key excerpts on "Java Queue Interface"

  • Book cover image for: Fundamentals of Python
    eBook - PDF

    Fundamentals of Python

    Data Structures

    A queue thus supports a first-in first-out (FIFO) protocol. Queues are omnipresent in everyday life and occur in any situation where people or things are lined up for service or processing on a first-come, first-served basis. Checkout lines in stores, highway tollbooth lines, and airport baggage check-in lines are familiar examples of queues. Queues have two fundamental operations: add, which adds an item to the rear of a queue, and pop, which removes an item from the front. Figure 8-1 shows a queue as it might appear at various stages in its lifetime. In the figure, the queue’s front is on the left, and its rear is on the right. Initially, the queue is empty. Then an item called a is added. Next, three more items called b, c, and d are added, after which an item is popped, and so forth. Figure 8-1 The states in the lifetime of a queue Copyright 2019 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s). Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it. 207 The Queue Interface and Its Use Related to queues is a collection called a priority queue. In a queue, the item popped, or served next, is always the item that has been waiting the longest. But in some circum- stances, this restriction is too rigid, and it’s preferable to combine the idea of waiting with a notion of priority. In a priority queue, higher-priority items are popped before those of lower priority, and items of equal priority are popped in FIFO order. Consider, for example, the manner in which passengers board an aircraft. The first-class passengers line up and board first, and the lower-priority coach-class passengers line up and board second.
  • Book cover image for: Data Structures and Algorithms Using C#
    80 STACKS AND QUEUES A A arrives in queue A B B C B arrives in queue A C C arrives in queue B A departs from queue C B departs from queue F IGURE 5.2. Queue Operations. Q UEUES , THE Q UEUE C LASS AND A Q UEUE C LASS I MPLEMENTATION A queue is a data structure where data enters at the rear of a list and is removed from the front of the list. Queues are used to store items in the order in which they occur. Queues are an example of a first-in, first-out (FIFO) data structure. Queues are used to order processes submitted to an operating system or a print spooler, and simulation applications use queues to model customers waiting in a line. Queue Operations The two primary operations involving queues are adding a new item to the queue and removing an item from the queue. The operation for adding a new item is called Enqueue , and the operation for removing an item from a queue is called Dequeue . The Enqueue operation adds an item at the end of the queue and the Dequeue operation removes an item from the front (or beginning) of the queue. Figure 5.2 illustrates these operations. The other primary operation to perform on a queue is viewing the beginning item. The Peek method, like its counterpoint in the Stack class, is used to view the beginning item. This method simply returns the item without actually removing it from the queue. There are other properties of the Queue class we can use to aid in our programming. However, before we discuss them let’s look at how we can implement a Queue class. Queues, the Queue Class and a Queue Class Implementation 81 A Queue Implementation Implementing the Queue class using an ArrayList is practically a no-brainer, as was our implementation of the Stack class. ArrayLists are excellent imple-mentation choices for these types of data structures because of their built-in dynamics. When we need to insert an item into our queue, the Arraylist Add method places the item in the next free element of the list.
  • Book cover image for: Data Structures and Program Design Using Java
    No longer available |Learn more
    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.
  • Book cover image for: Data Structures and Algorithms in Java
    • Michael T. Goodrich, Roberto Tamassia, Michael H. Goldwasser(Authors)
    • 2014(Publication Date)
    • Wiley
      (Publisher)
    238 Chapter 6. Stacks, Queues, and Deques 6.2 Queues Another fundamental data structure is the queue. It is a close “cousin” of the stack, but a queue is a collection of objects that are inserted and removed according to the first-in, first-out (FIFO) principle. That is, elements can be inserted at any time, but only the element that has been in the queue the longest can be next removed. We usually say that elements enter a queue at the back and are removed from the front. A metaphor for this terminology is a line of people waiting to get on an amusement park ride. People waiting for such a ride enter at the back of the line and get on the ride from the front of the line. There are many other applications of queues (see Figure 6.4). Stores, theaters, reservation centers, and other similar services typically process customer requests according to the FIFO principle. A queue would therefore be a logical choice for a data structure to handle calls to a customer service center, or a wait-list at a restaurant. FIFO queues are also used by many computing devices, such as a networked printer, or a Web server responding to requests. Tickets (a) Call Center Call Queue (b) Figure 6.4: Real-world examples of a first-in, first-out queue. (a) People waiting in line to purchase tickets; (b) phone calls being routed to a customer service center. 6.2. Queues 239 6.2.1 The Queue Abstract Data Type Formally, the queue abstract data type defines a collection that keeps objects in a sequence, where element access and deletion are restricted to the first element in the queue, and element insertion is restricted to the back of the sequence. This restriction enforces the rule that items are inserted and deleted in a queue accord- ing to the first-in, first-out (FIFO) principle. The queue abstract data type (ADT) supports the following two update methods: enqueue(e): Adds element e to the back of queue.
  • Book cover image for: A Textbook of Data Structures and Algorithms, Volume 1
    eBook - PDF
    • G. A. Vijayalakshmi Pai(Author)
    • 2022(Publication Date)
    • Wiley-ISTE
      (Publisher)
    5 Queues In this chapter, we discuss the queue data structure, its operations and its variants, namely, circular queues, priority queues and deques. The application of the data structure is demonstrated on the problem of job scheduling in a time-sharing system environment. 5.1. Introduction A queue is a linear list in which all insertions are made at one end of the list known as the rear or tail of the queue, and all deletions are made at the other end known as the front or head of the queue. An insertion operation is also referred to as enqueuing a queue, and a deletion operation is referred to as dequeuing a queue. Figure 5.1 illustrates a queue and its functionality. Here, Q is a queue of three elements a, b, and c (Figure 5.1(a)). When an element d is to join the queue, it is inserted at the rear end of the queue (Figure 5.1(b)), and when an element is to be deleted, the element at the front end of the queue, namely, a, is deleted automatically (Figure 5.1(c)). Thus, a queue data structure obeys the principle of First In First Out (FIFO) or First Come First Served (FCFS). Many examples of queues occur in everyday life. Figure 5.2(a) illustrates a queue of customers waiting to be served by a clerk at the booking counter, and Figure 5.2(b) illustrates a trail of components moving down an assembly line to be processed by a robot at the end of the line. The FIFO principle of insertion at the rear end of the queue when a new client arrives or when a new component is added, and deletion at the front end of the queue when the service of the client or processing of the component is complete, is evident. 102 A Textbook of Data Structures and Algorithms 1 Figure 5.1. A queue and its functionality 5.2. Operations on queues The queue data structure supports two operations, namely, i) insertion or addition of elements to a queue; ii) deletion or removal of elements from a queue.
  • Book cover image for: 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.
  • Book cover image for: Beginning Algorithms
    • Simon Harris, James Ross(Authors)
    • 2008(Publication Date)
    • Wrox
      (Publisher)
    Unbounded queues, conversely, are free to grow in size as the limits of the hardware allow. Queue Operations This chapter describes several different queues used throughout the course of this book, all with slightly different retrieval order. Irrespective of their behavior, the various queues all share a common interface. Table 4-1 lists each of the queue operations along with a brief description. Table 4-1: Queue Operations Operation Description enqueue Stores a value in the queue. The size of the queue will increase by one. dequeue Retrieves the value at the head of the queue. The size of the queue will decrease by one. Throws EmptyQueueException if there are no more items in the queue. clear Deletes all elements from the queue. The size of the queue will be reset to zero (0). Size Obtains the number of elements in the queue. isEmpty Determines whether the queue is empty (size() == 0) or not. Producer Queue Producer Producer Consumer Consumer Consumer 76 Chapter 4 As you can see, the queue interface is much simpler than that of the list: enqueue() is responsible for storing values, dequeue() for retrieving them. The remaining methods have the same behavior as those with the same names defined for lists. Notice also that there is no means for accessing all of the data items in a queue at once using an iterator (see Chapter 2), further reinforcing the idea that the only thing you can do is obtain the item at the head. The Queue Interface Any operations you have defined can be translated directly into a Java interface so that you can easily create pluggable implementations: package com.wrox.algorithms.queues; public interface Queue { public void enqueue(Object value); public Object dequeue() throws EmptyQueueException; public void clear(); public int size(); public boolean isEmpty(); } Each operation has been translated directly into a method on the interface.
  • Book cover image for: Data Structures And Algorithms
    103 104 A. Guercio 6.2. The Concept of Queue The concept of line explains very clearly the concept of a queue. Have you ever been in line in a bank or in a super market? That line is a queue. When you enter the line you put yourself at the end of the line, the tail and the person who is at the front of the line, the head is the next who will be served as soon as the cashier is available. The person will exit the queue and will be served. The next person who will enter the line will put himself behind you and so on. In the meanwhile the queue is served and the next person at the head of the line will exit the queue and will be served. While the queue is served, you will move towards the head of the line since each person that is served will be removed from the head of the queue. You will be, then, in the middle of the line waiting patiently for all the persons in front of you to be served and will exit the line one by one in the order they entered the queue. Finally you will reach the head of the line and you will exit the queue and be served. The behavior of this line is said a first-in-first-out (FIFO) behavior, that is the person that is removed from the line is the one that entered earliest the line. This behavior is very useful, in any case the order of arrival has to be maintained. Example of queues can be seen in the everyday life, but are common in computing applications as well. For example, processes or threads may be scheduled to run on the CPU in FIFO order. These processes may be stored by the operating system using a queue data structure. I/O requests are queued for service on a disk in a multi-user time-shared server. The I/O requests have to be served in a FIFO order. Messages are queued in a buffer in a client-server communication while waiting for the server to be available to receive the message. Summarizing, let us consider a finite set of data of type T, a queue is an abstract data type which exhibits a first-in-first-out behavior (Fig. 6.1).
  • Book cover image for: Open Data Structures
    • Pat Morin(Author)
    • 2013(Publication Date)
    • AU Press
      (Publisher)
    An interface describes what a data structure does, while an implementation describes how the data structure does it. An interface , sometimes also called an abstract data type , defines the set of operations supported by a data structure and the semantics, or meaning, of those operations. An interface tells us nothing about how the data structure implements these operations; it only provides a list of supported operations along with specifications about what types of argu-ments each operation accepts and the value returned by each operation. A data structure implementation , on the other hand, includes the inter-nal representation of the data structure as well as the definitions of the algorithms that implement the operations supported by the data struc-ture. Thus, there can be many implementations of a single interface. For example, in Chapter 2 , we will see implementations of the List interface using arrays and in Chapter 3 we will see implementations of the List interface using pointer-based data structures. Each implements the same interface, List , but in di ff erent ways. 4 Interfaces §1.2 x ··· add ( x )/ enqueue ( x ) remove ()/ dequeue () Figure 1.1: A FIFO Queue . 1.2.1 The Queue , Stack , and Deque Interfaces The Queue interface represents a collection of elements to which we can add elements and remove the next element. More precisely, the opera-tions supported by the Queue interface are • add ( x ): add the value x to the Queue • remove (): remove the next (previously added) value, y , from the Queue and return y Notice that the remove () operation takes no argument. The Queue ’s queue-ing discipline decides which element should be removed. There are many possible queueing disciplines, the most common of which include FIFO, priority, and LIFO.
  • Book cover image for: Programming with C++
    • Kyla McMullen, Elizabeth Matthews, June Jamrich Parsons, , Kyla McMullen, Kyla McMullen, Elizabeth Matthews, June Jamrich Parsons(Authors)
    • 2021(Publication Date)
    Asynchronous data transfer. Data that cannot always flow freely may be held in a buffer and then released. Keyboards include buffers that store keypresses until they can be handled by a software application. Print spool- ers hold character data until a printer is ready to receive it. Flood fill algorithms. Games such as Go and Minesweeper use queues to determine which squares are cleared. In Paint software, a queue controls the bucket tool that fills connected, similarly colored areas with a specified new color. See Figure 20-13. Figure 20-13 Flood fill algorithms are based on queues First come, first served. Applications that control commercial phone systems place callers on hold and connect them to agents on a first come, first served basis. Print queues hold a series of documents and print them out in the order that they were received. Traversal algorithms. Queues are used to follow a path through a hierarchical structure called a tree. Shortest path. A queue is a key part of the solution to mapping problems such as the classic Chess Knight Problem to find the minimum number of steps taken by the Knight to move from point A to point B on a chess- board. See Figure 20-14. Copyright 2022 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s). Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it. PROGRAMMING WITH C++ 364 Code a Queue (20.2.7, 20.2.8) Although most programming languages provide a variety of built-in functionality for queues, you have the most control if you create a linked list and provide it with methods for enqueueing and dequeuing.
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.