Thread: issue with linked list !!IMPORTANT

  1. #31
    Registered User
    Join Date
    Apr 2013
    Location
    Gurgaon, Haryana, India
    Posts
    41
    my new code looks like this
    Code:
    #include<stdio.h>#include<strings.h>
    #include<stdlib.h>
    #include<malloc.h>
    
    
    struct node
    {
        int data;
        struct node *next;
    };
    
    
    typedef struct node link;
    
    
    link create_link(link *r);
    
    
    int main()
    {
        link *trys,*k;
        int i,j,num;
    
    
        trys=malloc(sizeof(link));
        k=trys;
        printf("Enter the Number  data\n");
        scanf("%d",&num);
        trys->data=num;
        trys->next=NULL;
        p: printf("\nDo you want to add more numbers?(Y=1/N=0)");
        scanf("%d",&i);
        if(i==1)
        {
            for(j=0;trys->next!=NULL;j++)
                    trys=trys->next;
            create_link(trys);
            {
                trys=k;
                for(j=0;trys->next!=NULL;j++)
                    trys=trys->next;
                printf("\nEnter the next Number data\n");
                scanf("%d",&num);
                trys->data=num;
            }
            trys=k;
            goto p;
        }
        else
        {
            printf("\nNumber dat entered by you is:\n");
            for(j=0;trys->next==NULL;j++)
            {
                printf("%d\n",trys->data);
            }
        }
        return 0;
    }
    
    
    link create_link(link *r)
    {
        link *addon;
    
    
        addon=malloc(sizeof(link));
        r->next=addon;
        addon->next=NULL;
        return *r;
    }

  2. #32
    Registered User
    Join Date
    Jun 2013
    Posts
    6
    no no, != is correct, i seen your very first program and replied.

  3. #33
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    This should be removed:
    Code:
    #include<malloc.h>
    You should change the use of goto to be a loop.

    You should free what you malloc.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #34
    Registered User
    Join Date
    Apr 2013
    Location
    Gurgaon, Haryana, India
    Posts
    41
    @pappu hmm u r correct dude @laserlight will goto create a kind of error *why to remove malloc.h?
    plus this is my current out put *(not able to print the values)
    Enter the Number data34


    Do you want to add more numbers?(Y=1/N=0)1


    Enter the next Number data
    23


    Do you want to add more numbers?(Y=1/N=0)1


    Enter the next Number data
    234


    Do you want to add more numbers?(Y=1/N=0)0


    Number dat entered by you is:


    Process returned 0 (0x0) execution time : 13.140 s
    Press any key to continue.

  5. #35
    Registered User
    Join Date
    Apr 2013
    Location
    Gurgaon, Haryana, India
    Posts
    41
    **if i am using != in print i am getting an infinite loop print of my first value entered.

  6. #36
    Registered User
    Join Date
    Jun 2013
    Posts
    6
    This happens due to you have changed the trys to point its last node, its better to keep a head node that should not be moved.


    Quote Originally Posted by Amitesh93 View Post
    my new code looks like this
    Code:
    #include<stdio.h>#include<strings.h>
    
    #include<stdio.h>
    #include<strings.h>
    #include<stdlib.h>
    #include<malloc.h>
     
     
    struct node
    {
        int data;
        struct node *next;
    };
     
     
    typedef struct node link;
     
     
    link create_link(link *r);
     
     
    int main()
    {
        link *trys,*k;
        int i,j,num;
     
     
     //   k=trys;
     
        trys = (link *) malloc (sizeof(link)); 
        k = trys ;   //hereafter k is your head;
        printf("Enter the Number  data\n");
        scanf("%d",&num);
        trys->data=num;   //previously crash was happened here, now memory is allocated so here crash wont happen.
        trys->next=NULL;
        p: printf("\nDo you want to add more numbers?(Y=1/N=0)");
        trys = k;  // you are assigning head to trys, so every search will begin with head node.
        scanf("%d",&i);
        if(i==1)
        {
    /*        for(j=0;trys->next==NULL;j++)   //this also wrong, you have to try until u reach null  
                    trys=trys->next;
       */
            for(j=0;trys->next!=NULL;j++) 
                   trys=trys->next;
            create_link(&trys); // k points to the allocated memory
            {
    //         trys=k;     // here you are assigning k to trys, as of your code, memory is not allocated for trys, again while accessing trys may crash.
    //         for(j=0;trys->next==NULL;j++)   
    //             trys=trys->next;
                printf("\nEnter the next Number data\n");
                scanf("%d",&num);
                trys->data=num;
            }
           // trys=k;
            goto p;
        }
        else
        {
            printf("\nNumber dat entered by you is:\n");
            //for(j=0;trys->next==NULL;j++)
            for(j=0;trys->next!=NULL;j++)
            {
                printf("%d\n",trys->data);
            }
        }
        return 0;
    }
     
     
    link create_link(link **r)
    {
        link *addon;
     
     
        addon=malloc(sizeof(link));
        *r->next=addon;
        addon->next=NULL;
        return addon;
    }

  7. #37
    Registered User
    Join Date
    Apr 2013
    Location
    Gurgaon, Haryana, India
    Posts
    41
    @all who have helped me the program has been solved perfectly, uploading the final code working like a charm thanks all, the last and prior errors can be clarified by the code.
    Code:
    #include<stdio.h>#include<strings.h>
    #include<stdlib.h>
    #include<malloc.h>
    
    
    struct node
    {
        int data;
        struct node *next;
    };
    
    
    typedef struct node link;
    
    
    link create_link(link *r);
    
    
    int main()
    {
        link *trys,*k;
        int i,j,num;
    
    
        trys=malloc(sizeof(link));           /*trys initialisation*/
        k=trys;
        printf("Enter the Number  data\n");
        scanf("%d",&num);
        trys->data=num;                /*store the number in trys*/
        trys->next=NULL;
        p: printf("\nDo you want to add more numbers?(Y=1/N=0)");
        scanf("%d",&i);
        if(i==1)
        {
            for(j=0;trys->next!=NULL;j++)
                    trys=trys->next;            /*to send the pointer to last trys link for more addons*/
            create_link(trys);
            {
                trys=k;
                for(j=0;trys->next!=NULL;j++)                   /*moves the trys pointer to next links for adding numbers*/
                    trys=trys->next;
                printf("\nEnter the next Number data\n");
                scanf("%d",&num);
                trys->data=num;
            }
            trys=k;
            goto p;
        }
        else
        {
            printf("\nNumber dat entered by you is:\n");
            printf("%d\n",(*trys).data);      /*printing the first trys value*/
            for(j=0;trys->next!=NULL;j++)
            {
                trys=trys->next;                /*to increment the address of trys to next link*/
                printf("%d\n",(*trys).data);      /*printf takes value and not address*/
            }
        }
        free(trys);                /*freeing the malloc memory*/
        return 0;
    }
    
    
    link create_link(link *r)
    {
        link *addon;
    
    
        addon=malloc(sizeof(link));
        r->next=addon;
        addon->next=NULL;
        return *r;         /*return type needs to be juastified as type link*/
    }

  8. #38
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Amitesh93
    will goto create a kind of error
    It might lead to a logic error when you write code that is hard to understand because control jumps in unexpected ways. A loop can express your intention more clearly.

    Quote Originally Posted by Amitesh93
    why to remove malloc.h?
    It is non-standard and unnecessary since <stdlib.h> declares what you want.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #39
    Registered User
    Join Date
    Apr 2013
    Location
    Gurgaon, Haryana, India
    Posts
    41
    @laser light hmmm... i'll edit it thanx

  10. #40
    Registered User
    Join Date
    Jun 2013
    Posts
    6
    your program seems to be good, however you are wasting some cpu cycles unneccessarily for searching part ?? from line no 35 to 41, you could try to reduce this by keeping another pointer as tail which has to point the last node initially at head node, whenever adding an element, you could add after tail node and move the tail node to newly allocated one(like queue). Lets try that and post the code, if its wrong.

    Quote Originally Posted by Amitesh93 View Post
    @all who have helped me the program has been solved perfectly, uploading the final code working like a charm thanks all, the last and prior errors can be clarified by the code.
    Code:
    #include<stdio.h>#include<strings.h>
    #include<stdlib.h>
    #include<malloc.h>
    
    
    struct node
    {
        int data;
        struct node *next;
    };
    
    
    typedef struct node link;
    
    
    link create_link(link *r);
    
    
    int main()
    {
        link *trys,*k;
        int i,j,num;
    
    
        trys=malloc(sizeof(link));           /*trys initialisation*/
        k=trys;
        printf("Enter the Number  data\n");
        scanf("%d",&num);
        trys->data=num;                /*store the number in trys*/
        trys->next=NULL;
        p: printf("\nDo you want to add more numbers?(Y=1/N=0)");
        scanf("%d",&i);
        if(i==1)
        {
            for(j=0;trys->next!=NULL;j++)
                    trys=trys->next;            /*to send the pointer to last trys link for more addons*/
            create_link(trys);
            {
                trys=k;
                for(j=0;trys->next!=NULL;j++)                   /*moves the trys pointer to next links for adding numbers*/
                    trys=trys->next;
                printf("\nEnter the next Number data\n");
                scanf("%d",&num);
                trys->data=num;
            }
            trys=k;
            goto p;
        }
        else
        {
            printf("\nNumber dat entered by you is:\n");
            printf("%d\n",(*trys).data);      /*printing the first trys value*/
            for(j=0;trys->next!=NULL;j++)
            {
                trys=trys->next;                /*to increment the address of trys to next link*/
                printf("%d\n",(*trys).data);      /*printf takes value and not address*/
            }
        }
        free(trys);                /*freeing the malloc memory*/
        return 0;
    }
    
    
    link create_link(link *r)
    {
        link *addon;
    
    
        addon=malloc(sizeof(link));
        r->next=addon;
        addon->next=NULL;
        return *r;         /*return type needs to be juastified as type link*/
    }

  11. #41
    Registered User
    Join Date
    Jun 2013
    Posts
    6
    This code have memory leak, if you allocated more than one element, you have to free each and every element.
    instead of trying free(trys) you have to use


    Quote Originally Posted by Amitesh93 View Post
    @all who have helped me the program has been solved perfectly, uploading the final code working like a charm thanks all, the last and prior errors can be clarified by the code.
    Code:
    for ( ; k! = NULL; ) {
           trys = k->next ? k->next : NULL ;
          free (k);
           k = trys;
    }

  12. #42
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    This
    Code:
    trys = k->next ? k->next : NULL ;
    is equivalent to this
    Code:
    trys = k->next;

  13. #43
    Registered User
    Join Date
    Jun 2013
    Posts
    6
    yeah, actually i thought of using trys = k ? k->next:NULL ; since we already checked null check for k, we could use trys = k->next.

    Quote Originally Posted by oogabooga View Post
    This
    Code:
    trys = k->next ? k->next : NULL ;
    is equivalent to this
    Code:
    trys = k->next;

  14. #44
    Registered User
    Join Date
    Apr 2013
    Location
    Gurgaon, Haryana, India
    Posts
    41
    @pappu on using the abuv edition the program stops working after printing the number(*it crashes)

  15. #45
    Registered User
    Join Date
    Apr 2013
    Location
    Gurgaon, Haryana, India
    Posts
    41
    Quote Originally Posted by pappu_uddad View Post
    This code have memory leak, if you allocated more than one element, you have to free each and every element.
    instead of trying free(trys) you have to use
    k!=NULL should be k->next!=NULL then the prog is working fine.
    *can you tell me how to check memory leaks can the compiler tell us or we need to self asses the program i do understand the memory leak phenomenon of malloc but is there any compiler driven method to get it accuratly.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. linked list issue
    By Hayat Qyrt in forum C Programming
    Replies: 16
    Last Post: 10-27-2012, 05:40 AM
  2. linked list issue
    By roaan in forum C Programming
    Replies: 6
    Last Post: 09-01-2009, 01:21 AM
  3. Doubly Linked List Deletion Issue
    By josephjah in forum C++ Programming
    Replies: 22
    Last Post: 07-22-2007, 03:00 PM
  4. Linked list copy constructor issue
    By Craptastic! in forum C++ Programming
    Replies: 1
    Last Post: 08-03-2003, 08:30 PM

Tags for this Thread