double pointer to a node












2















My doubt is regarding pointer only,Here head is a double pointer to Queue if we are using *head than we are accessing the location(or address passed) inside main but when we are using simply head than we are using heading in the current function only which will hold the address of pointer to Queue now when we are doing this head=&(*head)->next the since (*head)->next is itself a address and when we use & 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 head 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



void queue_push(Queue **head, int d, int p)
{
Queue *q = queue_new(d, p);

while (*head && (*head)->priority < p) {
head = &(*head)->next;
}

q->next = *head;
*head = q;
}


Full program is



    #include <stdio.h>
#include <stdlib.h>
#include <assert.h>

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->data = d;
n->priority = p;
n->next = NULL;

return n;
}

int queue_pop(Queue **head)
{
assert(*head);

Queue *old = *head;
int res = old->data;

*head = (*head)->next;
free(old);

return res;
}

void queue_remove(Queue **head, int data)
{
while (*head && (*head)->data != data) {
head = &(*head)->next;
}

if (*head) queue_pop(head);
}

void queue_push(Queue **head, int d, int p)
{
Queue *q = queue_new(d, p);

while (*head && (*head)->priority < p) {
head = &(*head)->next;
}

q->next = *head;
*head = q;
}


int queue_empty(Queue *head)
{
return (head == NULL);
}

void queue_print(const Queue *q)
{
while (q) {
printf("%d[%d] ", q->data, q->priority);
q = q->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->v = v;
G->edge = calloc(v, sizeof(*G->edge));
G->dist = calloc(v, sizeof(*G->dist));
G->path = calloc(v, sizeof(*G->path));

return G;
}

void graph_delete(Graph *G)
{
if (G) {
for (int i = 0; i < G->v; i++) {
Edge *e = G->edge[i];

while (e) {
Edge *old = e;

e = e->next;
free(old);
}
}

free(G->edge);
free(G->dist);
free(G->path);
free(G);
}
}

Edge *edge_new(int vertex, int weight, Edge *next)
{
Edge *e = malloc(sizeof(*e));

e->vertex = vertex;
e->weight = weight;
e->next = next;

return e;
}

void graph_edge(Graph *G, int u, int v, int w)
{
G->edge[u] = edge_new(v, w, G->edge[u]);
G->edge[v] = edge_new(u, w, G->edge[v]);
}

void dijkstra(const Graph *G, int s)
{
Queue *queue = NULL;

for (int i = 0; i < G->v; i++) G->dist[i] = -1;
G->dist[s] = 0;

queue_push(&queue, s, 0);

while (!queue_empty(queue)) {
int v = queue_pop(&queue);
Edge *e = G->edge[v];

while (e) {
int w = e->vertex;
int d = G->dist[v] + e->weight;

if (G->dist[w] == -1) {
G->dist[w] = d;
G->path[w] = v;

queue_push(&queue, w, d);
}

if (G->dist[w] > d) {
G->dist[w] = d;
G->path[w] = v;

queue_remove(&queue, w);
queue_push(&queue, w, d);
}

e = e->next;
}
}
}

int main()
{
int t;

scanf("%d", &t);

while (t--) {
Graph *G;
int v, e, s;

scanf("%d %d", &v, &e);

G = graph_new(v);

for (int i = 0; i < e; i++) {
int u, v, w;

scanf("%d %d %d", &u, &v, &w);
graph_edge(G, u - 1, v - 1, w);
}

scanf("%d", &s);
dijkstra(G, s - 1);

for (int i = 0; i < G->v; i++) {
if (i != s - 1) {
printf("%d ", G->dist[i]);
}
}

puts("");
graph_delete(G);
}

return 0;
}









share|improve this question























  • while (*head && (*head)->priority < p) { head = &(*head)->next; } should walk the list, not change head.

    – chux
    Nov 16 '18 at 18:46











  • what will be the address ` &(*head)->next` how computer generate address of address without storing the (*head)->next anywhere there should be some memory block where we can store the address of (*head)->next and now the address of that memory block will be assigned to head if this happens then whenever loop runs then computer will store the address of all intermediates in same block or different memory block

    – user10628441
    Nov 16 '18 at 18:55











  • Nice use of sizeof(*G) in G = malloc(sizeof(*G)) rather than malloc(sizeof(Graph)).

    – chux
    Nov 16 '18 at 19:17
















2















My doubt is regarding pointer only,Here head is a double pointer to Queue if we are using *head than we are accessing the location(or address passed) inside main but when we are using simply head than we are using heading in the current function only which will hold the address of pointer to Queue now when we are doing this head=&(*head)->next the since (*head)->next is itself a address and when we use & 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 head 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



void queue_push(Queue **head, int d, int p)
{
Queue *q = queue_new(d, p);

while (*head && (*head)->priority < p) {
head = &(*head)->next;
}

q->next = *head;
*head = q;
}


Full program is



    #include <stdio.h>
#include <stdlib.h>
#include <assert.h>

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->data = d;
n->priority = p;
n->next = NULL;

return n;
}

