{"id":26878,"date":"2017-12-26T20:37:32","date_gmt":"2017-12-26T15:07:32","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=26878"},"modified":"2017-12-26T20:37:32","modified_gmt":"2017-12-26T15:07:32","slug":"java-programming-queue-data-structure-introduction-and-array-implementation","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/java-programming-queue-data-structure-introduction-and-array-implementation\/","title":{"rendered":"Java Programming-Queue-Data Structure-Introduction and Array Implementation"},"content":{"rendered":"<p>Like <a href=\"http:\/\/quiz.geeksforgeeks.org\/stack-set-1\/\" target=\"_blank\" rel=\"noopener noreferrer\">Stack<\/a>, <a href=\"http:\/\/en.wikipedia.org\/wiki\/Queue_%28data_structure%29\" target=\"_blank\" rel=\"noopener\">Queue\u00a0<\/a>is a linear structure which follows a particular order in which the operations are performed.<span id=\"more-142802\"><\/span> The order is <strong>F<\/strong>irst <strong>I<\/strong>n <strong>F<\/strong>irst <strong>O<\/strong>ut (FIFO). \u00a0A good example of queue is any queue of consumers for a resource where the consumer that came first is served first.<br \/>\nThe difference between stacks and queues is in removing. In a stack we remove the item the most recently added; in a queue, we remove the item the least recently added.<\/p>\n<p><strong>Operations on Queue:<\/strong><br \/>\nMainly the following four basic operations are performed on queue:<\/p>\n<p><strong>Enqueue:\u00a0<\/strong>Adds an item to the queue. If the queue is full, then it is said to be an Overflow condition.<br \/>\n<strong>Dequeue:<\/strong>\u00a0Removes an item from the queue. The items are popped in the same order in which they are pushed. If the queue is empty, then it is said to be an Underflow condition.<br \/>\n<strong>Front:\u00a0<\/strong>Get the front item from queue.<br \/>\n<strong>Rear:<\/strong> Get the last item from queue.<\/p>\n<p><img fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter size-full wp-image-26887\" src=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/Queue-Data-Structure-Introduction-and-Array-Implementation.png\" alt=\"Queue-Data Structure-Introduction and Array Implementation\" width=\"741\" height=\"272\" srcset=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/Queue-Data-Structure-Introduction-and-Array-Implementation.png 741w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/Queue-Data-Structure-Introduction-and-Array-Implementation-300x110.png 300w\" sizes=\"(max-width: 741px) 100vw, 741px\" \/><\/p>\n<p><strong>Applications of Queue:<\/strong><br \/>\nQueue is used when things don\u2019t have to be processed immediatly, but have to be processed in\u00a0<strong>F<\/strong>irst\u00a0<strong>I<\/strong>n<strong>F<\/strong>irst\u00a0<strong>O<\/strong>ut order like\u00a0<a href=\"http:\/\/en.wikipedia.org\/wiki\/Breadth-first_search\" target=\"_blank\" rel=\"noopener\">Breadth First Search<\/a>. This property of Queue makes it also useful in following kind of scenarios.<\/p>\n<p><strong>1)<\/strong>\u00a0When a resource is shared among multiple consumers. Examples include CPU scheduling, Disk Scheduling.<br \/>\n<strong>2)\u00a0<\/strong>When data is transferred asynchronously (data not necessarily received at same rate as sent) between two processes. Examples include IO Buffers, pipes, file IO, etc.<\/p>\n<p>See\u00a0<a href=\"http:\/\/introcs.cs.princeton.edu\/43stack\/\" target=\"_blank\" rel=\"noopener\">this\u00a0<\/a>for more detailed applications of Queue and Stack.<\/p>\n[ad type=&#8221;banner&#8221;]\n<p><strong>Array implementation Of Queue<\/strong><br \/>\nFor implementing queue, we need to keep track of two indices, front and rear. We enqueue an item at the rear and dequeue an item from front. If we simply increment front and rear indices, then there may be problems, front may reach end of the array. The solution to this problem is to increase front and rear in circular manner (See <a href=\"http:\/\/www.cs.colostate.edu\/~anderson\/cs200\/index.html\/doku.php?id=recit:array_based_queue\" target=\"_blank\" rel=\"noopener noreferrer\">this <\/a>for details)<\/p>\n<p><strong>Java \u00a0Programming<\/strong><\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-java code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-java code-embed-code\">\/\/ Java program for array implementation of queue<br\/>  <br\/>\/\/ A class to represent a queue<br\/>class Queue<br\/>{<br\/>    int front, rear, size;<br\/>    int  capacity;<br\/>    int array[];<br\/>      <br\/>    public Queue(int capacity) {<br\/>         this.capacity = capacity;<br\/>         front = this.size = 0; <br\/>         rear = capacity - 1;<br\/>         array = new int[this.capacity];<br\/>           <br\/>    }<br\/>      <br\/>    \/\/ Queue is full when size becomes equal to <br\/>    \/\/ the capacity <br\/>    boolean isFull(Queue queue)<br\/>    {  return (queue.size == queue.capacity);<br\/>    }<br\/>      <br\/>    \/\/ Queue is empty when size is 0<br\/>    boolean isEmpty(Queue queue)<br\/>    {  return (queue.size == 0); }<br\/>      <br\/>    \/\/ Method to add an item to the queue. <br\/>    \/\/ It changes rear and size<br\/>    void enqueue( int item)<br\/>    {<br\/>        if (isFull(this))<br\/>            return;<br\/>        this.rear = (this.rear + 1)%this.capacity;<br\/>        this.array[this.rear] = item;<br\/>        this.size = this.size + 1;<br\/>        System.out.println(item+ &quot; enqueued to queue&quot;);<br\/>    }<br\/>      <br\/>    \/\/ Method to remove an item from queue.  <br\/>    \/\/ It changes front and size<br\/>    int dequeue()<br\/>    {<br\/>        if (isEmpty(this))<br\/>            return Integer.MIN_VALUE;<br\/>          <br\/>        int item = this.array[this.front];<br\/>        this.front = (this.front + 1)%this.capacity;<br\/>        this.size = this.size - 1;<br\/>        return item;<br\/>    }<br\/>      <br\/>    \/\/ Method to get front of queue<br\/>    int front()<br\/>    {<br\/>        if (isEmpty(this))<br\/>            return Integer.MIN_VALUE;<br\/>          <br\/>        return this.array[this.front];<br\/>    }<br\/>       <br\/>    \/\/ Method to get rear of queue<br\/>    int rear()<br\/>    {<br\/>        if (isEmpty(this))<br\/>            return Integer.MIN_VALUE;<br\/>          <br\/>        return this.array[this.rear];<br\/>    }<br\/>}<br\/>  <br\/>   <br\/>\/\/ Driver class<br\/>public class Test<br\/>{<br\/>    public static void main(String[] args) <br\/>    {<br\/>        Queue queue = new Queue(1000);<br\/>           <br\/>        queue.enqueue(10);<br\/>        queue.enqueue(20);<br\/>        queue.enqueue(30);<br\/>        queue.enqueue(40);<br\/>       <br\/>        System.out.println(queue.dequeue() + <br\/>                     &quot; dequeued from queue\\n&quot;);<br\/>       <br\/>        System.out.println(&quot;Front item is &quot; + <br\/>                               queue.front());<br\/>          <br\/>        System.out.println(&quot;Rear item is &quot; + <br\/>                                queue.rear());<br\/>    }<br\/>}<br\/> <\/code><\/pre> <\/div>\n<p>Output:<\/p>\n<pre>10 enqueued to queue\r\n20 enqueued to queue\r\n30 enqueued to queue\r\n40 enqueued to queue\r\n10 dequeued from queue\r\nFront item is 20\r\nRear item is 40<\/pre>\n<p><strong>Time Complexity:<\/strong> Time complexity of all operations like enqueue(), dequeue(), isFull(), isEmpty(), front() and rear() is O(1). There is no loop in any of the operations.<\/p>\n[ad type=&#8221;banner&#8221;]\n","protected":false},"excerpt":{"rendered":"<p>queue data structure introduction array implementation Like Stack, Queue is a linear structure which follows a particular order in which the operations<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1,73012,2139],"tags":[80209,80203,80208,80207,80205,80204,80206,80200,80202,70068,80210,75626,80198,80199,80201],"class_list":["post-26878","post","type-post","status-publish","format-standard","hentry","category-coding","category-data-structures","category-java","tag-advantages-and-disadvantages-of-array-in-data-structure","tag-algorithm-for-insertion-and-deletion-in-queue","tag-application-of-array-in-data-structure","tag-array-in-data-structure-and-algorithm","tag-array-in-data-structure-pdf","tag-array-in-data-structure-with-example","tag-array-operations-in-data-structure-ppt","tag-circular-queue-using-array-in-c","tag-implementation-of-queue-using-array-in-data-structure","tag-linear-array-in-data-structure","tag-one-dimensional-array-in-data-structure","tag-queue-in-c-using-linked-list","tag-queue-in-data-structure-with-example","tag-queue-using-array-in-c","tag-write-a-c-program-to-implement-queue-using-array"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26878","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=26878"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26878\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=26878"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=26878"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=26878"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}