C Program to Find Middle Element of a Linked List

«
»
This C Program prints the middle most node of a linked list.

Here is source code of the C Program print the middle most 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 Middle most Node 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 middlenode(struct node *);
  15. void release(struct node **);
  16.  
  17. int main()
  18. {
  19.     struct node *p = NULL;
  20.  
  21.     printf("Enter data into the list\n");
  22.     create(&p);
  23.     middlenode(p);
  24.     release (&p);
  25.  
  26.     return 0;
  27. }
  28.  
  29. void middlenode(struct node *head)
  30. {
  31.     struct node *p, *q;
  32.     int flag = 0;
  33.  
  34.     q = p = head;
  35.     /*for every two hops of q, one hop for p*/
  36.     while (q->next != NULL)
  37.     {
  38.         q = q->next;
  39.         if (flag)
  40.         {
  41.             p = p->next;
  42.         }
  43.         flag = !flag;
  44.     }
  45.     if (flag)
  46.     {
  47.         printf("List contains even number of nodes\nThe middle two node's values are: %d  %d\n", p->next->num, p->num);
  48.     }
  49.     else
  50.     {
  51.         printf("The middle node of the list is: %d\n", p->num);
  52.     }
  53. }
  54.  
  55. void create(struct node **head)
  56. {
  57.     int c, ch;
  58.     struct node *temp;
  59.  
  60.     do
  61.     {
  62.         printf("Enter number: ");
  63.         scanf("%d", &c);
  64.         temp = (struct node *)malloc(sizeof(struct node));
  65.         temp->num = c;
  66.         temp->next = *head;
  67.         *head = temp;
  68.         printf("Do you wish to continue [1/0]: ");
  69.         scanf("%d", &ch);
  70.     } while (ch != 0);
  71.     printf("\n");
  72. }
  73.  
  74. void release(struct node **head)
  75. {
  76.     struct node *temp = *head;
  77.     *head = (*head)->next;
  78.     while ((*head) != NULL)
  79.     {
  80.         free(temp);
  81.         temp = *head;
  82.         (*head) = (*head)->next;
  83.     }
  84. }

$ cc middlenode.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
 
The middle node of the list is: 3

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Note: Join free Sanfoundry classes at Telegram or Youtube
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.

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 & technical discussions at Telegram SanfoundryClasses.