DarkLord
BAN USER@Vipul...We can do it without modifying the linked list through the use of recursion...
int ispalindrome(struct node **head, struct node *head1)
{
if(head1 == NULL)
return 1;
int flag = ispalindrome(head,head1-next);
int x;
if(flag != -1)
return 0;
if(head1->data == (*head)->data)
x =1;
*head = (*head)->next;
return x;
}
int ref[] = {2,3,6,7,8};
void printcombination(int n,int index,int i)
{
static int a[100];
int j;
if (n == 0)
{
for(j=0;j<index;j++)
printf("%d ",a[j]);
printf("\n");
}
else if(n>0)
{
for(j=i;j<5;j++)
{
a[index]=ref[j];
printcombination(n-ref[j],index+1,j);
}
}
}
main()
{
int n;
printf("enter value of n :");
scanf("%d",&n);
printcombination(n,0,0);
}
Replarryehickl, Associate at ABC TECH SUPPORT
I am a blogger in the Geek system operator . As an editor with a strong background in english and hindi ...
@siva
- DarkLord June 08, 2011yes as pointed by nee.agl it can be resolved by taking a counter and even if not the complexity is not affected....
@Martin
consider an example 1->2->2->1
if u see the recursion, head1 reaches last 1. and it is matched with head(which is pointing to beginning 1).if it matches, it returns 1. but before that head is made to point the second element i.e 2.
when the function returns,head1 is pointing second last element i.e 2 and head is pointing to second element 2. it matches and hence head is again incremented...
the process continues..try to visualise this using a stack where the stack gets filled by the elements pointed by head1...
I hope I am clear