Thread: Checking If A Linked List Is Palindrome Or Not

  1. #1
    Registered User
    Join Date
    Aug 2016
    Posts
    3

    Checking If A Linked List Is Palindrome Or Not

    I am trying the check if a link is palindrome or not using the following method:

    1) Create and read the original linked list from user.
    2) Traverse through the original linked list and using the stack implementation using linked list reverse the original linked list.
    3) Initialize flag = 1. Traverse through both the linked lists and compare each node and break the loop and print "Not Palindrome" if any two node data doesn't match else print "Palindrome".

    The Program Is Given Below:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    int flag = 1;
    struct node {
     int data;
     struct node *next;
    }*start = NULL, *head = NULL;
    void create() {
     char ch;
     
     do {
      struct node *new_node, *prev;
      new_node = (struct node*)malloc(sizeof(struct node));
      printf("Please Enter The Data: ");
      scanf("%d", &new_node->data);
      new_node->next = NULL;
      if(start == NULL) {
       start = new_node;
      } else {
       prev->next = new_node;
      }
      prev = new_node;
      printf("Do You Still Want To Insert(y/n)? ");
      fflush(stdin);
      scanf("%c", &ch);
     }while(ch != 'n');
    }
    void reverse() {
     struct node *current;
     current = start;
     while(current != NULL) {
      struct node *new_node, *prev;
      new_node = (struct node*)malloc(sizeof(struct node));
      new_node->data = current->data;
      new_node->next = NULL;
      if(head == NULL) {
       head = new_node;
      } else {
       new_node->next = head;
       head = new_node;
      }
      prev = new_node;
      current = current->next;
     }
     
    }
    int checkPal() {
     struct node *current1, *current2;
     current1 = start;
     current2 = head;
     while(current1 != NULL && current2 != NULL) {
      if(current1->data != current2->data) {
       flag = 0;
       break;
      }
      current1 = current1->next;
      current2 = current2->next;
     } 
     if(flag = 1)
      return 1;
     else
      return 0;
    }
    void display(struct node *list) {
     while(list != NULL) {
      printf("%d --> ", list->data);
      list = list->next;
     }
     printf("NULL\n");
    }
    int main() {
     create();
     printf("The original linked list is: \n");
     display(start);
     reverse();
     printf("The reversed linked list is: \n");
     display(head);
     if(checkPal()) {
      printf("The Linked List Is Palindrome!\n");
     } else {
      printf("The Linked List Is Not Palindrome!\n");
     }
    }
    However, I am always getting "The Linked List Is Palindrome!" even when it is not! What did I do wrong?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    > if(flag = 1)
    Try using == instead.

    Also this
    FAQ > Why fflush(stdin) is wrong - Cprogramming.com
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. checking whether a string is palindrome or not..
    By Pulock2009 in forum C Programming
    Replies: 1
    Last Post: 11-04-2013, 09:31 PM
  2. Replies: 13
    Last Post: 09-22-2013, 10:34 PM
  3. Recursively Checking a Palindrome string
    By TheUmer in forum C Programming
    Replies: 19
    Last Post: 12-04-2009, 07:38 PM
  4. Palindrome checking
    By sea_wave in forum C++ Programming
    Replies: 5
    Last Post: 08-12-2009, 02:33 AM
  5. Replies: 6
    Last Post: 03-02-2005, 02:45 AM

Tags for this Thread