List

This is a discussion on List within the C Programming forums, part of the General Programming Boards category; Hi i have a little question here is a simple piece of code inserting an int in ftont of a ...

  1. #1
    Registered User BraneMxm's Avatar
    Join Date
    Apr 2007
    Location
    Croatia
    Posts
    20

    Smile List

    Hi i have a little question

    here is a simple piece of code inserting an int in ftont of a linked list

    if i change
    void insertFront(list **ptrL, int data)
    into
    void *insertFront(list **ptrL, int data)

    the compiler doesnt see any difference.

    I supose that it doesnt matter what i write because it it is the same if the func returns void or pointer to void.

    Can anyone explain it to me?

    Thanks

    Code:
    void insertFront(list **ptrL, int data)
    {
        list *temp=(list*)malloc(sizeof(struct node));
       
        temp->data=data;
        temp->next=*ptrL;
        *ptrL=temp;
    }

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,706
    void pointers are only useful when you need to point to a type anonymously, like memory addresses, for example. Changing the return type of a function that returns void to void pointer is just completely ignorant, because it doesn't mean anything similar. void is meant to express the lack of a returned value. You can actually use and return void pointers, malloc does it all the time.

  3. #3
    Registered User BraneMxm's Avatar
    Join Date
    Apr 2007
    Location
    Croatia
    Posts
    20

    Static

    Please take a look at this code

    Code:
    int countLeaves(bTree *ptrT)
    {
     static int i=0;
        if(!ptrT)
           return i;
        else
        {    
            if(!(ptrT->left) && !(ptrT->right))
                    i++;
            i=countLeaves(ptrT->left);
            i=countLeaves(ptrT->right);
        }
        return i;        
    }
    Is there any way that i can make i=0 when I call it from main()? I could do it by passing a parameter that tells if its from main or recursive but is there nay other way?

    Thanks
    Last edited by BraneMxm; 04-17-2007 at 08:10 AM.

  4. #4
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,706
    The way things are now I don't think you need to do anything. The static variable i will be reinitilized to zero with every function call because of where you've declared and initialized that variable.

  5. #5
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    how about:

    Code:
    int countLeaves(bTree *ptrT)
    {
        if(!(ptrT->left) && !(ptrT->right))
           return 1;
        else
        {    
            return countLeaves(ptrT->left) + countLeaves(ptrT->right);
        }
    }

  6. #6
    Registered User BraneMxm's Avatar
    Join Date
    Apr 2007
    Location
    Croatia
    Posts
    20

    Re->List

    Hi and good morning to all
    @citizen
    The way things are now I don't think you need to do anything. The static variable i will be reinitilized to zero with every function call because of where you've declared and initialized that variable.
    Well I thought that the word static would be there to ignore the initialization to 0 when i call the function again, she is there to preserve the value.

    @Koni
    Ill try it out, I guess that is what I was seraching about

    Thanks to all

  7. #7
    Registered User BraneMxm's Avatar
    Join Date
    Apr 2007
    Location
    Croatia
    Posts
    20

    Re->Tree not list;-)

    @koni
    I added something to your code to wouldnt work else. Am I right?
    Code:
    int countLeaves(bTree *ptrT)
    {
        if(ptrT)
           if(!(ptrT->left) && !(ptrT->right))
                  return 1;
              else
              {    
                      return countLeaves(ptrT->left) + countLeaves(ptrT->right);
              }
        else return 0;      
    }
    Thanks

  8. #8
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    That's correct. Since I call countLeaves() on both the left and right side without even checking if they exist, it happens that I call the function on a node that doesn't exist, in which case your condition is absolutely correct. And it is also correct to return 0 in that case.

  9. #9
    Registered User BraneMxm's Avatar
    Join Date
    Apr 2007
    Location
    Croatia
    Posts
    20
    I recieved some access violation error when I got on a node that was a leaf. However it works now.
    Thanks and greetings to Luxemburg
    Brane

  10. #10
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,304
    Quote Originally Posted by citizen View Post
    The way things are now I don't think you need to do anything. The static variable i will be reinitilized to zero with every function call because of where you've declared and initialized that variable.
    No it wont. That's not how static variables within a function work. static is not needed here anyway.

    Here is a fixed version of the function how it should be written:
    Code:
    int countLeaves(const bTree *ptrT) {
    	int count = 0;
    	while (ptrT != NULL) {
    		//recursively count left nodes (add one for this node)
    		count += countLeaves(ptrT->left) + 1;
    		//iteratively count right nodes
    		ptrT = ptrT->right;
    	}
    	return count;
    }
    Less chance of stack overflow due to explicit tail-recursion optimisation.
    No static variables.
    const input parameter because the pointed to data is never modified.
    No crashing if passed NULL.
    Last edited by iMalc; 04-18-2007 at 02:14 AM.

  11. #11
    Registered User BraneMxm's Avatar
    Join Date
    Apr 2007
    Location
    Croatia
    Posts
    20
    @iMalc

    Thanks for the advice, I want to count the leaves not the nodes!

    So I guess it is a good practice always to use const in the parameter when the passed value isnt changed, only used to go through the tree and ect. Can you explain it to me in more detail? When my teacher asks why I used const I want to be able to explain it.

    Thank you

  12. #12
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    Quote Originally Posted by BraneMxm View Post
    So I guess it is a good practice always to use const in the parameter when the passed value isnt changed, only used to go through the tree and ect. Can you explain it to me in more detail? When my teacher asks why I used const I want to be able to explain it.

    Thank you
    There's two changes const makes...
    • Enforces that you do not modify the parameter and can only call const functions to it.
    • Allows the function to be called by other functions which also use a const type for the parameter.


    The second point can be rephrased as... if you don't use const, then you force everyone who calls your method to not use const. Consider the following:
    Code:
    void printTreeInfo (bTree const * info_me) {  // Const paramater because it doesn't modify the tree.
       printf ("Num Leaves: %d\n", countLeaves(info_me)); // Only works if countLeaves accepts a const parameter.
       printf ("Num Nodes: %d\n", countNodes(info_me));
       // &ct...
    }
    Addendum:
    Trees aren't the best example of needing const-correctness, since you lose the const once you use Ptr->left or Ptr->right.
    Callou collei we'll code the way
    Of prime numbers and pings!

  13. #13
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Also:
    Code:
    void printTreeInfo (bTree const * info_me) {  // Const paramater because it doesn't modify the tree.
    That may look strange, but you can put keywords in any order. These are all the same thing:
    Code:
    const unsigned long int
    int unsigned const long
    int long unsigned const
    But this isn't the same thing:
    Code:
    int *const p;
    A "const" after the asterisk means that it's a constant pointer, not a pointer to a constant value: i.e., for that example, you can't change which variable p points too, though you can modify the value of the variable that p points to. Sorry for confusing you. You don't have to understand that. I was just warning you not to use a const after an asterisk.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  14. #14
    Registered User BraneMxm's Avatar
    Join Date
    Apr 2007
    Location
    Croatia
    Posts
    20
    Well I guess there is no strong reason to use it in Trees and Lists, and as I dont understand it very well I wont use it.

    Thank you all

  15. #15
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,304
    Quote Originally Posted by BraneMxm View Post
    Well I guess there is no strong reason to use it in Trees and Lists, and as I dont understand it very well I wont use it.
    I hope you simply mean that you wont use it yet.
    Sorry I missed the fact that you only want to count leaves (I've already slapped myself for missing that). Here is an alternative that will do just that:
    Code:
    int countLeaves(const bTree *ptrT) {
    	int count = 0;
    	while (ptrT != NULL) {
    		if (ptrT->left == NULL) {
    			if (ptrT->right == NULL)
    				++count;
    		} else {
    			count += countLeaves(ptrT->left);
    		}
    		ptrT = ptrT->right;
    	}
    	return count;
    }

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 05:55 PM
  2. instantiated from here: errors...
    By advocation in forum C++ Programming
    Replies: 5
    Last Post: 03-27-2005, 08:01 AM
  3. How can I traverse a huffman tree
    By carrja99 in forum C++ Programming
    Replies: 3
    Last Post: 04-28-2003, 05:46 PM
  4. List class
    By SilasP in forum C++ Programming
    Replies: 0
    Last Post: 02-10-2002, 04:20 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21