C Program to Find Nth Node from End of Linked List

This C Program prints the Nth node from the last node of a linked list. The approach used is to first point a pointer to a node which is N nodes ahead from the head. Then, move the Nth pointer and head pointer one node at a time till the Nth pointer has reached to the rear node. The pointer in the middle is the Nth pointer from the end.

Here is a source code of the C program to print the nth node from the last node of a linked list. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C Program to Print Nth Node from the last of a Linked List 
  3.  */
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6.  
  7. struct node
  8. {
  9.     int num;
  10.     struct node *next;
  11. };
  12.  
  13. void create(struct node **);
  14. void nthnode(struct node *, int);
  15. void release(struct node **);
  16.  
  17. int main()
  18. {
  19.     struct node *p = NULL;
  20.     int n;
  21.  
  22.     printf("Enter data into the list\n");
  23.     create(&p);
  24.     printf("Enter the value n to find nth position from the last node: ");
  25.     scanf("%d", &n);
  26.     nthnode(p, n);
  27.     release (&p);
  28.  
  29.     return 0;
  30. }
  31.  
  32. void nthnode(struct node *head, int n)
  33. {
  34.     struct node *p, *q;
  35.     int i;
  36.  
  37.     q = p = head;
  38.  
  39.     for (i = 0; i < n && q != NULL; i++)
  40.     {
  41.         q = q->next;
  42.     }
  43.     if (i < n)
  44.     {
  45.         printf("Entered n = %d is larger than the number of elements = %d in list. Please try again.\n", n, i);
  46.     }
  47.     else
  48.     {
  49.         while (q->next != NULL)
  50.         {
  51.             q = q->next;
  52.             p = p->next;
  53.         }
  54.         printf("%d is %d nodes from the last node.\n", p->num, n);
  55.     }
  56. }
  57.  
  58. void create(struct node **head)
  59. {
  60.     int c, ch;
  61.     struct node *temp, *rear;
  62.  
  63.     do
  64.     {
  65.         printf("Enter number: ");
  66.         scanf("%d", &c);
  67.         temp = (struct node *)malloc(sizeof(struct node));
  68.         temp->num = c;
  69.         temp->next = NULL;
  70.         if (*head == NULL)
  71.         {
  72.             *head = temp;
  73.         }
  74.         else
  75.         {
  76.             rear->next = temp;
  77.         }
  78.         rear = temp;
  79.         printf("Do you wish to continue [1/0]: ");
  80.         scanf("%d", &ch);
  81.     } while (ch != 0);
  82.     printf("\n");
  83. }
  84.  
  85. void release(struct node **head)
  86. {
  87.     struct node *temp = *head;
  88.     *head = (*head)->next;
  89.     while ((*head) != NULL)
  90.     {
  91.         free(temp);
  92.         temp = *head;
  93.         (*head) = (*head)->next;
  94.     }
  95. }

$ cc nthnode.c 
$ ./a.out
Enter data into the list
Enter number: 1
Do you wish to continue [1/0]: 1
Enter number: 2
Do you wish to continue [1/0]: 1
Enter number: 3
Do you wish to continue [1/0]: 1
Enter number: 4
Do you wish to continue [1/0]: 1
Enter number: 5
Do you wish to continue [1/0]: 0
 
Enter the value n to find nth position from the last node: 2
3 is 2 nodes from the last node.

Sanfoundry Global Education & Learning Series – 1000 C Programs.

advertisement
advertisement

Here’s the list of Best Books in C Programming, Data-Structures and Algorithms

If you wish to look at programming examples on all topics, go to C Programming Examples.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.