#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    9
    Rep Power
    0

    No output showing in Dev C++ compiler..help please


    Hi All,
    I'm entering a very basic code of appending nodes in a link list in ascending order. But the compiler shows a black screen as output.
    Please Help. Here's the code...



    #include<stdio.h>
    #include<conio.h>
    #include<malloc.h>

    struct node{
    int data;
    struct node *link;
    };

    void addinasc(struct node **, int);
    void display(struct node *);
    void count(struct node *);

    int main()
    {
    struct node *p;
    p=NULL;

    addinasc(&p , 10);
    addinasc(&p , 7);
    addinasc(&p , 74);
    addinasc(&p , 91);
    addinasc(&p , 56);
    addinasc(&p , 24);
    addinasc(&p , 35);
    addinasc(&p , -5);

    count(p);
    display(p);

    /*reverse(&p);

    display(p);*/
    getch();
    return 0;
    }

    void addinasc(struct node **q, int num)
    {
    struct node *temp , *r;
    temp = *q;

    r=(struct node*)malloc(sizeof(struct node));
    r->data = num;

    if(*q==NULL || (*q)->data>num)
    {
    r->link = *q;
    *q = r;
    }
    else
    {
    while(temp!=NULL)
    if(temp->data<=num && (temp->link->data > num || temp->link == NULL))
    {
    r->link = temp->link;
    temp->link = r;
    return;
    }
    temp = temp->link;
    }
    }

    void count(struct node *q)
    {
    int count=0;
    while(q!=NULL)
    {
    count++;
    q=q->link;
    }
    printf("\n\nNo. of elements in the linked list : %d\n" , count);
    }

    void display(struct node *q)
    {
    while(q!=NULL)
    {
    printf("\t%d", q->data);
    q=q->link;
    }
    }
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2012
    Posts
    29
    Rep Power
    0
    Had you given the input for the function, to display the link list ? check for that
  4. #3
  5. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,900
    Rep Power
    481
    You've discovered the infinite loop!

    I amended your addinasc function to display a message every time it's called:
    Code:
    void addinasc(struct node **q, int num) {
      struct node *temp , *r;
      puts("addinasc"); /*THIS LINE INSERTED **************/
      temp = *q;
    And when I ran the program I got this output, which I had to kill because it runs forever:

    $ a=./c && make -k $a && $a
    make: `c' is up to date.
    addinasc
    addinasc
    addinasc


    I'd say that
    addinasc(&p , 74);
    failed. Look there.
    [code]Code tags[/code] are essential for python code and Makefiles!
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    9
    Rep Power
    0

    Corrected Code


    Again i forgot to enclose the code within while loop in the { }.
    Anyways, I have done that now and I am posting the code again.
    Please help . Cant understand why no output is showing.

    Again, I'm using Dev C++ IDE.


    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<malloc.h>
    
    struct node{
           int data;
           struct node *link;
           };
           
    void addinasc(struct node **, int);
    void display(struct node *);
    void count(struct node *);
           
    int main()
    {
        struct node *p;
        p=NULL;
        
        addinasc(&p,10);       
        addinasc(&p,7); 
        addinasc(&p,74);
        addinasc(&p,91);
        addinasc(&p,56);
        addinasc(&p,24);
        addinasc(&p,35);
        addinasc(&p,-5);    
        
        count(p);
        display(p);
        
        /*reverse(&p); 
        
        display(p);*/
        getch();
        return 0;
    }
    
    void addinasc(struct node **q, int num)
    {
         struct node *temp , *r;
         temp = *q;
         
         r=(struct node*)malloc(sizeof(struct node));
         r->data = num;
         
         if(*q==NULL || (*q)->data>num)
         {
                     r->link = *q;
                     *q = r;
         }
         else
         {
             while(temp!=NULL)
             {
                    if(temp->data<=num && (temp->link->data > num || temp->link == NULL))
                    {
                               r->link = temp->link;
                               temp->link = r;
                               return;
                    }
                    temp = temp->link;
             }
         }
    }
    
    void count(struct node *q)
    {
        int count=0;
        while(q!=NULL)
        {
                       count++;
                       q=q->link;
        }
        printf("\n\nNo. of elements in the linked list : %d\n" , count);             
    }
    
    void display(struct node *q)
    {
         while(q!=NULL)
         {
                       printf("\t%d", q->data);
                       q=q->link;
         }
    }
  8. #5
  9. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,175
    Rep Power
    2222
    Er, b49P23TIvg informed you that you were trapped within an infinite loop. What do you not understand about infinite loop?

    In my undergraduate days, we were on an IBM/370 system that emulated the IBM/360 OS. The vast majority of error messages we received told us almost absolutely nothing useful ("Probable Cause: Programmer error." "Corrective Action: Correct the programming error and resubmit". Except for the ones that told us that our time allocation had timed out, which told us that we had been trapped within an infinite loop.

    An infinite loop is one in which there is no escape. For example, the code
    Code:
      while (1)
        { }
    would be an infinite loop. You would be forever trapped within the loop, unless some kind of condition allowed you to jump out of it (eg, something to trigger a break). As long as you're trapped within an infinite loop, there is no way out and you will remain there forever. Which means that any code
    that lies after that infinite loop will never ever execute.

    Again, b49P23TIvg demonstrated that your code, addinasc in particular, is trapped within an infinite loop. You need to correct that.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    9
    Rep Power
    0
    I do know what an infinite loop is.
    But what i'm not able to fathom is how and where i have entered that infinite loop. Infact i've cross checked the code from a book also and it's also pretty similar.

    Please post a solution to the problem(probably some corrected lines of code) if possible. Would help a lot
  12. #7
  13. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,113
    Rep Power
    1803
    I would suggest that the simplest way to find out what is happening is to run and step the code in teh debugger. However Dev-C++'s debugger sucks to the point of uselessness, so I strongly suggest that you abandon it - it has not been maintained since 2005 and is distributed with an equally old version of MinGW/GCC. Use VC++ 2010 Express Edition instead. Free, more fully featured, and has a best-in-class debugger.

    Your code compiles without complaint in VC++ 2010 with one minor change; replace getch() with _getch().

    When run it crashes, but the debugger helpfully stops and shows you exactly where it has crashed. At this line
    Code:
    if(temp->data<=num && (temp->link->data > num || temp->link == NULL))
    in addinasc().. At the call addinasc(&p, 74), the value of temp->link at the point it crashes is NULL - you are dereferencing a NULL pointer. You need to figure out either why it is NULL if it shouldn't be or if NULL is acceptable, you need to add a check for it before attempting to dereference it.

    Really - use a tool with a working debugger and use it!. The analysis above took me no longer than the time it took to compile and run the code. Debugging by forum is impractical and a waste of everyone's time including yours. I only took the time here to illustrate how effective a debugger can be in the hope that you would see the light.
    Last edited by clifford; September 1st, 2012 at 03:14 AM.
  14. #8
  15. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,113
    Rep Power
    1803
    Looking at that line again, I see that you do check for temp->link == NULL, but only after you have attempted to inspect temp->link->data, which will fail if link is NULL.

    If you swap the test as follows:
    Code:
    if( temp->data <= num && (temp->link == NULL || temp->link->data > num ) )
    The short-circuit evaluation of || will prevent the dereferencing of the NULL pointer. That is to say when temp->link == NULL is true, the evaluation of || stops without evaluating the right-hand side expression, because the left-hand side is already true therefore the whole expression is known to be true already.
    Last edited by clifford; September 1st, 2012 at 03:24 AM.
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    9
    Rep Power
    0

    Problem Solved


    Thanks a lot Clifford..
    The problem is resolved and I got to learn a new thing.
    Thanks so much :)

IMN logo majestic logo threadwatch logo seochat tools logo