Data Structure/Algorithm Challenger III
node* nth_last(node* head, int n);
where, head is pointer to first node of the list.
-Pradeep
Member • May 11, 2009
int PrintNthFromEnd( struct node* ptrNode, int iNth )
{
int ret = 0;
if( ptrNode != NULL )
if( (ret = PrintNthFromEnd( ptrNode->next, iNth )) == (iNth+1) )
printf("%d value from end of list is %d.\n", iNth, ptrNode->data );
return ret+1;
}
And here is the entire code for preforming the task,
/* written by: Arvind(slashfear)
Language: C */
#include <stdio.h>
#include <stdlib.h>
struct node{
int data;
struct node* next;
};
int PrintNthFromEnd( struct node* ptrNode, int iNth )
{
int ret = 0;
if( ptrNode != NULL )
if( (ret = PrintNthFromEnd( ptrNode->next, iNth )) == (iNth+1) )
printf("%d value from end of list is %d.\n", iNth, ptrNode->data );
return ret+1;
}
int main()
{
struct node *head, *curr;
/* List = 4 */
head = malloc(sizeof(struct node));
head->data = 4;
curr = head;
/* List = 4->9 */
curr->next = malloc(sizeof(struct node));
curr = curr->next;
curr->data = 9;
/* List = 4->9->14 */
curr->next = malloc(sizeof(struct node));
curr = curr->next;
curr->data = 14;
/* List = 4->9->14->15 */
curr->next = malloc(sizeof(struct node));
curr = curr->next;
curr->data = 15;
/* List = 4->9->14->15->19 */
curr->next = malloc(sizeof(struct node));
curr = curr->next;
curr->data = 19;
/* List = 4->9->14->15->19->21 */
curr->next = malloc(sizeof(struct node));
curr = curr->next;
curr->data = 21;
curr->next = NULL;
PrintNthFromEnd( head, 0 ); /* Should print 21 */
PrintNthFromEnd( head, 1 ); /* Should print 19 */
PrintNthFromEnd( head, 2 ); /* Should print 15 */
PrintNthFromEnd( head, 3 ); /* Should print 14 */
PrintNthFromEnd( head, 4 ); /* Should print 9 */
PrintNthFromEnd( head, 5 ); /* Should print 4 */
return 0;
}
-Arvind(slashfear)
Member • May 11, 2009
Hi Slashfear, the code works perfectly fine. Just few comments:slashfear/* written by: Arvind(slashfear) Language: C */ #include <stdio.h> #include <stdlib.h> struct node{ int data; struct node* next; }; int PrintNthFromEnd( struct node* ptrNode, int iNth ) { int ret = 0; if( ptrNode != NULL ) if( (ret = PrintNthFromEnd( ptrNode->next, iNth )) == (iNth+1) ) printf("%d value from end of list is %d.\n", iNth, ptrNode->data ); return ret+1; } int main() { struct node *head, *curr; /* List = 4 */ head = malloc(sizeof(struct node)); head->data = 4; curr = head; /* List = 4->9 */ curr->next = malloc(sizeof(struct node)); curr = curr->next; curr->data = 9; /* List = 4->9->14 */ curr->next = malloc(sizeof(struct node)); curr = curr->next; curr->data = 14; /* List = 4->9->14->15 */ curr->next = malloc(sizeof(struct node)); curr = curr->next; curr->data = 15; /* List = 4->9->14->15->19 */ curr->next = malloc(sizeof(struct node)); curr = curr->next; curr->data = 19; /* List = 4->9->14->15->19->21 */ curr->next = malloc(sizeof(struct node)); curr = curr->next; curr->data = 21; curr->next = NULL; PrintNthFromEnd( head, 0 ); /* Should print 21 */ PrintNthFromEnd( head, 1 ); /* Should print 19 */ PrintNthFromEnd( head, 2 ); /* Should print 15 */ PrintNthFromEnd( head, 3 ); /* Should print 14 */ PrintNthFromEnd( head, 4 ); /* Should print 9 */ PrintNthFromEnd( head, 5 ); /* Should print 4 */ return 0; }
In a real time situation the node may contain lots of data and i may need the node instead of printing the value. So i provided a prototype where function return the node.slashfearI have used PrintNthFromEnd function to obtain the task.
Member • May 11, 2009
Member • May 17, 2009
node* nth_last(node* head, int n) {
int count = 0;
node* tmp = NULL;
node* result = NULL;
tmp = result = head;
while(tmp != NULL) {
count++;
if(count > n) {
result = result->next;
}
tmp = tmp->next;
}
if(count < n) {
result = NULL;
}
return result;
}
Let me know if it need any clarification.