Linked List Problem

This is a discussion on Linked List Problem within the C++ Programming forums, part of the General Programming Boards category; I'm having a problem with a function for my linked list. What it does is navigate through a given number ...

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    203

    Linked List Problem

    I'm having a problem with a function for my linked list. What it does is navigate through a given number of nodes, seperates the list at that point, and then returns a pointer to the new head of the second list. If the end of the list is reached before the given number is reach, then it is supposed to return NULL. The function seems to work properly unless the end of the list is reached. I'm getting an exception if the list is shorter than the given number. I've tried a few different things and I don't understand what's causing it. NextNode is the List* for the list.
    Code:
    List* breakList(List* NodeHead, int NumNodes)
    {
    	List* NewNodeHead;
    
    	while(NodeHead->NextNode && NumNodes)
    	{
    		NodeHead = NodeHead->NextNode;
    		NumNodes--;
    	}
    
    	NewNodeHead = NodeHead->NextMenuLine;
    	NodeHead->NextNode = NULL;
    
    	return NewNodeHead;
    }
    edit: Using text as test data I found I was losing 1 node each call. I've changed the return
    Last edited by Syneris; 01-06-2006 at 10:01 PM.

  2. #2
    C++ Newbie
    Join Date
    Nov 2005
    Posts
    49
    You've helped me before so I'll help you now, I think I can cope... I'm newbie too.

    There is a problem with the NumNodes variable, what if the argument passed were a valid list and -1? I always check whether list length is between 0 and list_size.

    What is NextMenuLine exactly?

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    203
    Problem fixed. I was calling the function with bad pointers. The list had already reached the end. Just need to add a check on the pointer before the while loop now.

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    203
    NextMenuLine should have been NextNode. I simplified the code for posting purposes. Thanks on the -1 as that could also cause problems. Making it an unsigned it should fix that and probably cause a compiler error if used wrong

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,485
    > I'm getting an exception if the list is shorter than the given number
    Probably because your exit code should be surrounded with an if statement like
    Code:
    if ( NodeHead->NextNode == NULL ) {
      return NULL;
    } else {
      // other stuff
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help sorting a linked list. Beginner
    By scarlet00014 in forum C Programming
    Replies: 1
    Last Post: 09-27-2008, 06:16 PM
  2. singly linked circular list
    By DarkDot in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2007, 08:55 PM
  3. Replies: 6
    Last Post: 03-02-2005, 01:45 AM
  4. Linked list with two class types within template.
    By SilasP in forum C++ Programming
    Replies: 3
    Last Post: 02-09-2002, 05:13 AM
  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