int queue_pop(Queue **head)
{
assert(*head);

Queue *old = *head;
int res = old->data;

*head = (*head)->next;
free(old);

return res;
}

void queue_remove(Queue **head, int data)
{
while (*head && (*head)->data != data) {
head = &(*head)->next;
}

if (*head) queue_pop(head);
}

void queue_push(Queue **head, int d, int p)
{
Queue *q = queue_new(d, p);

while (*head && (*head)->priority < p) {
head = &(*head)->next;
}

q->next = *head;
*head = q;
}


int queue_empty(Queue *head)
{
return (head == NULL);
}

void queue_print(const Queue *q)
{
while (q) {
printf("%d[%d] ", q->data, q->priority);
q = q->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->v = v;
G->edge = calloc(v, sizeof(*G->edge));
G->dist = calloc(v, sizeof(*G->dist));
G->path = calloc(v, sizeof(*G->path));

return G;
}

void graph_delete(Graph *G)
{
if (G) {
for (int i = 0; i < G->v; i++) {
Edge *e = G->edge[i];

while (e) {
Edge *old = e;

e = e->next;
free(old);
}
}

free(G->edge);
free(G->dist);
free(G->path);
free(G);
}
}

Edge *edge_new(int vertex, int weight, Edge *next)
{
Edge *e = malloc(sizeof(*e));

e->vertex = vertex;
e->weight = weight;
e->next = next;

return e;
}

void graph_edge(Graph *G, int u, int v, int w)
{
G->edge[u] = edge_new(v, w, G->edge[u]);
G->edge[v] = edge_new(u, w, G->edge[v]);
}

void dijkstra(const Graph *G, int s)
{
Queue *queue = NULL;

for (int i = 0; i < G->v; i++) G->dist[i] = -1;
G->dist[s] = 0;

queue_push(&queue, s, 0);

while (!queue_empty(queue)) {
int v = queue_pop(&queue);
Edge *e = G->edge[v];

while (e) {
int w = e->vertex;
int d = G->dist[v] + e->weight;

if (G->dist[w] == -1) {
G->dist[w] = d;
G->path[w] = v;

queue_push(&queue, w, d);
}

if (G->dist[w] > d) {
G->dist[w] = d;
G->path[w] = v;

queue_remove(&queue, w);
queue_push(&queue, w, d);
}

e = e->next;
}
}
}

int main()
{
int t;

scanf("%d", &t);

while (t--) {
Graph *G;
int v, e, s;

scanf("%d %d", &v, &e);

G = graph_new(v);

for (int i = 0; i < e; i++) {
int u, v, w;

scanf("%d %d %d", &u, &v, &w);
graph_edge(G, u - 1, v - 1, w);
}

scanf("%d", &s);
dijkstra(G, s - 1);

for (int i = 0; i < G->v; i++) {
if (i != s - 1) {
printf("%d ", G->dist[i]);
}
}

puts("");
graph_delete(G);
}

return 0;
}









share|improve this question























  • while (*head && (*head)->priority < p) { head = &(*head)->next; } should walk the list, not change head.

    – chux
    Nov 16 '18 at 18:46











  • what will be the address ` &(*head)->next` how computer generate address of address without storing the (*head)->next anywhere there should be some memory block where we can store the address of (*head)->next and now the address of that memory block will be assigned to head if this happens then whenever loop runs then computer will store the address of all intermediates in same block or different memory block

    – user10628441
    Nov 16 '18 at 18:55











  • Nice use of sizeof(*G) in G = malloc(sizeof(*G)) rather than malloc(sizeof(Graph)).

    – chux
    Nov 16 '18 at 19:17














2












2








2








My doubt is regarding pointer only,Here head is a double pointer to Queue if we are using *head than we are accessing the location(or address passed) inside main but when we are using simply head than we are using heading in the current function only which will hold the address of pointer to Queue now when we are doing this head=&(*head)->next the since (*head)->next is itself a address and when we use & 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 head 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



void queue_push(Queue **head, int d, int p)
{
Queue *q = queue_new(d, p);

while (*head && (*head)->priority < p) {
head = &(*head)->next;
}

q->next = *head;
*head = q;
}


Full program is



    #include <stdio.h>
#include <stdlib.h>
#include <assert.h>

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->data = d;
n->priority = p;
n->next = NULL;

return n;
}

