Thread: Get Nth node in a linked list

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    74

    Get Nth node in a linked list

    Hi there. I'm back :-)

    Code:
    void GetNth (node** head)
    {
    	node* current = *head;
    	node* walker = *head;
    	int n, length, count;
    	
    	printf("Input n ...  ");
    	scanf("%d",n);
    	
    	length = 0;
    	count = 0;
    	if (current == NULL)
    	{
    		length = 0;
    	}
    	else
    	{
    		while (current != NULL) 
    		{
    			length++;
    			current = current->next;
    		}		
    	}
    	
    	if (n>length)
    	{
    		printf("\nNo such Node\n");
    	}
    	else
    	{
    		do  
    		{
    			walker = walker->next;
    			count++;
    		} while (count != n);
    			
    		/* found the Nth*/	
    		printf("N-th => NAME: %s\tAGE: %d\n",walker->name,walker->age);
    		
    		}
    	}
    
    }
    this doesn't work no matter which number I enter for n, and no matter how long the list is. can you help me?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    scanf("%d",n) should be scanf("%d", &n). However, consider what happens if the user enters a negative number.

    Incidentally, I do not think that you need to get the length of the linked list. You can begin traversing over it straight away, and if you find that you have reached the end of the linked list without reaching the nth node, then clearly there is no such node.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    74
    do you mind giving me a little help with that algorithm?

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    You've done it already.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by MK27
    You've done it already.
    No, since what budala implemented involves first finding the length of the linked list, and then if tghe desired index does not exceed the length, traverse over the linked list to find the desired node.

    Quote Originally Posted by budala
    do you mind giving me a little help with that algorithm?
    Sure. Firstly, I suggest that you separate the finding of the nth node from its use. That is, create another function that returns a pointer to the nth node, or a null pointer if no such node exists:
    Code:
    node *GetNthNode(node *head, int n);
    If n is less than 0, return a null pointer. Otherwise, initialise the current node to point to the head (or you can reuse the pointer named head) and initialise a counter to 0. While the current node is not null and your counter is less than n, make the next node be the current node and increment the counter. Return the pointer to the current node when the loop ends.

    Now, implementing GetNth() is easy: you just need to get the input from the user (which you have already done), call GetNthNode(), and depending on whether the return value is a null pointer you decide to either print the error message or print the node's contents.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. linked list question
    By brb9412 in forum C Programming
    Replies: 16
    Last Post: 01-04-2009, 04:05 PM
  2. Replies: 3
    Last Post: 12-06-2008, 07:54 PM
  3. help me debug this linked list !
    By Dark Angel in forum C Programming
    Replies: 6
    Last Post: 04-18-2008, 02:10 PM
  4. Problem with linked list ADT and incomplete structure
    By prawntoast in forum C Programming
    Replies: 1
    Last Post: 04-30-2005, 01:29 AM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM