Thread: Linked List Problem

Threaded View

Previous Post Previous Post   Next Post Next Post
  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 11:01 PM.

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, 02:45 AM
  4. Linked list with two class types within template.
    By SilasP in forum C++ Programming
    Replies: 3
    Last Post: 02-09-2002, 06:13 AM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM