C Puzzles

yet another place for C puzzles

Friday, July 22, 2005

 

MAX of two numbers without relational operators.


int max(int a,int b)
{
int g=0 ;

!a && b && ( ((b+1)/b ) && (g = b) || (g=a));
!b && a && ( ((a+1)/a ) && (g = a) || (g=b));

(!(!a || !b)) &&
( (( ( !((b+1)/b) && (!( a/b) ) )
|| ( ((a+1)/a) && (a/b) )
) && (g=a))
|| (g=b)
);
return g;
}


logic is here:

int max(int a,int b)
{
int g=0 ;
if (a == 0 and b != 0 ) {
if ( b is +ve)
b is greatest
else
a is greatest
}
if (b == 0 and a != 0 ) {
if ( a is +ve)
a is greatest
else
b is greatest
}
if ( a and b are non-zero ) {
if( [b is negative and b is magnitudely high]
or [a is positive and a is magnitudely high] )
then
a is greatest
otherwise
b is greatest
}
return the greatest value;
}

NOTE: this program will not work for a single value LONG_MAX;
Unit-test and other ideas are added as comments

Tuesday, July 19, 2005

 

Reverse a single linked list using single pointer iteratively and recursively


here is my solution.

____________________________________________________________________
(Recursive)
struct list * reverse_ll( struct list * head)
{
struct list * temp = NULL;

if ( head == NULL)
return NULL;
if ( head->next == NULL )
return head;

temp = reverse_ll ( head->next );
head->next -> next = head;
head->next = NULL;
return temp;
}

int main()
{
...
head = reverse_ll(head);
...
}
____________________________________________________________________
(Iterative)

#define pointer_swap( a, b) ((int)(a))^=((int)(b))^=((int)(a))^=((int)(b))

struct list * reverse_ll( struct list * head)
{
struct list * temp = NULL;
if (head && head->next) {
temp = head->next;
head->next = NULL;
} else {
return head;
}
while (temp->next) {
pointer_swap(head, temp->next);
pointer_swap(head,temp);
}
temp->next = head;
//head = temp;
return temp;
}
____________________________________________________________________



Harish's code. Minimized version of my code.
#define pointer_swap(a,b) ((int)(a)) ^= ((int)(b)) ^= ((int)(a)) ^= ((int)(b))

struct list * reverse_ll(struct list * head)
{
struct list * temp = NULL;

while(head) {
pointer_swap(temp,head->next);
pointer_swap(temp,head);
}
return temp;
}


Thursday, July 14, 2005

 

Misc Puzzles

Write a program whose printed output is an exact copy of the source.Needless to say, merely echoing the actual source file is not allowed.


char *p="char *p=%c%s%c;main(){printf(p,34,p,34);}";main(){printf(p,34,p,34);}


----------------------------------

Write a "Hello World" program in 'C' without using a semicolon.


int main(){if (printf("Hello World" )){}}


----------------------------------
Find the purpose of the following function.


int s_( char * _){ return _?*_? (1 + s_(_+1)):0:0;}



Ans: String length !!!!

Tuesday, July 12, 2005

 

Finding if there is any loop inside linked list

Best Solution
--------------

loop(node *ptr)
{
node *temp1,*temp2;
temp1=ptr;
if (temp1) {
temp2=temp1->next;
}

while(temp1 && temp2 && temp2->next && temp1 != temp2)
{
temp1==temp1->next;
temp2=temp2->next->next;
}

if(temp1==temp2)
printf("The loop exist\n")
else
printf("There exist no loop\n");
}


My solution
-----------
Just Pseudo Code only


list * visited_list[BIG_ARRAY] ={0};

int any_loops( list * p)
{
if ( p == null ) return 0; // means no loops
search for "p" in "visited_list"
if present
return 1; // means loop identified.
add p in "visited_list".
return any_loops (p->next)
}

You may use "visited_list" as another linked list instead of array.


Another solution
---------------
Regarding finding a loop inside a linked list, If setting a flag in the
node is allowed (which is initially reset) then the following will be
better.
This doesn't need recursion or any search inside the array which may be
time consuming.


int findloop(node_t *head)
{
while (head != NULL) {
     if (!head->visited) {
head->visited =1;
head = head->next;
}
else {
printf("found a loop in the linked list\n");
return 1;
}
}
printf("no loop found in the linked list\n");
return 0;
}


Archives

July 2005   August 2005   October 2005   December 2005   March 2006   June 2006   July 2006   December 2006   February 2007   June 2007   March 2010   May 2010  

This page is powered by Blogger. Isn't yours?

Subscribe to Posts [Atom]