Thread: Check if a singly linked list is palindrome

  1. #1
    Registered User
    Join Date
    Jul 2014
    Posts
    5

    Check if a singly linked list is palindrome

    Hi,

    I try to make a program to check if a linked list is palindrome, but I have one problem.
    When I compile, I don't know why but it doesn't show me the four lists.


    Check if a singly linked list is palindrome-screenshot-png

    The code show like this :

    Code:
    #include <stdio.h>#include <stdlib.h>
    
    
    #define ZERO    0
    #define ONE     1
    
    
    struct Node
    {
        char character;
        struct Node *next;
    };
    
    
    struct Node *createList(char *charPtr, int nr_01);
    void displayList(struct Node *head);
    
    
    int main()
    {
        struct Node *head = NULL;
        int nr_01 = 14, nr_02 = 14, nr_03 = 15, nr_04 = 16;
    
    
        char ch_01[] = "ABCDEFGGFEDCBA";
        char ch_02[] = "ABCDEFGJFEDCBA";
        char ch_03[] = "ABCDEFGJGFEDCBA";
        char ch_04[] = "ABCDEFGJTGFEDCBA";
        
        /// Function to create a linked list
        head = createList(ch_01, nr_01);
        /// Function to display the list
        displayList(head);
    
    
        head = createList(ch_02, nr_02);
        displayList(head);
    
    
        head = createList(ch_03, nr_03);
        displayList(head);
    
    
        head = createList(ch_04, nr_04);
        displayList(head);
    
    
        return 0;
    }
    
    
    struct Node *createList(char *charPtr, int nr)
    {
        struct Node *head = NULL, *current = NULL, *temp = NULL;
        int i, Flag = ONE;
    
    
        for(i=0; i<nr; i++)
        {
            if(Flag == ONE)
            {
                head = (struct Node*)malloc(sizeof(struct Node*));
                if(head == NULL)
                    printf("\n Memory allocation failed! \n");
                else
                {
                    head->character = charPtr[0];
                    head->next = NULL;
                    temp = head;
                }
                Flag = ZERO;
            }
            else
            {
                current = (struct Node*)malloc(sizeof(struct Node*));
                if(current == NULL)
                    printf("\n Memory allocation failed! \n");
                else
                {
                    /// store the character array's elements in character  
                    current->character = charPtr[i];
                    current->next = NULL;
                    temp->next = current;
                    temp = temp->next;
                }
            }
        }
        return head;
    }
    
    
    void displayList(struct Node *head)
    {
        struct Node *current = head;
        printf("\n ");
        while(current != NULL)
        {
            printf("%c->", current->character);
            current = current->next;
        }
        printf("NULL\n");
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > head = (struct Node*)malloc(sizeof(struct Node*));
    1. There's no need to cast the result of malloc in a C program.
    2. Your sizeof is wrong - you only allocate the size of a pointer, not the size of a node.

    Using this form saves this kind of mistake.
    head = malloc( sizeof(*head) );
    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.

  3. #3
    Registered User
    Join Date
    Jul 2014
    Posts
    5
    Yes, it works. Thank you!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Singly linked list c
    By Creotik in forum C Programming
    Replies: 5
    Last Post: 01-15-2014, 08:42 PM
  2. singly linked list
    By aextine in forum C Programming
    Replies: 2
    Last Post: 06-08-2010, 02:20 PM
  3. need help with singly linked-list
    By vearns in forum C++ Programming
    Replies: 20
    Last Post: 04-09-2008, 09:48 AM
  4. singly linked list to doubly linked list
    By t48j in forum C Programming
    Replies: 3
    Last Post: 03-23-2005, 06:37 PM
  5. singly linked list
    By Luigi in forum C++ Programming
    Replies: 1
    Last Post: 11-30-2002, 11:19 AM

Tags for this Thread