{"id":26920,"date":"2017-12-26T22:07:24","date_gmt":"2017-12-26T16:37:24","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=26920"},"modified":"2017-12-26T22:07:24","modified_gmt":"2017-12-26T16:37:24","slug":"java-programming-queue-data-structure-linked-list-implementation","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/java-programming-queue-data-structure-linked-list-implementation\/","title":{"rendered":"Java Programming-Queue -Data Structure-Linked List Implementation"},"content":{"rendered":"<p>In the <a href=\"http:\/\/quiz.geeksforgeeks.org\/queue-set-1introduction-and-array-implementation\/\" target=\"_blank\" rel=\"noopener noreferrer\">previous post<\/a>, we introduced Queue and discussed array implementation. <span id=\"more-142785\"><\/span>In this post, linked list implementation is discussed. The following two main operations must be implemented efficiently.<\/p>\n<p>In a Queue data structure, we maintain two pointers, <em>front<\/em> and <em>rear<\/em>. The <em>front<\/em> points the first item of queue and <em>rear<\/em> points to last item.<\/p>\n<p><strong>enQueue()<\/strong> This operation adds a new node after <em>rear <\/em>and moves <em>rear<\/em> to the next node.<\/p>\n<p><strong>deQueue()<\/strong> This operation removes the front node and moves <em>front<\/em> to the next node.<\/p>\n<p><strong>Java programming<\/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 linked-list implementation of queue<br\/>  <br\/>\/\/ A linked list (LL) node to store a queue entry<br\/>class QNode<br\/>{<br\/>    int key;<br\/>    QNode next;<br\/>     <br\/>    \/\/ constructor to create a new linked list node<br\/>    public QNode(int key) {<br\/>        this.key = key;<br\/>        this.next = null;<br\/>    }<br\/>}<br\/> <br\/>\/\/ A class to represent a queue<br\/>\/\/The queue, front stores the front node of LL and rear stores ths<br\/>\/\/last node of LL<br\/>class Queue<br\/>{<br\/>    QNode front, rear;<br\/>      <br\/>    public Queue() {<br\/>        this.front = this.rear = null;<br\/>    }<br\/>      <br\/>    \/\/ Method to add an key to the queue.  <br\/>    void enqueue(int key)<br\/>    {<br\/>         <br\/>        \/\/ Create a new LL node<br\/>        QNode temp = new QNode(key);<br\/>      <br\/>        \/\/ If queue is empty, then new node is front and rear both<br\/>        if (this.rear == null)<br\/>        {<br\/>           this.front = this.rear = temp;<br\/>           return;<br\/>        }<br\/>      <br\/>        \/\/ Add the new node at the end of queue and change rear<br\/>        this.rear.next = temp;<br\/>        this.rear = temp;<br\/>    }<br\/>      <br\/>    \/\/ Method to remove an key from queue.  <br\/>    QNode dequeue()<br\/>    {<br\/>        \/\/ If queue is empty, return NULL.<br\/>        if (this.front == null)<br\/>           return null;<br\/>      <br\/>        \/\/ Store previous front and move front one node ahead<br\/>        QNode temp = this.front;<br\/>        this.front = this.front.next;<br\/>      <br\/>        \/\/ If front becomes NULL, then change rear also as NULL<br\/>        if (this.front == null)<br\/>           this.rear = null;<br\/>        return temp;<br\/>    }<br\/>}<br\/>  <br\/>   <br\/>\/\/ Driver class<br\/>public class Test<br\/>{<br\/>    public static void main(String[] args) <br\/>    {<br\/>        Queue q=new Queue();<br\/>        q.enqueue(10);<br\/>        q.enqueue(20);<br\/>        q.dequeue();<br\/>        q.dequeue();<br\/>        q.enqueue(30);<br\/>        q.enqueue(40);<br\/>        q.enqueue(50);<br\/>         <br\/>        System.out.println(&quot;Dequeued item is &quot;+ q.dequeue().key);<br\/>    }<br\/>}<\/code><\/pre> <\/div>\n<p><strong>\u00a0<\/strong>Output:<\/p>\n<pre>Dequeued item is 30<\/pre>\n<p><strong>Time Complexity:<\/strong> Time complexity of both operations enqueue() and dequeue() is O(1) as we only change few pointers in both operations. There is no loop in any of the operations.<\/p>\n[ad type=&#8221;banner&#8221;]\n","protected":false},"excerpt":{"rendered":"<p>Java programming queue data structure linked list implementation In the previous post, we introduced Queue and discussed array implementation<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[73012,79476],"tags":[80342,80340,80330,80339,80331,80327,80334,80332,80333,80341,80335,80338,80336,80337,80328,80329],"class_list":["post-26920","post","type-post","status-publish","format-standard","hentry","category-data-structures","category-linked-list","tag-circular-queue-implementation-in-c","tag-circular-queue-using-linked-list-in-c","tag-doubly-linked-list-implementation-in-java","tag-implement-queue-using-linked-list-geeksforgeeks","tag-linked-list-data-structure-in-java","tag-linked-list-implementation-c","tag-linked-list-implementation-in-c","tag-linked-list-implementation-in-java-source-code","tag-linked-list-program-in-java","tag-linked-queue-in-data-structure","tag-queue-implementation-using-array","tag-queue-using-linked-list-algorithm","tag-queue-using-linked-list-c","tag-queue-using-linked-list-java","tag-singly-linked-list-implementation-in-java","tag-singly-linked-list-java-tutorial"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26920","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=26920"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26920\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=26920"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=26920"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=26920"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}