DSA
-
Sir, first code doesn't actually need to free something as it is not creating a node to perform operation rather doing the same thing using the front pointer directly. And yes I have wrote a function 'isFull' already
Quote:
first code doesn't actually need to free something as it is not creating a node
Neither the second one is. So the second code wrongly releases memory.
Quote:
And yes I have wrote a function 'isFull' already
But you should check for not-empty condition, with is NOT quite the same of is-full, is it?
"In testa che avete, Signor di Ceprano?" -- Rigoletto
-
Quote:
first code doesn't actually need to free something as it is not creating a node
Neither the second one is. So the second code wrongly releases memory.
Quote:
And yes I have wrote a function 'isFull' already
But you should check for not-empty condition, with is NOT quite the same of is-full, is it?
"In testa che avete, Signor di Ceprano?" -- Rigoletto
How is that possible sir! Second code is written by a renowned youtuber 'code_with_harry'. Okk, for your better understanding i'm pasting full source code here: #include #include struct Node *f = NULL; struct Node *r = NULL; struct Node{ int data; struct Node *next; }; void traversal(struct Node *f){ struct Node *ptr=f; printf("------------\n"); while(ptr!=NULL){ printf("%d ",ptr->data); ptr=ptr->next; } printf("\n------------\n"); } int isEmpty(struct Node *f){ if (f == NULL){ return 1; } return 0; } int isFull(struct Node *f){ struct Node *n = (struct Node *)malloc(sizeof(struct Node)); if (n == NULL){ return 1; } return 0; } struct Node *enqueue(int val){ if (isFull(f)){ printf("Queue Overflow! Can't Enqueue %d\n", val); return f; } else{ struct Node *n = (struct Node *)malloc(sizeof(struct Node)); n->data = val; n->next = NULL; if (isEmpty(f)){ f = r = n; } r->next = n; r = n; return f; } } // int dequeue(){ // if (isFull(f)){ // printf("Queue Underflow! Nothing to Dequeue"); // } // int dqd = f->data; // f = f->next; // return dqd; // } int dequeue(){ int val=-1; struct Node *ptr=f; if (f==NULL){ printf("Empty\n"); } else{ f=f->next; val=ptr->data; free(ptr); return val; } } int main(){ enqueue(11); enqueue(22); enqueue(33); enqueue(44); traversal(f); dequeue(); dequeue(); traversal(f); return 0; }
-
How is that possible sir! Second code is written by a renowned youtuber 'code_with_harry'. Okk, for your better understanding i'm pasting full source code here: #include #include struct Node *f = NULL; struct Node *r = NULL; struct Node{ int data; struct Node *next; }; void traversal(struct Node *f){ struct Node *ptr=f; printf("------------\n"); while(ptr!=NULL){ printf("%d ",ptr->data); ptr=ptr->next; } printf("\n------------\n"); } int isEmpty(struct Node *f){ if (f == NULL){ return 1; } return 0; } int isFull(struct Node *f){ struct Node *n = (struct Node *)malloc(sizeof(struct Node)); if (n == NULL){ return 1; } return 0; } struct Node *enqueue(int val){ if (isFull(f)){ printf("Queue Overflow! Can't Enqueue %d\n", val); return f; } else{ struct Node *n = (struct Node *)malloc(sizeof(struct Node)); n->data = val; n->next = NULL; if (isEmpty(f)){ f = r = n; } r->next = n; r = n; return f; } } // int dequeue(){ // if (isFull(f)){ // printf("Queue Underflow! Nothing to Dequeue"); // } // int dqd = f->data; // f = f->next; // return dqd; // } int dequeue(){ int val=-1; struct Node *ptr=f; if (f==NULL){ printf("Empty\n"); } else{ f=f->next; val=ptr->data; free(ptr); return val; } } int main(){ enqueue(11); enqueue(22); enqueue(33); enqueue(44); traversal(f); dequeue(); dequeue(); traversal(f); return 0; }
ZAID razvi wrote:
Second code is written by a renowned youtuber
At least 90% of YouTube coding tutorial videos aren't worth the paper they're printed on. And how do you know that this Harry is "renowned"? Is this something that other well-known, competent, and respected developers have said? Or is it an adjective he's applied to himself? Either way, if the code doesn't work, there are two options: either you've made a mistake in copying it, or he made a mistake in writing it.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
ZAID razvi wrote:
Second code is written by a renowned youtuber
At least 90% of YouTube coding tutorial videos aren't worth the paper they're printed on. And how do you know that this Harry is "renowned"? Is this something that other well-known, competent, and respected developers have said? Or is it an adjective he's applied to himself? Either way, if the code doesn't work, there are two options: either you've made a mistake in copying it, or he made a mistake in writing it.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
Okay sir! Got your point, please calm down 😊
-
How is that possible sir! Second code is written by a renowned youtuber 'code_with_harry'. Okk, for your better understanding i'm pasting full source code here: #include #include struct Node *f = NULL; struct Node *r = NULL; struct Node{ int data; struct Node *next; }; void traversal(struct Node *f){ struct Node *ptr=f; printf("------------\n"); while(ptr!=NULL){ printf("%d ",ptr->data); ptr=ptr->next; } printf("\n------------\n"); } int isEmpty(struct Node *f){ if (f == NULL){ return 1; } return 0; } int isFull(struct Node *f){ struct Node *n = (struct Node *)malloc(sizeof(struct Node)); if (n == NULL){ return 1; } return 0; } struct Node *enqueue(int val){ if (isFull(f)){ printf("Queue Overflow! Can't Enqueue %d\n", val); return f; } else{ struct Node *n = (struct Node *)malloc(sizeof(struct Node)); n->data = val; n->next = NULL; if (isEmpty(f)){ f = r = n; } r->next = n; r = n; return f; } } // int dequeue(){ // if (isFull(f)){ // printf("Queue Underflow! Nothing to Dequeue"); // } // int dqd = f->data; // f = f->next; // return dqd; // } int dequeue(){ int val=-1; struct Node *ptr=f; if (f==NULL){ printf("Empty\n"); } else{ f=f->next; val=ptr->data; free(ptr); return val; } } int main(){ enqueue(11); enqueue(22); enqueue(33); enqueue(44); traversal(f); dequeue(); dequeue(); traversal(f); return 0; }
-
How is that possible sir! Second code is written by a renowned youtuber 'code_with_harry'. Okk, for your better understanding i'm pasting full source code here: #include #include struct Node *f = NULL; struct Node *r = NULL; struct Node{ int data; struct Node *next; }; void traversal(struct Node *f){ struct Node *ptr=f; printf("------------\n"); while(ptr!=NULL){ printf("%d ",ptr->data); ptr=ptr->next; } printf("\n------------\n"); } int isEmpty(struct Node *f){ if (f == NULL){ return 1; } return 0; } int isFull(struct Node *f){ struct Node *n = (struct Node *)malloc(sizeof(struct Node)); if (n == NULL){ return 1; } return 0; } struct Node *enqueue(int val){ if (isFull(f)){ printf("Queue Overflow! Can't Enqueue %d\n", val); return f; } else{ struct Node *n = (struct Node *)malloc(sizeof(struct Node)); n->data = val; n->next = NULL; if (isEmpty(f)){ f = r = n; } r->next = n; r = n; return f; } } // int dequeue(){ // if (isFull(f)){ // printf("Queue Underflow! Nothing to Dequeue"); // } // int dqd = f->data; // f = f->next; // return dqd; // } int dequeue(){ int val=-1; struct Node *ptr=f; if (f==NULL){ printf("Empty\n"); } else{ f=f->next; val=ptr->data; free(ptr); return val; } } int main(){ enqueue(11); enqueue(22); enqueue(33); enqueue(44); traversal(f); dequeue(); dequeue(); traversal(f); return 0; }
Harry is "renowned"? Prolific YouTube creator, maybe, but not renowned. A 125 video series on "How to become a job ready programmer in 3 months"? BWAHAHAHAHA! That's bullshit, and everybody in this field knows it. He's got a shit-ton of self-promotion marketing, but that doesn't mean he can teach.
Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
Dave Kreskowiak -
ZAID razvi wrote:
Second code is written by a renowned youtuber
At least 90% of YouTube coding tutorial videos aren't worth the paper they're printed on. And how do you know that this Harry is "renowned"? Is this something that other well-known, competent, and respected developers have said? Or is it an adjective he's applied to himself? Either way, if the code doesn't work, there are two options: either you've made a mistake in copying it, or he made a mistake in writing it.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
Quote:
ouTube coding tutorial videos aren't worth the paper they're printed on.
Or the screen they are presented on.
Quote:
either you've made a mistake in copying it, or he made a mistake in writing it
Indeed, back to the drawing board.
-
Quote:
ouTube coding tutorial videos aren't worth the paper they're printed on.
Or the screen they are presented on.
Quote:
either you've made a mistake in copying it, or he made a mistake in writing it
Indeed, back to the drawing board.
Andre Oosthuizen wrote:
Or the screen they are presented on.
That's the point - the screen they're presented on is worth much more than the paper they're (not) printed on. :-D
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
Harry is "renowned"? Prolific YouTube creator, maybe, but not renowned. A 125 video series on "How to become a job ready programmer in 3 months"? BWAHAHAHAHA! That's bullshit, and everybody in this field knows it. He's got a shit-ton of self-promotion marketing, but that doesn't mean he can teach.
Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
Dave KreskowiakDave Kreskowiak wrote:
that doesn't mean he can teach
Or even write code, from the sounds of it. :laugh:
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
How is that possible sir! Second code is written by a renowned youtuber 'code_with_harry'. Okk, for your better understanding i'm pasting full source code here: #include #include struct Node *f = NULL; struct Node *r = NULL; struct Node{ int data; struct Node *next; }; void traversal(struct Node *f){ struct Node *ptr=f; printf("------------\n"); while(ptr!=NULL){ printf("%d ",ptr->data); ptr=ptr->next; } printf("\n------------\n"); } int isEmpty(struct Node *f){ if (f == NULL){ return 1; } return 0; } int isFull(struct Node *f){ struct Node *n = (struct Node *)malloc(sizeof(struct Node)); if (n == NULL){ return 1; } return 0; } struct Node *enqueue(int val){ if (isFull(f)){ printf("Queue Overflow! Can't Enqueue %d\n", val); return f; } else{ struct Node *n = (struct Node *)malloc(sizeof(struct Node)); n->data = val; n->next = NULL; if (isEmpty(f)){ f = r = n; } r->next = n; r = n; return f; } } // int dequeue(){ // if (isFull(f)){ // printf("Queue Underflow! Nothing to Dequeue"); // } // int dqd = f->data; // f = f->next; // return dqd; // } int dequeue(){ int val=-1; struct Node *ptr=f; if (f==NULL){ printf("Empty\n"); } else{ f=f->next; val=ptr->data; free(ptr); return val; } } int main(){ enqueue(11); enqueue(22); enqueue(33); enqueue(44); traversal(f); dequeue(); dequeue(); traversal(f); return 0; }
Quote:
How is that possible sir! Second code is written by a renowned youtuber 'code_with_harry'.
I know. But here is renowed expert CPallini :-D. If you run Harry's code using Valgrind, then you get (something similar to)
==3144== LEAK SUMMARY:
==3144== definitely lost: 64 bytes in 4 blocks
==3144== indirectly lost: 0 bytes in 0 blocks
==3144== possibly lost: 0 bytes in 0 blocks
==3144== still reachable: 0 bytes in 0 blocks
==3144== suppressed: 0 bytes in 0 blocks
==3144==
==3144== For lists of detected and suppressed errors, rerun with: -s
==3144== ERROR SUMMARY: 4 errors from 4 contexts (suppressed: 0 from 0)On the other hand, if you run the following, alternative code
#include
#includestruct Node
{
int val;
struct Node * next;
};void traversal(const struct Node * p)
{
printf("------------\n");
while (p)
{
printf("%d ",p->val);
p = p->next;
}
printf("\n------------\n");
}// isEmpty is not strictly needed
// isFull is meaning-lessstruct Node * enqueue(struct Node *p, int val)
{struct Node * prev = NULL;
while (p)
{
prev = p;
p = p->next;
}
p = malloc(sizeof(struct Node ));
if ( p )
{
p->val = val;
p->next = NULL;
if ( prev )
prev->next = p;
}
return p;
}int dequeue(struct Node ** pp)
{
int val = -1;
struct Node * p = *pp;
if ( p )
{
val = p->val;
*pp = p->next;
free(p);
}
return val;
}int main()
{
struct Node * ph = enqueue(NULL, 11);
enqueue(ph, 22);
enqueue(ph, 33);
enqueue(ph, 44);
traversal(ph);
dequeue(&ph);
dequeue(&ph);
traversal(ph);
dequeue(&ph);
dequeue(&ph);
return 0;
}then Valgrind is somewhat happier:
==3153== HEAP SUMMARY:
==3153== in use at exit: 0 bytes in 0 blocks
==3153== total heap usage: 5 allocs, 5 frees, 1,088 bytes allocated
==3153==
==3153== All heap blocks were freed -- no leaks are possible
==3153==
==3153== For lists of detected and suppressed errors, rerun with: -s
==3153== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)"In testa che avete, Signor di Ceprano?" -- Rigoletto