954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

help needed with C program using a linked list to determine if a word is a palindrome

Hi,

i'm new to C programming and i could use some help with an assignment. i would like to store a word in a linked list, with each letter of the word in a separate node of the linked list. i would then like to traverse the linked list to check whether the word is a palindrome. the nodes in the palindrome should be double pointer nodes.

i'm not quite sure how to start here, any assistance will be appreciated.

red devils
Newbie Poster
4 posts since Jul 2008
Reputation Points: 10
Solved Threads: 0
 

First you have to declare a structure that is each node. If you search for "linked lists" you will get lots of examples, such as this tutorial by Stanford University.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

i'll check the tutorial and see if that helps.

red devils
Newbie Poster
4 posts since Jul 2008
Reputation Points: 10
Solved Threads: 0
 

First create a doubly link list. A doubly link list with each nodes having the address of next and previous node. The structure of the such node is as follows:

struct node {
        char data;
        node *next;
        node *prev;
};


Then write some functions to add, delete and traverse etc And what write functions for what u exactly want.

Luckychap
Posting Pro in Training
444 posts since Aug 2006
Reputation Points: 83
Solved Threads: 61
 

thanks. each node is supposed to store one character of the entered word. the first and last letters of the word would then be compared and if the same, then the second would be compared to the second to last etc. what would be the best way to do this?

red devils
Newbie Poster
4 posts since Jul 2008
Reputation Points: 10
Solved Threads: 0
 

Ok!!

One thing more, you have to maintain two pointers..head and tail. Which will have the addresses of first and last node respectively.

And write a function which will count the length of the link list (ie number of characters).

Once that is done you can loop through the list in this way:

//Assuming that head and tail are global
int isPalindrome() {
       node *tempHead = head;
       node *tempTail = tail;
     
       //function to calculate the length of link list
       int length = getLength();
       int i = 0;
       for(;i<length/2;i++) {
             if(tempHead->data != tempTail->data)
                  return 0;
       
             tempHead = tempHead->next;
             tempTail = tempTail->prev;
      }
      return 1;
}


Hope u got it.

Luckychap
Posting Pro in Training
444 posts since Aug 2006
Reputation Points: 83
Solved Threads: 61
 
Adak
Nearly a Posting Virtuoso
1,479 posts since Jun 2008
Reputation Points: 425
Solved Threads: 185
 

Thanks a whole lot man!

red devils
Newbie Poster
4 posts since Jul 2008
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You