Thread: Beginner help

  1. #1
    Registered User
    Join Date
    Apr 2009
    Location
    ISTANBUL
    Posts
    8

    Question Beginner help

    Hi,

    I have just learned linked lists on C and want to write a program about it. But even this simple code gives runtime error which I added as attachment.

    Could you help me about it, thanks...

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    First, I want to point out that malloc is in stdlib.h, but you aren't including it.
    As for the problem, it's because head->next != NULL. You never initialize it to anything, so the program think it's valid.
    Last edited by Elysia; 07-27-2009 at 03:32 PM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Mar 2003
    Location
    UK
    Posts
    170
    The next pointer in head needs to be set to NULL after the first malloc:

    head->next = NULL;

  4. #4
    Registered User
    Join Date
    Apr 2009
    Location
    ISTANBUL
    Posts
    8
    Quote Originally Posted by Scarlet7 View Post
    The next pointer in head needs to be set to NULL after the first malloc:

    head->next = NULL;
    Thanks, it works I missed it. But what about the dump value at the end of the list?

  5. #5
    Registered User Cooloorful's Avatar
    Join Date
    Feb 2009
    Posts
    59
    Why not just write out your values like this?

    Code:
        while(current != NULL){
          if(current->coef != 0)
            printf("%dx%d+",current->coef,current->pow);
          current = current->next;
        }
    wipe on -
    A slap on the hand is better than a slap on the face. A tragic lesson learned far too late in life.
    - wipe off

  6. #6
    Registered User
    Join Date
    Apr 2009
    Location
    ISTANBUL
    Posts
    8
    Quote Originally Posted by Cooloorful View Post
    Why not just write out your values like this?

    Code:
        while(current != NULL){
          if(current->coef != 0)
            printf("%dx%d+",current->coef,current->pow);
          current = current->next;
        }
    Maybe but I just try the code to understand

  7. #7
    Registered User Cooloorful's Avatar
    Join Date
    Feb 2009
    Posts
    59
    Code:
        while(current != NULL){
          if(current->coef != 0)
            printf("%dx%d+",current->coef,current->pow);
          current = current->next;
        }
    Ok, the blue sections are your code and won't be explained. The green is mine and I will explain.

    When you are reading in the values you are taking in a final value with a coefficient of 0. You allow it in the list, but you technically only use it to let your program know when to stop reading in new values. Ok, that is fine. Given that fact, why even print something that is basically just a signal to stop reading? So the if statement checks if the coefficient is zero. If the coefficient is zero, the term is skipped.
    wipe on -
    A slap on the hand is better than a slap on the face. A tragic lesson learned far too late in life.
    - wipe off

  8. #8
    Registered User
    Join Date
    Apr 2009
    Location
    ISTANBUL
    Posts
    8
    Quote Originally Posted by Cooloorful View Post
    Code:
        while(current != NULL){
          if(current->coef != 0)
            printf("%dx%d+",current->coef,current->pow);
          current = current->next;
        }
    Ok, the blue sections are your code and won't be explained. The green is mine and I will explain.

    When you are reading in the values you are taking in a final value with a coefficient of 0. You allow it in the list, but you technically only use it to let your program know when to stop reading in new values. Ok, that is fine. Given that fact, why even print something that is basically just a signal to stop reading? So the if statement checks if the coefficient is zero. If the coefficient is zero, the term is skipped.
    OK It is the same but have you ever tried to run, when I attempt to exit, a dump value occurs at the end of the list

    THANKS

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Same old beginner question...
    By Sharmz in forum C Programming
    Replies: 15
    Last Post: 08-04-2008, 11:48 AM
  2. What are some good beginner programs I shouold make?
    By oobootsy1 in forum C# Programming
    Replies: 6
    Last Post: 08-09-2005, 02:02 PM
  3. Beginner, trying to learn how to make an image.
    By shishkabob in forum C++ Programming
    Replies: 1
    Last Post: 10-24-2004, 08:46 PM
  4. Windows programming for beginner (Absolute beginner)
    By WDT in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2004, 11:21 AM

Tags for this Thread