Get Nth node in a linked list

This is a discussion on Get Nth node in a linked list within the C Programming forums, part of the General Programming Boards category; Hi there. I'm back :-) Code: void GetNth (node** head) { node* current = *head; node* walker = *head; int ...

  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
    22,272
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    22,272
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 03:10 PM
  4. Problem with linked list ADT and incomplete structure
    By prawntoast in forum C Programming
    Replies: 1
    Last Post: 04-30-2005, 02:29 AM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 11:21 PM

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