int queue_pop(Queue **head)
{
assert(*head);

Queue *old = *head;
int res = old->data;

*head = (*head)->next;
free(old);

return res;
}

void queue_remove(Queue **head, int data)
{
while (*head && (*head)->data != data) {
head = &(*head)->next;
}

if (*head) queue_pop(head);
}

void queue_push(Queue **head, int d, int p)
{
Queue *q = queue_new(d, p);

while (*head && (*head)->priority < p) {
head = &(*head)->next;
}

q->next = *head;
*head = q;
}


int queue_empty(Queue *head)
{
return (head == NULL);
}

void queue_print(const Queue *q)
{
while (q) {
printf("%d[%d] ", q->data, q->priority);
q = q->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->v = v;
G->edge = calloc(v, sizeof(*G->edge));
G->dist = calloc(v, sizeof(*G->dist));
G->path = calloc(v, sizeof(*G->path));

return G;
}

void graph_delete(Graph *G)
{
if (G) {
for (int i = 0; i < G->v; i++) {
Edge *e = G->edge[i];

while (e) {
Edge *old = e;

e = e->next;
free(old);
}
}

free(G->edge);
free(G->dist);
free(G->path);
free(G);
}
}

Edge *edge_new(int vertex, int weight, Edge *next)
{
Edge *e = malloc(sizeof(*e));

e->vertex = vertex;
e->weight = weight;
e->next = next;

return e;
}

void graph_edge(Graph *G, int u, int v, int w)
{
G->edge[u] = edge_new(v, w, G->edge[u]);
G->edge[v] = edge_new(u, w, G->edge[v]);
}

void dijkstra(const Graph *G, int s)
{
Queue *queue = NULL;

for (int i = 0; i < G->v; i++) G->dist[i] = -1;
G->dist[s] = 0;

queue_push(&queue, s, 0);

while (!queue_empty(queue)) {
int v = queue_pop(&queue);
Edge *e = G->edge[v];

while (e) {
int w = e->vertex;
int d = G->dist[v] + e->weight;

if (G->dist[w] == -1) {
G->dist[w] = d;
G->path[w] = v;

queue_push(&queue, w, d);
}

if (G->dist[w] > d) {
G->dist[w] = d;
G->path[w] = v;

queue_remove(&queue, w);
queue_push(&queue, w, d);
}

e = e->next;
}
}
}

int main()
{
int t;

scanf("%d", &t);

while (t--) {
Graph *G;
int v, e, s;

scanf("%d %d", &v, &e);

G = graph_new(v);

for (int i = 0; i < e; i++) {
int u, v, w;

scanf("%d %d %d", &u, &v, &w);
graph_edge(G, u - 1, v - 1, w);
}

scanf("%d", &s);
dijkstra(G, s - 1);

for (int i = 0; i < G->v; i++) {
if (i != s - 1) {
printf("%d ", G->dist[i]);
}
}

puts("");
graph_delete(G);
}

return 0;
}









share|improve this question














My doubt is regarding pointer only,Here head is a double pointer to Queue if we are using *head than we are accessing the location(or address passed) inside main but when we are using simply head than we are using heading in the current function only which will hold the address of pointer to Queue now when we are doing this head=&(*head)->next the since (*head)->next is itself a address and when we use & 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 head 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



void queue_push(Queue **head, int d, int p)
{
Queue *q = queue_new(d, p);

while (*head && (*head)->priority < p) {
head = &(*head)->next;
}

q->next = *head;
*head = q;
}


Full program is



    #include <stdio.h>
#include <stdlib.h>
#include <assert.h>

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->data = d;
n->priority = p;
n->next = NULL;

return n;
}

int queue_pop(Queue **head)
{
assert(*head);

Queue *old = *head;
int res = old->data;

*head = (*head)->next;
free(old);

return res;
}

void queue_remove(Queue **head, int data)
{
while (*head && (*head)->data != data) {
head = &(*head)->next;
}

if (*head) queue_pop(head);
}

void queue_push(Queue **head, int d, int p)
{
Queue *q = queue_new(d, p);

while (*head && (*head)->priority < p) {
head = &(*head)->next;
}

q->next = *head;
*head = q;
}


int queue_empty(Queue *head)
{
return (head == NULL);
}

void queue_print(const Queue *q)
{
while (q) {
printf("%d[%d] ", q->data, q->priority);
q = q->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->v = v;
G->edge = calloc(v, sizeof(*G->edge));
G->dist = calloc(v, sizeof(*G->dist));
G->path = calloc(v, sizeof(*G->path));

return G;
}

void graph_delete(Graph *G)
{
if (G) {
for (int i = 0; i < G->v; i++) {
Edge *e = G->edge[i];

while (e) {
Edge *old = e;

e = e->next;
free(old);
}
}

free(G->edge);
free(G->dist);
free(G->path);
free(G);
}
}

Edge *edge_new(int vertex, int weight, Edge *next)
{
Edge *e = malloc(sizeof(*e));

e->vertex = vertex;
e->weight = weight;
e->next = next;

return e;
}

void graph_edge(Graph *G, int u, int v, int w)
{
G->edge[u] = edge_new(v, w, G->edge[u]);
G->edge[v] = edge_new(u, w, G->edge[v]);
}

void dijkstra(const Graph *G, int s)
{
Queue *queue = NULL;

for (int i = 0; i < G->v; i++) G->dist[i] = -1;
G->dist[s] = 0;

queue_push(&queue, s, 0);

while (!queue_empty(queue)) {
int v = queue_pop(&queue);
Edge *e = G->edge[v];

while (e) {
int w = e->vertex;
int d = G->dist[v] + e->weight;

if (G->dist[w] == -1) {
G->dist[w] = d;
G->path[w] = v;

queue_push(&queue, w, d);
}

if (G->dist[w] > d) {
G->dist[w] = d;
G->path[w] = v;

queue_remove(&queue, w);
queue_push(&queue, w, d);
}

e = e->next;
}
}
}

int main()
{
int t;

scanf("%d", &t);

while (t--) {
Graph *G;
int v, e, s;

scanf("%d %d", &v, &e);

G = graph_new(v);

for (int i = 0; i < e; i++) {
int u, v, w;

scanf("%d %d %d", &u, &v, &w);
graph_edge(G, u - 1, v - 1, w);
}

scanf("%d", &s);
dijkstra(G, s - 1);

for (int i = 0; i < G->v; i++) {
if (i != s - 1) {
printf("%d ", G->dist[i]);
}
}

puts("");
graph_delete(G);
}

return 0;
}






c pointers memory-management






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 16 '18 at 18:10







user10628441




















  • while (*head && (*head)->priority < p) { head = &(*head)->next; } should walk the list, not change head.

    – chux
    Nov 16 '18 at 18:46











  • what will be the address ` &(*head)->next` how computer generate address of address without storing the (*head)->next anywhere there should be some memory block where we can store the address of (*head)->next and now the address of that memory block will be assigned to head if this happens then whenever loop runs then computer will store the address of all intermediates in same block or different memory block

    – user10628441
    Nov 16 '18 at 18:55











  • Nice use of sizeof(*G) in G = malloc(sizeof(*G)) rather than malloc(sizeof(Graph)).

    – chux
    Nov 16 '18 at 19:17



















  • while (*head && (*head)->priority < p) { head = &(*head)->next; } should walk the list, not change head.

    – chux
    Nov 16 '18 at 18:46











  • what will be the address ` &(*head)->next` how computer generate address of address without storing the (*head)->next anywhere there should be some memory block where we can store the address of (*head)->next and now the address of that memory block will be assigned to head if this happens then whenever loop runs then computer will store the address of all intermediates in same block or different memory block

    – user10628441
    Nov 16 '18 at 18:55











  • Nice use of sizeof(*G) in G = malloc(sizeof(*G)) rather than malloc(sizeof(Graph)).

    – chux
    Nov 16 '18 at 19:17

















while (*head && (*head)->priority < p) { head = &(*head)->next; } should walk the list, not change head.

– chux
Nov 16 '18 at 18:46





while (*head && (*head)->priority < p) { head = &(*head)->next; } should walk the list, not change head.

– chux
Nov 16 '18 at 18:46













what will be the address ` &(*head)->next` how computer generate address of address without storing the (*head)->next anywhere there should be some memory block where we can store the address of (*head)->next and now the address of that memory block will be assigned to head if this happens then whenever loop runs then computer will store the address of all intermediates in same block or different memory block

– user10628441
Nov 16 '18 at 18:55





what will be the address ` &(*head)->next` how computer generate address of address without storing the (*head)->next anywhere there should be some memory block where we can store the address of (*head)->next and now the address of that memory block will be assigned to head if this happens then whenever loop runs then computer will store the address of all intermediates in same block or different memory block

– user10628441
Nov 16 '18 at 18:55













Nice use of sizeof(*G) in G = malloc(sizeof(*G)) rather than malloc(sizeof(Graph)).

– chux
Nov 16 '18 at 19:17





Nice use of sizeof(*G) in G = malloc(sizeof(*G)) rather than malloc(sizeof(Graph)).

– chux
Nov 16 '18 at 19:17












1 Answer
1






active

oldest

votes


















0














queue_push() fails to insert a new node correctly.



Only one node's .next is updated. Usually 2 nodes need their .next member updated. One in the original list (previous to the new node's location) and the new one.





