Thread: is there a better way to optimise this?

  1. #16
    Temporal Apparition qubit67's Avatar
    Join Date
    Jan 2007
    Posts
    85
    sh!t your right, I should have initialized the prev to NULL to use for the head....
    sorry for the frustration..

  2. #17
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by iMalc View Post
    Oh no it's just getting worse - I can't stand it any longer!
    This is how to do it, the way you've defined things:
    Code:
    list_t* Insert(list_t *list, data_t value)
    {
    	node_t *prev = NULL, *curr = list->head, *ins = malloc(sizeof(node_t));
    	while (curr != NULL)
    	{
    		if (value < curr->data) break;
    		prev = curr;
    		curr = curr->next;
    	}
    	if (prev == NULL)
    		list->head = ins;
    	else
    		prev->next = ins;
    	if (list->foot == NULL)
    		list->foot = ins;
    	ins->next = curr;
    	ins->data = value;
    	return list;
    }
    Wouldn't list->foot need to be updated even in situations where it's non-NULL? [I'm assuming "foot" is equivalent of "tail" in this case, rather than for example "the first inserted element", as the latter isn't a particularly useful concept as far as I'm aware.

    I think the expresson shoudl be "if(curr == NULL)" instead, but I'm not 100% sure. Can you confirm, iMalc?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #18
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by matsp View Post
    Wouldn't list->foot need to be updated even in situations where it's non-NULL? [I'm assuming "foot" is equivalent of "tail" in this case, rather than for example "the first inserted element", as the latter isn't a particularly useful concept as far as I'm aware.

    I think the expresson shoudl be "if(curr == NULL)" instead, but I'm not 100% sure. Can you confirm, iMalc?

    --
    Mats
    Um yeah, good spotting, that's what it should be. Made a mistake in translating my C++ code to his C code.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #19
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by iMalc View Post
    Um yeah, good spotting, that's what it should be. Made a mistake in translating my C++ code to his C code.
    Sure, I make mistakes too, as you may know... :-)

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #20
    Temporal Apparition qubit67's Avatar
    Join Date
    Jan 2007
    Posts
    85
    yeh, foot/tail needs to be updated, but so what? it was the algorithm i was looking for... the proccess... of course His program needs to be updated, but really...
    He doesn't care and doesn't think about it so anal....

    so what do you recommend? code wise?
    Last edited by qubit67; 09-07-2007 at 04:56 AM.

  6. #21
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Well, if the algorithm is "broken", it needs to be fixed, otherwise it's wrong. You'll find that A LOT of us here are anal about getting things right, because there are lots of things that are wrong. If you fix the
    Code:
    	if (list->foot == NULL)
    		list->foot = ins;
    into
    Code:
    	if (curr == NULL)
    		list->foot = ins;
    then you should be fine. Yes, it's anal, but it's "necessary" to get it right, assuming you do use foot for some purpose.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Optimise?
    By rtan3005 in forum C Programming
    Replies: 17
    Last Post: 11-14-2006, 03:10 PM
  2. Will 'static' optimise my code?
    By samGwilliam in forum C++ Programming
    Replies: 3
    Last Post: 03-29-2005, 11:28 AM
  3. optimise algorithm
    By cyberCLoWn in forum C++ Programming
    Replies: 10
    Last Post: 01-18-2005, 02:27 PM
  4. Optimise FindFiles.
    By anonytmouse in forum Windows Programming
    Replies: 0
    Last Post: 01-14-2004, 02:41 AM
  5. How to optimise this c program?
    By megablue in forum C Programming
    Replies: 5
    Last Post: 06-28-2003, 04:20 AM