title: double pointer to a node
body: <p>My doubt is regarding pointer only,Here  head is a double pointer  to <code>Queue</code> if we are using <code>*head</code> than we are accessing the location(or address passed) inside main but when we are using simply <code>head</code> than we are using heading in the current function only which will hold the address of pointer to <code>Queue</code> now when we  are doing this <code>head=&amp;(*head)-&gt;next</code> the since (*head)->next is itself a address and when we use <code>&amp;</code> before this ,than will a separate block will memory block will be created and hold the address of (*head)->next and we are assigning that address to head
  I have this doubt because its like a two step process we cannot directly put the (*head)->next to sore something inside <code>head</code> we need to pass address of address  for that we would require a extra block and when the loop will executed say n times than there will be n intermediate blocks?
  Please tell me if i am correct or not
  and tell the right logic thanks </p>
  
  <pre><code>void queue_push(Queue **head, int d, int p)
  {
      Queue *q = queue_new(d, p);
  
      while (*head &amp;&amp; (*head)-&gt;priority &lt; p) {
          head = &amp;(*head)-&gt;next;
      }
  
      q-&gt;next = *head;
      *head = q;
  }
  </code></pre>
  
  <p>Full program is</p>
  
  <pre><code>    #include &lt;stdio.h&gt;
      #include &lt;stdlib.h&gt;
      #include &lt;assert.h&gt;
  
      typedef struct Queue Queue;
  
      struct Queue {
          int data;
          int priority;
          Queue *next;
      };
  
      Queue *queue_new(int d, int p)
      {
          Queue *n = malloc(sizeof(*n));
  
          n-&gt;data = d;
          n-&gt;priority = p;
          n-&gt;next = NULL;
  
          return n;
      }
  
      int queue_pop(Queue **head)
      {
          assert(*head);
  
          Queue *old = *head;
          int res = old-&gt;data;
  
          *head = (*head)-&gt;next;
          free(old);
  
          return res;
      }
  
      void queue_remove(Queue **head, int data)
      {
          while (*head &amp;&amp; (*head)-&gt;data != data) {
              head = &amp;(*head)-&gt;next;
          }
  
          if (*head) queue_pop(head);
      }
  
      void queue_push(Queue **head, int d, int p)
      {
          Queue *q = queue_new(d, p);
  
          while (*head &amp;&amp; (*head)-&gt;priority &lt; p) {
              head = &amp;(*head)-&gt;next;
          }
  
          q-&gt;next = *head;
          *head = q;
      }
  
  
      int queue_empty(Queue *head)
      {
          return (head == NULL);
      }
  
      void queue_print(const Queue *q)
      {
          while (q) {
              printf("%d[%d] ", q-&gt;data, q-&gt;priority);
              q = q-&gt;next;
          }
  
          puts("$");
      }
  
      typedef struct Graph Graph;
      typedef struct Edge Edge;
  
      struct Edge {
          int vertex;
          int weight;
          Edge *next;
      };
  
      struct Graph {
          int v;
          Edge **edge;
          int *dist;
          int *path;
      };
  
      Graph *graph_new(int v)
      {
          Graph *G = malloc(sizeof(*G));
  
          G-&gt;v = v;
          G-&gt;edge = calloc(v, sizeof(*G-&gt;edge));
          G-&gt;dist = calloc(v, sizeof(*G-&gt;dist));
          G-&gt;path = calloc(v, sizeof(*G-&gt;path));
  
          return G;
      }
  
      void graph_delete(Graph *G)
      {
          if (G) {
              for (int i = 0; i &lt; G-&gt;v; i++) {
                  Edge *e = G-&gt;edge[i];
  
                  while (e) {
                      Edge *old = e;
  
                      e = e-&gt;next;
                      free(old);
                  }
              }
  
              free(G-&gt;edge);
              free(G-&gt;dist);
              free(G-&gt;path);
              free(G);
          }
      }
  
      Edge *edge_new(int vertex, int weight, Edge *next)
      {
          Edge *e = malloc(sizeof(*e));
  
          e-&gt;vertex = vertex;
          e-&gt;weight = weight;
          e-&gt;next = next;
  
          return e;
      }
  
      void graph_edge(Graph *G, int u, int v, int w)
      {
          G-&gt;edge[u] = edge_new(v, w, G-&gt;edge[u]);
          G-&gt;edge[v] = edge_new(u, w, G-&gt;edge[v]);
      }
  
      void dijkstra(const Graph *G, int s)
      {
          Queue *queue = NULL;
  
          for (int i = 0; i &lt; G-&gt;v; i++) G-&gt;dist[i] = -1;
          G-&gt;dist[s] = 0;
  
          queue_push(&amp;queue, s, 0);
  
          while (!queue_empty(queue)) {
              int v = queue_pop(&amp;queue);
              Edge *e = G-&gt;edge[v];
  
              while (e) {
                  int w = e-&gt;vertex;
                  int d = G-&gt;dist[v] + e-&gt;weight;
  
                  if (G-&gt;dist[w] == -1) {
                      G-&gt;dist[w] = d;
                      G-&gt;path[w] = v;
  
                      queue_push(&amp;queue, w, d);
                  }
  
                  if (G-&gt;dist[w] &gt; d) {
                      G-&gt;dist[w] = d;
                      G-&gt;path[w] = v;
  
                      queue_remove(&amp;queue, w);
                      queue_push(&amp;queue, w, d);
                  }
  
                  e = e-&gt;next;
              }
          }
      }
  
      int main()
      {
          int t;
  
          scanf("%d", &amp;t);
  
          while (t--) {
              Graph *G;
              int v, e, s;
  
              scanf("%d %d", &amp;v, &amp;e);
  
              G = graph_new(v);
  
              for (int i = 0; i &lt; e; i++) {
                  int u, v, w;
  
                  scanf("%d %d %d", &amp;u, &amp;v, &amp;w);
                  graph_edge(G, u - 1, v - 1, w);
              }
  
              scanf("%d", &amp;s);
              dijkstra(G, s - 1);
  
              for (int i = 0; i &lt; G-&gt;v; i++) {
                  if (i != s - 1) {
                      printf("%d ", G-&gt;dist[i]);
                  }
              }
  
              puts("");
              graph_delete(G);
          }
  
          return 0;
      }
  </code></pre>