Friday, February 25, 2005

Reversing a linked list


#include <stdio.h>

typedef struct node {
int val;
struct node *next;
} Node;

Node* reverse (Node* head) {
Node* previous=NULL;
Node* current=head;
Node* follow=head->next;
Node* tmp;
if (head == NULL || follow == NULL)
return head;
while (follow->next != NULL) {
tmp=follow;
follow=follow->next;
tmp->next=current;
current->next=previous;
previous=current;
current=tmp;
}
follow->next=current;
current->next=previous;
return follow;
}

void printlist (Node* node) {
if (node==NULL)
return;
printf ("%d ",node->val);
printlist (node->next);
}

int main (int argc, char* argv[]) {
Node a,b,c,d;
Node *t;
a.val=1;
a.next=&b;
b.val=2;
b.next=&c;
c.val=3;
c.next=&d;
d.val=4;
d.next=NULL;
printlist(&a);
printlist(t);
printf ("\n");
return 0;
}

Wednesday, February 23, 2005

An observation of discussion forum

Recently, I came across a post in a discussion forum, it reported that the following question generated >3000 replies in a mainland discussion forum. Here is the question:

Richard bought a chicken using $8, he sold it for $9. He then bought it back using $10 and sell it again for $11. Question: How much does Richard earn?

I was surprised that such a question can generate >3000 replies, so I followed through the thread, which also has >100 replies, to try make something out of it. My observation is that
1) 10% of the replies are saying that the answer is 0, 1, or 3
2) 70% of the replies are reiterating the correct answer 2 using various mathematical/ economic/ accounting models.
3) 20% of the replies are simply junk.

Any conclusion from the above observations? Well, may be the following:
1) The web is being filled with junk.
2) People are not using their time wisely on the Internet (including me).
3) Anything can happen beyond your imagination on the Internet.

Sunday, February 20, 2005

Demetrio Albertini 2

A sequel to the Prelude
After spending one season in Lazio, Albertini was given a free transfer to Altanta, while he is going to end the professional soccer life in a miserable team, "miracle" happened. He was given a chance to transfer to Barcelona. It means that Albertini will appear in the Spanish league again in a team, which Milan had beaten 4-0 in the Super Cup exactly 10 years ago 1994 in Japan, after his first debut in Altheltico Madrid. Good luck to his career in Barcelona!

Thursday, February 17, 2005

Google Map

Here is a in-depth description of how google map works.

Saturday, February 12, 2005

Testing virtual functions, destructor and static_cast properties


#include <iostream>

class A {
public:
virtual void function () {
cout << "A" << endl;
}
~A () {
cout << "DA" << endl;
}
};

class B : public A {
public:
virtual void function () {
cout << "B" << endl;
}
~B () {
cout << "DB" << endl;
}
void f1 () {
cout << "F1" << endl;
}
};

class C : public B {
public:
virtual void function () {
cout << "C" << endl;
}
~C () {
cout << "DC" << endl;
}
};

int main(int argc, char* argv[]) {
A* a;
B* b;
C* c=new C();
a=c;
a->function();
b=static_cast<B*>(a);
b->f1();
delete b;
return 0;
}

Friday, February 11, 2005

do division without using / and %


#include <stdio.h>
#include <stdlib.h>


void division(int dividen, int divisor, int& quotient, int& remainder) {
int count=0;

if (divisor<=0 || dividen <=0)
return;

remainder=dividen;
while (divisor <= remainder) {
divisor<<=1;
count++;
}
for (int i=0;i<count+1;i++) {
remainder-=divisor;
if (remainder>=0) {
quotient<<=1;
quotient|=0x01;
} else {
remainder+=divisor;
quotient<<=1;
}
divisor>>=1;
}
}

int main (int argc, char** argv) {
if (argc!=3) {
printf ("Usage: %s dividen divisor\n",argv[0]);
return 0;
}
int a=atoi(argv[1]);
int b=atoi(argv[2]);
int c=0,d=0;
division(a,b,c,d);
printf ("dividen: %d divisor:%d quotient:%d remainder:%d\n",a,b,c,d);
return 0;
}

