C Program to Check if Singly Linked List is Palindrome

This C Program checks whether the singly linked list is a palindrome. A palindrome is a pattern in list in which the contents when read from front is the same as when read from last.

Here is source code of the C Program to check whether a singly linked list is a palindrome. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

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

$ cc linklistpalin.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: 3
Do you wish to continue [1/0]: 1
Enter number: 2
Do you wish to continue [1/0]: 1
Enter number: 1
Do you wish to continue [1/0]: 0
 
The linked list is a palindrome.

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.