Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. DSA

DSA

Scheduled Pinned Locked Moved C / C++ / MFC
data-structures
13 Posts 6 Posters 51 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • CPalliniC CPallini

    Well... The second one releases memory (while the first one does NOT relaase memory). Moreover, in the first one, the

    if (isFull(f)){

    line looks suspicious.

    "In testa che avete, Signor di Ceprano?" -- Rigoletto

    Z Offline
    Z Offline
    ZAID razvi
    wrote on last edited by
    #3

    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

    CPalliniC 1 Reply Last reply
    0
    • Z ZAID razvi

      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

      CPalliniC Offline
      CPalliniC Offline
      CPallini
      wrote on last edited by
      #4

      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

      In testa che avete, signor di Ceprano?

      Z 1 Reply Last reply
      0
      • CPalliniC CPallini

        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

        Z Offline
        Z Offline
        ZAID razvi
        wrote on last edited by
        #5

        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; }

        Richard DeemingR L D CPalliniC 4 Replies Last reply
        0
        • Z ZAID razvi

          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; }

          Richard DeemingR Offline
          Richard DeemingR Offline
          Richard Deeming
          wrote on last edited by
          #6

          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

          "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

          Z A 2 Replies Last reply
          0
          • Richard DeemingR Richard Deeming

            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

            Z Offline
            Z Offline
            ZAID razvi
            wrote on last edited by
            #7

            Okay sir! Got your point, please calm down 😊

            1 Reply Last reply
            0
            • Z ZAID razvi

              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; }

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #8

              ZAID razvi wrote:

              written by a renowned youtuber 'code_with_harry'.

              Believe that and you will believe anything. And if that is an example of his output I would say he is renowned for writing poor code.

              1 Reply Last reply
              0
              • Z ZAID razvi

                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; }

                D Offline
                D Offline
                Dave Kreskowiak
                wrote on last edited by
                #9

                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

                Richard DeemingR 1 Reply Last reply
                0
                • Richard DeemingR Richard Deeming

                  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

                  A Offline
                  A Offline
                  Andre Oosthuizen
                  wrote on last edited by
                  #10

                  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.

                  Richard DeemingR 1 Reply Last reply
                  0
                  • A Andre Oosthuizen

                    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.

                    Richard DeemingR Offline
                    Richard DeemingR Offline
                    Richard Deeming
                    wrote on last edited by
                    #11

                    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

                    "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                    1 Reply Last reply
                    0
                    • D Dave Kreskowiak

                      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

                      Richard DeemingR Offline
                      Richard DeemingR Offline
                      Richard Deeming
                      wrote on last edited by
                      #12

                      Dave 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

                      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                      1 Reply Last reply
                      0
                      • Z ZAID razvi

                        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; }

                        CPalliniC Offline
                        CPalliniC Offline
                        CPallini
                        wrote on last edited by
                        #13

                        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
                        #include

                        struct 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-less

                        struct 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

                        In testa che avete, signor di Ceprano?

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups