Thursday, October 21, 2010

Few tricky code snaps on Linked List....


Few months back, when i was started for attending interviews to get into so called software industry, my thoughtprocess was not in line. Also because of language and other non-technical issues, every question was new and challenging to answer...!


At that point of time, i have decided to get benefited from all the interviews i attend. Thats how my notes called 'Notes from Interviews, Notes for Interviews' got its birth. At that point of time, i didn't have my technical blog.

So, now i am putting some of those into my blog to get remembered and to make you all remembered...!

Data structures(stack, queues ans Linked Lists) was one of my interesting subjects. Also because of this subject only i learned something about C language. Before that, i didn't know about what printf will do, what is getch() etc. One day before of my lab exam in 1st sem, i had written each lab program 150 times to get passed in exam.....!!!!!

Other interesting thing is, to understand DS
better, i have taken DS exam twise....!!!(?) (understood)

Here, i am sharing some tricky questions on Singly linked list.


Deletion of a node whose address only known in a singly linked list.


Node_Deletion(NODE ptr)
{
NODE next;
ptr->data = ptr->link->data;
next = ptr->link;
ptr->link = ptr->link->link;
free(next);
}


Reversing a singly linked list:

void Revrse_SSL(NODE first)
{
NODE ptr, pre=0, next;
ptr = first;
next = ptr->link;

while(ptr->link)
{
ptr->link = pre;
pre = ptr;
ptr = next;
next=next->link;
}
ptr->link = pre;
}


Finding out a middle node of a singly linked list;

FindMiddleNode(NODE first)
{
NODE current, mid;
int check = 0;
current=mid=first;

while(current)
{
current = current->link;
check = (check+1)%2;
if(check==0)
mid = mid->link;
}
return mid->info;
}

There could be some other ways to do same as above. If you would like to share, drop me a mail the same.

Always welcome to comments and suggestions...

Have a great career...

------

Wednesday, October 20, 2010

Remember about memcpy/memmove, strcpy/strncpy and malloc/calloc ....!


memcpy and memmove


These memory handling functions make us to confuse when anybody asks about the same. Better to keep in my blog for me and you...!

Among these, memmove handles memory overlapping but memcpy does not.

int main()
{
char str1[]="Mera Bharat Mahan";
char str2[40];
char str3[40];
memcpy (str2,str1,strlen(str1)+1);
memcpy (str3,"copy successful",16);
printf ("str1: %s\nstr2: %s\nstr3: %s\n",str1,str2,str3);
return 0;
}

Output is:
Mera Bharat Mahan
Mera Bharat Mahan
copy successful


Look at the following code snap.

int main ()
{
char str[] = "memmove can be very useful......";
memmove(str+20,str+15,11);
puts (str);
return 0;
}

Output is: memmove can be very very useful

If we change the above code to memcpy(), then ,

int main ()
{
char str[] = "memmove can be very useful";
memcpy(str+20,str+15,5);
puts (str);
return 0;
}

output: memmove can be very very l

In case of memcpy, when address gets overlapped, then the data copy process starts from the
eginning of src string (2nd parameter). Hence memcpy is not a better function to use when
here is a possibility of address overlaps.



strcpy() and strncpy()

strncpy(str1, str2, 10);

The major drawback of strncpy() is, when u r copying data from str2 to str1, if the number of characters if less
han or equal to string length of str2, then the NULL character of str2 will not be appended and hence the values
fter strlen(str2) will remains same as it is.


void main ()
{
char ptr[] = "Raghu";
char str[] = "Pramod Mathur";
printf("%s\n", strncpy(str,ptr,5));
}

Output will be ---- Raghud Mathur

void main ()
{
char ptr[] = "Raghu";
char str[] = "Pramod Mathur";
printf("%s\n", strncpy(str,ptr,6));
}

output will be ---- Raghu


malloc() and calloc()

The difference between malloc and calloc also could be one of the interview question. Even though if we have worked 4+ years in C programming itself, it doesnt mean that we definitely used calloc and many other functions. Also, it is in out hand itself about how we utilise the library functions. Better always to use most of the functionalities of C to becaome familiar.

When you allocate a memory to a pointer using calloc, be default, the content in the memory location, will be NULL. But whereas in the case of malloc, it is not. Thats the difference between malloc and calloc.

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

Please let me know if any of u found mistakes : drop a mail to pramoda.ma@gmail.com...

Gud luckckckckck......

----

Tuesday, October 12, 2010

C Program where, registers playing a role, unknowingly....!


Few days back, when I was assisting one of my colleagues in learning C language, one C program didn’t work as I expected. The program was to explain the stack concept in storing local variables.


main()
{
int a=10;
int b=20;
int *ptr;
ptr = &a;
ptr--;
printf("%d %d\n",*ptr);
}

Conceptually, a, b and ptr will be stored in stack because of its local scope. Hence address will decrement from a to b. As a result, *ptr should give the value of b. But at that point of time, it was not like that...! I was surprised...! I have tested in my machine as follows.

main()
{
int a=10;
int b=20;
int *ptr;
ptr = &a;
ptr--;
printf("%u %u %u\n", &a, &b, ptr);
printf("%d %d\n",*ptr);
}

When I tested this program, the output was as I expected. Say, 1000, 996, 996 and 20. Once again I surprised...! In my machine, the output is different and in my colleague's system, the output if different for the same program and same GCC compiler....!!!!!! Once again looked into both programs, there was no difference except address printing line. Thought that address printing line won’t affect the output.

But after some days, I have discussed this thing and I came to know that, if registers are available and if there is no explicit reference to the variables, then compiler could use CPU registers to store the local variables. Because of that only, stack operation result was reflecting in the output. When I try to print the address explicitly, then compiler should use the memory location to store the local variables. Hence stack operation was successful.

It doesn’t mean that, every compiler behaves like this. I found this behavior in GCC. There could be some other compilers which can use stack memory for storing local variables even though we don’t try to fetch the address of the variables. All depends on Compilers....

Gud luk...