I find creating a temporary node before the list, superhead, simplifies code.



void queue_push(Queue **head, int d, int p) {
Queue *q = queue_new(d, p);

Queue superhead; // Only superhead.next member important.
superhead.next = *head;
Queue *previous = &superhead;

while (previous->next && previous->next->priority < p) {
previous = previous->next;
}

q->next = previous->next;
previous->next = q;
*head = superhead.next;
}





share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53343267%2fdouble-pointer-to-a-node%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown
























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    queue_push() fails to insert a new node correctly.



    Only one node's .next is updated. Usually 2 nodes need their .next member updated. One in the original list (previous to the new node's location) and the new one.





    I find creating a temporary node before the list, superhead, simplifies code.



    void queue_push(Queue **head, int d, int p) {
    Queue *q = queue_new(d, p);

    Queue superhead; // Only superhead.next member important.
    superhead.next = *head;
    Queue *previous = &superhead;

    while (previous->next && previous->next->priority < p) {
    previous = previous->next;
    }

    q->next = previous->next;
    previous->next = q;
    *head = superhead.next;
    }





    share|improve this answer






























      0














      queue_push() fails to insert a new node correctly.



      Only one node's .next is updated. Usually 2 nodes need their .next member updated. One in the original list (previous to the new node's location) and the new one.





      I find creating a temporary node before the list, superhead, simplifies code.



      void queue_push(Queue **head, int d, int p) {
      Queue *q = queue_new(d, p);

      Queue superhead; // Only superhead.next member important.
      superhead.next = *head;
      Queue *previous = &superhead;

      while (previous->next && previous->next->priority < p) {
      previous = previous->next;
      }

      q->next = previous->next;
      previous->next = q;
      *head = superhead.next;
      }





      share|improve this answer




























        0












        0








        0







        queue_push() fails to insert a new node correctly.



        Only one node's .next is updated. Usually 2 nodes need their .next member updated. One in the original list (previous to the new node's location) and the new one.





        I find creating a temporary node before the list, superhead, simplifies code.



        void queue_push(Queue **head, int d, int p) {
        Queue *q = queue_new(d, p);

        Queue superhead; // Only superhead.next member important.
        superhead.next = *head;
        Queue *previous = &superhead;

        while (previous->next && previous->next->priority < p) {
        previous = previous->next;
        }

        q->next = previous->next;
        previous->next = q;
        *head = superhead.next;
        }





        share|improve this answer















        queue_push() fails to insert a new node correctly.



        Only one node's .next is updated. Usually 2 nodes need their .next member updated. One in the original list (previous to the new node's location) and the new one.





        I find creating a temporary node before the list, superhead, simplifies code.



        void queue_push(Queue **head, int d, int p) {
        Queue *q = queue_new(d, p);

        Queue superhead; // Only superhead.next member important.
        superhead.next = *head;
        Queue *previous = &superhead;

        while (previous->next && previous->next->priority < p) {
        previous = previous->next;
        }

        q->next = previous->next;
        previous->next = q;
        *head = superhead.next;
        }






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 16 '18 at 19:20

























        answered Nov 16 '18 at 19:14









        chuxchux

        81.4k871148




        81.4k871148






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53343267%2fdouble-pointer-to-a-node%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            鏡平學校

            ꓛꓣだゔៀៅຸ໢ທຮ໕໒ ,ໂ'໥໓າ໼ឨឲ៵៭ៈゎゔit''䖳𥁄卿' ☨₤₨こゎもょの;ꜹꟚꞖꞵꟅꞛေၦေɯ,ɨɡ𛃵𛁹ޝ޳ޠ޾,ޤޒޯ޾𫝒𫠁သ𛅤チョ'サノބޘދ𛁐ᶿᶇᶀᶋᶠ㨑㽹⻮ꧬ꧹؍۩وَؠ㇕㇃㇪ ㇦㇋㇋ṜẰᵡᴠ 軌ᵕ搜۳ٰޗޮ޷ސޯ𫖾𫅀ल, ꙭ꙰ꚅꙁꚊꞻꝔ꟠Ꝭㄤﺟޱސꧨꧼ꧴ꧯꧽ꧲ꧯ'⽹⽭⾁⿞⼳⽋២៩ញណើꩯꩤ꩸ꩮᶻᶺᶧᶂ𫳲𫪭𬸄𫵰𬖩𬫣𬊉ၲ𛅬㕦䬺𫝌𫝼,,𫟖𫞽ហៅ஫㆔ాఆఅꙒꚞꙍ,Ꙟ꙱エ ,ポテ,フࢰࢯ𫟠𫞶 𫝤𫟠ﺕﹱﻜﻣ𪵕𪭸𪻆𪾩𫔷ġ,ŧآꞪ꟥,ꞔꝻ♚☹⛵𛀌ꬷꭞȄƁƪƬșƦǙǗdžƝǯǧⱦⱰꓕꓢႋ神 ဴ၀க௭எ௫ឫោ ' េㇷㇴㇼ神ㇸㇲㇽㇴㇼㇻㇸ'ㇸㇿㇸㇹㇰㆣꓚꓤ₡₧ ㄨㄟ㄂ㄖㄎ໗ツڒذ₶।ऩछएोञयूटक़कयँृी,冬'𛅢𛅥ㇱㇵㇶ𥄥𦒽𠣧𠊓𧢖𥞘𩔋цѰㄠſtʯʭɿʆʗʍʩɷɛ,əʏダヵㄐㄘR{gỚṖḺờṠṫảḙḭᴮᵏᴘᵀᵷᵕᴜᴏᵾq﮲ﲿﴽﭙ軌ﰬﶚﶧ﫲Ҝжюїкӈㇴffצּ﬘﭅﬈軌'ffistfflſtffतभफɳɰʊɲʎ𛁱𛁖𛁮𛀉 𛂯𛀞నఋŀŲ 𫟲𫠖𫞺ຆຆ ໹້໕໗ๆทԊꧢꧠ꧰ꓱ⿝⼑ŎḬẃẖỐẅ ,ờỰỈỗﮊDžȩꭏꭎꬻ꭮ꬿꭖꭥꭅ㇭神 ⾈ꓵꓑ⺄㄄ㄪㄙㄅㄇstA۵䞽ॶ𫞑𫝄㇉㇇゜軌𩜛𩳠Jﻺ‚Üမ႕ႌႊၐၸဓၞၞၡ៸wyvtᶎᶪᶹစဎ꣡꣰꣢꣤ٗ؋لㇳㇾㇻㇱ㆐㆔,,㆟Ⱶヤマފ޼ޝަݿݞݠݷݐ',ݘ,ݪݙݵ𬝉𬜁𫝨𫞘くせぉて¼óû×ó£…𛅑הㄙくԗԀ5606神45,神796'𪤻𫞧ꓐ㄁ㄘɥɺꓵꓲ3''7034׉ⱦⱠˆ“𫝋ȍ,ꩲ軌꩷ꩶꩧꩫఞ۔فڱێظペサ神ナᴦᵑ47 9238їﻂ䐊䔉㠸﬎ffiﬣ,לּᴷᴦᵛᵽ,ᴨᵤ ᵸᵥᴗᵈꚏꚉꚟ⻆rtǟƴ𬎎

            Why https connections are so slow when debugging (stepping over) in Java?