Print an integer using putchar only


#include <stdio.h>

int main (int argc, char** argv) {
int i=390487;
int base=10;
int remainder=0;
for (remainder=i/base;
remainder>0;
base*=10,remainder=i/base);
base=base/10;
remainder=i;
while (base>0) {
putchar('0'+remainder/base);
remainder=remainder-(remainder/base)*base;
base/=10;
}
putchar('\n');
return 0;
}

Reversing words in a sentence

#include <stdio.h>

int reverse(char* start, char* end) {
char tmp;
while (start < end) {
tmp=*start;
*start=*end;
*end=tmp;
start++;
end--;
}
return 0;
}

int main (int argc, char** argv) {

char test[]="A loves B";
char* start=&test[0];
char* end=start+strlen(test)-1;
char* begin;
char* stop;
reverse(start,end);
printf ("%s\n",test);
begin=start;
stop=start;
while (stop<end) {
while (*stop!=' ' && stop!=end)
stop++;
reverse(begin,stop-1);
begin=stop+1;
stop=begin;
}
printf ("%s\n",test);
return 0;

}

Multiplying a number by 7 without using *

#include <stdio.h>

int main (int argc, char **argv) {
int number=30;
printf ("%d\n",(number<<3)-number);
return 0;
}

Thursday, February 03, 2005

Old man and doctor

A 70-year-old man goes to the doctor's for a physical.

The doctor runs some tests and says to the man, ''Well, everything seems to be in top condition physically, but what about mentally? How is your connection with God?''

And the man says, ''Oh me and God? We're tight. We have a real bond, he's good to me. Every night when I have to get up to go to the bathroom, he turns on the light for me, and then, when I leave, he turns it back off.''

Well, upon hearing this the doctor was astonished.

He called the man's wife and said, ''I'd like to speak to you about your husband's connection with God. He claims that every night when he needs to use the restroom, God turns on the light for him and turns it off for him again when he leaves. Is this true?''

And she says, ''That idiot, he's been peeing in the refrigerator!''

Blonde and doctor

A blonde walks into a doctor's office and tells the doctor
she's broken every single bone in her body.
"That's impossible!" says the doctor.

The blonde says, "No, it's really true. Look!"
She then touches her leg with her index finger and screams "Ouch!"
Then she touches her arm and yells "Eeeeoooow!"
Finally she touches her ribs and can barely maintain her composure
as the tears start to roll down her face.
She says, "See, I told you I broke every bone in my body."

The doctor rubs his chin, then conducts a thorough examination.

"Well, miss," he tells her,
"I've got some good news and some bad news.
The good news is, you haven't broken every bone in your body.
The bad news is, you've broken your finger."

Life insurance

Richard is a sales agent for life insurance plan. One day he went to a training camp to promote the life insurance plan provided by his company. 100% of the soldier subscribe to the plan after this promotion talk. His advisor, John, want to know what makes Richard such a successful sales, so he decided to attend his talk next time.

Richard: "Hey buddies, if you unforturnately die in operation and had subscribed to our life insurance plan, the US government will reimburse your family memeber 200K dollars, otherwise, the government will only pay 6K dollars"

A soldier: "WTF, our lives are invaluable, no matter how much our family get back, they can't turn us alive!!!!!"

Richard replied: "Dude, think carefully, if a war really happen, which kind of soldiers do you think the US government will send there? with or withour insurance plan?"

.................The talk ends and again 100% of the soldiers subscribe to the life insurance plan.

Wednesday, February 02, 2005

If you don't see me for 3 days...........

On Friday morning, the husband went to work and get his salary as it's the last day of the month. He was so happy and hang out with his collegues throughout the weekend and spend all his moeny. He went back home on Sunday night. No doubt, his wife was very furious and angry at him.

Wife: "How do you feel if you don't see me for 3 days?"
Husband: "Well, that sounds good"

On Monday, he can't see his wife.....

On Tuesday, he can't see his wife .......

On Wednesday, he still can't see his wife ......

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
On Thursday, his swollen eyes get better and can barely see his wife again :)