using a linked list to store a word and determine whether its a palindrome
Question posted by: chungjoel
(Newbie)
on
July 1st, 2008 10:40 PM
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.
Would you like to answer this question?
Sign up for a free account, or Login (if you're already a member).
|
|
July 1st, 2008 11:05 PM
# 2
|
Re: using a linked list to store a word and determine whether its a palindrome
OK, how much do you know about structs and pointers (or references)? I'm guessing, you'll need that knowledge to create a linked list.
Now, I'll assume, you have your linked list, that looks something like this:
A <-> N <-> N <-> A
Then you can have a pointer to the first and last element. If the contents of both of these Elements are equal, go to the second first and second last and so on. If there's a difference somewhere, cancel. Then, you don't have a Palindrome.
Greetings,
Nepomuk
|
|
July 1st, 2008 11:35 PM
# 3
|
Re: using a linked list to store a word and determine whether its a palindrome
i know the theory behind linked lists but the thing is i've never created one before. is this how you would create simple linked list? if not can you please guide me here?
struct word {
char letter;
struct word *nodeptr;
};
in my program i would like to check one word and then either exit the program or check another word. if i choose to check another word, should i add to the back of the linked list (like in a queue)?
|
|
July 1st, 2008 11:51 PM
# 4
|
Re: using a linked list to store a word and determine whether its a palindrome
Quote:
Originally Posted by chungjoel
is this how you would create simple linked list? if not can you please guide me here?
Code: ( text )
struct word { char letter; struct word *nodeptr; };
|
Seems fine to me for a simple linked list (but I still am quite new to C/C++, so I might have missed something), but I thought you wanted "the nodes in the palindrome [to] be double pointer nodes"?
Quote:
Originally Posted by chungjoel
in my program i would like to check one word and then either exit the program or check another word. if i choose to check another word, should i add to the back of the linked list (like in a queue)?
|
If you did that, your linked list would become longer and longer and longer. Why do that? Why not basically create a new list every time you get a new word?
Greetings,
Nepomuk
PS.: Check out the CODE tags. The make code look so much better!
|
|
July 2nd, 2008 12:14 AM
# 5
|
Re: using a linked list to store a word and determine whether its a palindrome
Quote:
Originally Posted by
i would like to store a word in a linked list
|
Why? Are you required to do this, or is it a design choice you made yourself? I'll presume it's required, unless you say otherwise.
Quote:
Originally Posted by
the nodes in the palindrome should be double pointer nodes
|
Clarify. This is a nonsensical statement, and has to be interpreted to some reasonable approximation.
|
|
July 2nd, 2008 03:21 PM
# 6
|
Re: using a linked list to store a word and determine whether its a palindrome
Just realized, you made a mistake here:
Quote:
Originally Posted by chungjoel
Code: ( text )
struct word { char letter; struct word *nodeptr; };
|
should be
Code: ( text )
struct word { char letter; word *nodeptr; };
for a single sided list.
By the way, I've just written such a program in C++ and it works as suggested. I won't give you the code of course, but if you have specific questions, I can check them with my own version.
Greetings,
Nepomuk
|
|
July 2nd, 2008 03:25 PM
# 7
|
Re: using a linked list to store a word and determine whether its a palindrome
This doesn't sound too bad.
Your double pointer nodes allow a traverse of the list in either direction. This is a standard double-linked list.
Be careful about checking your palindrome sionce the palindrome may have several words: "Madam, I'm Adam" or "A Man A Plan A Canal Panama". In these cases the words themselves are not palindromes but the phrase is. That means you will probably have a separate list for each palindrome.
There are plenty of tutorials on how to do a linked list. It is certainly well worth your time you learn how to do this. I like the book Teach Yourself Data Structures and Algorithms by Robert LaFore.
|
|
July 2nd, 2008 05:45 PM
# 8
|
Re: using a linked list to store a word and determine whether its a palindrome
Hi again!
Something I realized is, that you can easily forget to ignore case (I'm guessing, you want A == a to be valid in your test) and punctuation (ignore dots, commas, etc.). For that, note that 'a' < 'z' as well as 'A' < 'Z' and 'A'-'a'=32.
When you've finished your program, of course you should test it. I'd recommend Weird Al Yankovic's song Bob where every single line is a Palindrome. ^^ (Of course, you can just enjoy the song on YouTube.)
Greetings,
Nepomuk
|
|
July 3rd, 2008 03:30 AM
# 9
|
Re: using a linked list to store a word and determine whether its a palindrome
this was really helpful. thanks a lot.
Not the answer you were looking for? Post your question . . .
183,905 Experts ready to help you find a solution.
Sign up for a free account, or Login (if you're already a member).
|
|
|
Latest Articles: Read & Comment
Top C / C++ Forum Contributors
|