title: priority queue vs linked list java
body: <p>I was solving <code>BFS</code> problem. I used PriorityQueue but I was getting wrong answer, then I used <code>LinkedList</code>, I got right ans. I'm unable to find the difference between them. Here are both the codes. Why both the answers are different?</p>
  
  <pre><code>Code1:    
          LinkedList q=new LinkedList();
          q.add(src);
          dist[src]=0;
          visited[src]=1;
          while(!q.isEmpty()){
              u=(int)(Integer) q.remove(0);
              for (int k = 0; k &lt; n; k++) {
                  if(a[u][k]==1 &amp;&amp; visited[k]==0)
                  {
                      dist[k]=dist[u]+1;
                      q.add(k);
                      visited[k]=1;
                  }   
              }
          }
  
  Code 2: 
      PriorityQueue&lt;Integer&gt; q= new PriorityQueue&lt;&gt;();
          q.add(src);            
          dist[src]=0;
          visited[src]=1;
          while(!q.isEmpty()){
              u=q.remove();               
              for (int k = 0; k &lt; n; k++) {
                  if(a[u][k]==1 &amp;&amp; visited[k]==0)
                  {
                      dist[k]=dist[u]+1;
                      q.add(k);
                      visited[k]=1;
                  }   
              }
          }
  </code></pre>
  
  <p>Also when I used Adjacency List instead of Adjacency matrix, Priority Queue implementation gave right ans.</p>