Re: Palindrome in Linked list
From: CBFalconer (cbfalconer_at_yahoo.com)
Date: 01/24/05
- Next message: Matt Gregory: "Re: Programming: Motivations?"
- Previous message: CBFalconer: "Re: Copying listboxes VB6"
- In reply to: amujoo_at_yahoo.com: "Palindrome in Linked list"
- Next in thread: Willem: "Re: Palindrome in Linked list"
- Reply: Willem: "Re: Palindrome in Linked list"
- Reply: Randy Howard: "Re: Palindrome in Linked list"
- Reply: moi: "Re: Palindrome in Linked list"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Mon, 24 Jan 2005 15:52:34 GMT
amujoo@yahoo.com wrote:
>
> TEchnical Interview question: given that each character of word
> is stored in each node of singly linked list. how would you
> determine that the word is a palindrome?
This should do it:
/* Subject: Palindrome in Linked list Date: 23 Jan 2005 23:41:57
-0800 From: amujoo@yahoo.com Organization:
http://groups.google.com Newsgroups: comp.programming
TEchnical Interview question: given that each character of word
is stored in each node of singly linked list. how would you
determine that the word is a palindrome? */
#include <stdio.h>
#include <stdlib.h>
typedef struct litem { struct litem *next; char ch; } litem,
*litemp; /* ------------------ */ static litem
*appendtolist(litem *oldlist, const char *str) { litem *new;
while (*str) { if (!(new = malloc(sizeof *new))) break; else {
new->next = oldlist; new->ch = *str++; oldlist = new; } } return
oldlist; /* fail by not appending */ } /* appendtolist */ /*
------------------ */ /* believed necessary and sufficient for
NULL terminations */ /* Reverse a singly linked list. Reentrant
(pure) code */ static litem *revlist(litem *root) { litem *curr,
*nxt;
if (root) { /* non-empty list */ curr = root->next; root->next =
NULL; /* terminate new list */ while (curr) { nxt = curr->next;
/* save for walk */ curr->next = root; /* relink */ root = curr;
/* save for next relink */ curr = nxt; /* walk onward */ } } /*
else empty list is its own reverse; */ return root; } /* revlist
*/ /* ------------------ */ static void showlist(litem *list) {
while (list) { putchar(list->ch); list = list->next; } } /*
showlist */ /* ------------------ */ static int qpalind(litem
*list) { char ch;
if (NULL == list) return 1; else { ch = list->ch; if (NULL ==
(list = list->next)) return 1; list = revlist(list); if (ch !=
list->ch) return 0; else { list = list->next; return
qpalind(list); } } } /* qpalind */ /* ------------------ */ int
main(int argc, char **argv) { litem *list; int i;
if (argc < 2) fprintf(stderr, "usage: qpalind string"); else { /*
form full list */ list = NULL; list = appendtolist(list,
argv[1]); for (i = 2; i < argc; i++) { list = appendtolist(list,
" "); list = appendtolist(list, argv[i]); } showlist(list); /*
check for palindrome */ if (qpalind(list)) printf(" is"); else
printf(" is not"); printf(" a palindrome\n"); } return 0; } /*
qpalind main */
-- "If you want to post a followup via groups.google.com, don't use the broken "Reply" link at the bottom of the article. Click on "show options" at the top of the article, then click on the "Reply" at the bottom of the article headers." - Keith Thompson
- Next message: Matt Gregory: "Re: Programming: Motivations?"
- Previous message: CBFalconer: "Re: Copying listboxes VB6"
- In reply to: amujoo_at_yahoo.com: "Palindrome in Linked list"
- Next in thread: Willem: "Re: Palindrome in Linked list"
- Reply: Willem: "Re: Palindrome in Linked list"
- Reply: Randy Howard: "Re: Palindrome in Linked list"
- Reply: moi: "Re: Palindrome in Linked list"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|