Advertisement
Guest User

cllpalindrome

a guest
Jun 8th, 2016
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.18 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdbool.h>
  4. #include <assert.h>
  5.  
  6. /* Linked list node data structure */
  7. typedef struct _ll_node_ {
  8.     int data;
  9.     struct _ll_node_ *next;
  10. } node;
  11.  
  12. /*
  13.  * @brief   Prints the state of the list
  14.  */
  15. void print_list(node *head)
  16. {
  17.     node *tmp = head;
  18.     int counter = 0;
  19.     while (tmp)
  20.     {
  21.         printf("Node:\t%d\tValue:\t%d\n", ++counter, tmp->data);
  22.         tmp = tmp->next;
  23.     }
  24.     printf("\n\n");
  25. }
  26.  
  27. /*
  28.  * @brief   Adds a node at the end of the list
  29.  */
  30. node *add_node(node *head, int data)
  31. {
  32.     node *tmp;
  33.     if (head == NULL)
  34.     {
  35.         head = malloc(sizeof(node));
  36.         assert(head != NULL);
  37.         head->data = data;
  38.         head->next = NULL;
  39.     }
  40.     else
  41.     {
  42.         tmp = head;
  43.         while (tmp->next)
  44.             tmp = tmp->next;
  45.         tmp->next = malloc(sizeof(node));
  46.         assert(tmp->next != NULL);
  47.         tmp = tmp->next;
  48.         tmp->data = data;
  49.         tmp->next = NULL;
  50.     }
  51.     return head;
  52. }
  53.  
  54. /*
  55.  * @brief   Checks to see if two lists are identical
  56.  */
  57. bool are_identical(node *headA, node *headB)
  58. {
  59.     if (headA == NULL && headB == NULL)
  60.         return true;
  61.     else if (headA != NULL && headB != NULL)
  62.         return (headA->data == headB->data &&\
  63.             are_identical(headA->next, headB->next));
  64.     else
  65.         return false;
  66. }
  67.  
  68. /*
  69.  * @brief   Utility to reverses a list
  70.  */
  71. node *reverse_list(node **head)
  72. {
  73.     node *prev = NULL;
  74.     node *curr = *head;
  75.     node *next;
  76.  
  77.     while (curr)
  78.     {
  79.         next = curr->next;
  80.         curr->next = prev;
  81.         prev = curr;
  82.         curr = next;
  83.     }
  84.  
  85.     return prev;
  86. }
  87.  
  88. /*
  89.  * @brief   Checks if a list is a palindrome or not
  90.  */
  91. bool is_palindrome(node *head)
  92. {
  93.     node *first;
  94.     node *second;
  95.     node *f_ptr = head;
  96.     node *s_ptr = head;
  97.     node *prev = NULL;
  98.     bool ret = false;
  99.  
  100.     while (f_ptr && f_ptr->next && f_ptr->next->next)
  101.     {
  102.         prev = s_ptr;
  103.         s_ptr = s_ptr->next;
  104.         f_ptr = f_ptr->next->next;
  105.     }
  106.  
  107.     /* List with even number of elements */
  108.     if (!(f_ptr->next->next))
  109.     {
  110.         first = head;
  111.         second = s_ptr->next;
  112.         s_ptr->next = NULL;
  113.         /* Reverse the second half */
  114.         second = reverse_list(&second);
  115.         /* Compare the first & second half */
  116.         ret = are_identical(first, second);
  117.         /* Finally, construct the original list back */
  118.         second = reverse_list(&second);
  119.         s_ptr->next = second;
  120.         print_list(head);
  121.     }
  122.     /* List with odd number of elements */
  123.     if (!(f_ptr->next))
  124.     {
  125.         first = head;
  126.         second = s_ptr->next;
  127.         prev->next = NULL;
  128.         s_ptr->next = NULL;
  129.         /* Reverse the second half */
  130.         second = reverse_list(&second);
  131.         /* Compare the first & second half */
  132.         ret = are_identical(first, second);
  133.         /* Finally, construct the original list back */
  134.         second = reverse_list(&second);
  135.         prev->next = s_ptr; s_ptr->next = second;
  136.         print_list(head);
  137.     }
  138.     return ret;
  139. }
  140.  
  141. /*
  142.  * @brief   Driver function
  143.  */
  144. int main(int argc, char *argv[])
  145. {
  146.     node *head = NULL;
  147.     int arr[] = { 1, 2, 3, 3, 2, 1};
  148.     int len = sizeof(arr) / sizeof(arr[0]);
  149.  
  150.     /* Populate the list */
  151.     for (int i = 0; i < len; i++)
  152.         head = add_node(head, arr[i]);
  153.  
  154.     /* Print the state of the list */
  155.     print_list(head);
  156.  
  157.     /* Algorithm to check if the list is a palindrome */
  158.     if (is_palindrome(head))
  159.         printf("The link is a palindrome\n");
  160.     else
  161.         printf("The list is NOT a palindrome\n");
  162.  
  163.     return 0;
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement