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.
/*
* C Program to Check whether a Singly Linked List is a Palindrome
*/
#include <stdio.h>
#include <stdlib.h>
struct node
{
int num;
struct node *next;
};
int create(struct node **);
int palin_check (struct node *, int);
void release(struct node **);
int main()
{
struct node *p = NULL;
int result, count;
printf("Enter data into the list\n");
count = create(&p);
result = palin_check(p, count);
if (result == 1)
{
printf("The linked list is a palindrome.\n");
}
else
{
printf("The linked list is not a palindrome.\n");
}
release (&p);
return 0;
}
int palin_check (struct node *p, int count)
{
int i = 0, j;
struct node *front, *rear;
while (i != count / 2)
{
front = rear = p;
for (j = 0; j < i; j++)
{
front = front->next;
}
for (j = 0; j < count - (i + 1); j++)
{
rear = rear->next;
}
if (front->num != rear->num)
{
return 0;
}
else
{
i++;
}
}
return 1;
}
int create (struct node **head)
{
int c, ch, count = 0;
struct node *temp;
do
{
printf("Enter number: ");
scanf("%d", &c);
count++;
temp = (struct node *)malloc(sizeof(struct node));
temp->num = c;
temp->next = *head;
*head = temp;
printf("Do you wish to continue [1/0]: ");
scanf("%d", &ch);
}while (ch != 0);
printf("\n");
return count;
}
void release (struct node **head)
{
struct node *temp = *head;
while ((*head) != NULL)
{
(*head) = (*head)->next;
free(temp);
temp = *head;
}
}
$ 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.
Related Posts:
- Practice Design & Analysis of Algorithms MCQ
- Check Computer Science Books
- Practice Computer Science MCQs
- Check Data Structure Books
- Apply for Computer Science Internship