Thread: Linked list logic error

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    16

    Linked list logic error

    Hi, their is part of a homework problem which I seem to be stuck on. It asks what are the logic errors in a program if the function defined is supposed to return the legnth of a linked list.

    The function is:
    Code:
    int length(NodePtr *Head){
    int size = 0;
    NodePtr *cur;
    while(cur != NULL){
    size++;
    cur = cur->next;
    }
    }
    I am having trouble seeing what is wrong with it...It seems as it is set to increment the int variable size by one while the pointed variable cur is not null. cur is then set to be the next value. This would continue until cur had nothing left thus making it null and stopping the while loop.

    Help please!

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> NodePtr *cur;

    hint: what does this point to?
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    16
    to the node counter?

    ..nvm, let me think some more before answering.

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    regardless of the data type, a variable should be initialized to a valid state before working with it. consider:

    Code:
    int
    main(void)
    {
    	int i;
    	cout << i << endl;
    }
    what do you think the program will print?
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  5. #5
    Registered User
    Join Date
    Feb 2006
    Posts
    16
    whatever random value the os has stored in that memory space for i at that time. Other compiler would just give an error.

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    right.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  7. #7
    Registered User
    Join Date
    Feb 2006
    Posts
    16
    And this is the exact question if I asked it wrong:
    Code:
    The following function is supposed to return the length of a linked list. What are the logical errors in
    this function?
    int length(NodePtr *Head){
    int size = 0;
    NodePtr *cur;
    while(cur != NULL){
    size++;
    cur = cur->next;
    }
    }

  8. #8
    Registered User
    Join Date
    Feb 2006
    Posts
    16
    Oh, so you are saying since *cur is pointing to nothing, it cant work?

  9. #9
    Registered User
    Join Date
    Feb 2006
    Posts
    16
    I believe it assumes that elsewhere NodePtr and Head, etc are defined.

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    It's an uninitialized pointer. It doesn't matter if those things are defined elsewhere, cur doesn't point to anything when it is first used.

    >> Other compiler would just give an error.
    No, it is not a compile error, it is a logical error. All compilers should probably give a warning, though.

  11. #11
    Registered User
    Join Date
    Feb 2006
    Posts
    16
    Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how do you resolve this error?
    By -EquinoX- in forum C Programming
    Replies: 32
    Last Post: 11-05-2008, 04:35 PM
  2. Getting other processes class names
    By Hawkin in forum Windows Programming
    Replies: 3
    Last Post: 03-20-2008, 04:02 PM
  3. more then 100errors in header
    By hallo007 in forum Windows Programming
    Replies: 20
    Last Post: 05-13-2007, 08:26 AM
  4. error: template with C linkage
    By michaels-r in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2006, 08:11 AM
  5. Stupid compiler errors
    By ChrisEacrett in forum C++ Programming
    Replies: 9
    Last Post: 11-30-2003, 05:44 PM