Wednesday, January 23, 2013

Write a program that reverses alternate elements in a given linked list input: a->b->c->d->e, output should be b->a->d->c->e

Node reverseAlterante ( Node head) {
if ( head == null || head.next == null )
return head;
Node r = reverseAlternate(head.next.next);
Node t = head.next ;
t.next = head;
head.next = r;
return t ;
}

Monday, January 21, 2013

Printing all possible subsets using BitMask

#include<stdio.h>

void PrintAllSubset(int n)
{
int mask,pos,i,j,noOfSubSet = 1<<n; // subset size 2^n subsets
for(i=1;i<noOfSubSet;i++)
 {
for(j=0;j<n;j++)
if((i&(1<<j)) > 0)
printf("%d ",j+1);
printf("\n");
}
}
int main()
{
int n;
while(scanf("%d",&n) == 1)
{
PrintAllSubset(n);
}
return 0;
}