Sunday, September 23, 2012

Never Give up !!!!




haste hue muskurate hue ghum raha tha mein,
laga sab kuch to mil gaya , masti mein jhum raha tha mein,
lekin hua jab duur is duniya ke jhoote dikhawo se,
kache rishto se, laalach ke chalawo se,
tab laga mere andar kuch to adhura h abhi,
kuch to hai jiski abhi bhi h mujhme kami,
kuch socha na samjha, bas chal pada kaato ki raho par,
mila dard hi dard jab pade paav in angaro par,
magar mile chahe kitna bhi dard,
nahi chorunga mein in raaho ko, lehro se bhari samundar ki in baho ko,
kyuki naseeb hote h sirf unhi gine chuno ko kinaare,
jo nirasha ke andhkaar se kabhi nahi h haare...........

keep going and never ever stop ...........

myself as Messi.......




college me mein engineer banne aaya tha,
bade bade sapno ko sach karne aaya tha,
lekin dekha jab use dimag ghum gaya,
padhai se dhyan hata dil jhum gaya,
lekin pata chala uska pehle se hi koi boyfriend tha,
jodi bhi achi thi dono ka pyaar bhi durust tha,
magar agar wo tha goal keeper to mein bhi ban gaya Lionel Messi,
aakhir kaise jane deta, ladki bhi thi kareena jaisi,
baad me pata chala ek nahi do nahi kaafi h uske liye excited,
ek akela Messi kya kare, samne h pura Manchester United............. ;-)... :P


Monday, September 17, 2012

Why use double pointer when adding node to Linked List?

Why use double pointer when adding node to Linked List? This question struck my mind when i first read about Linked Lists in my Second Semester. At that time i didnt gave it much thought. But from past 1 week i wanted the answer of this question. I googled it, but forums were full of nasty definitions of pointers and their usage, asked my seniours but they told me the bookish reasons which didnt made me satisfied, so i decided to do some experiments and make some conclusion......

OBSERVATION 1- If the linked list is not empty then we can add the nodes in it (obviously at the end) by using a single pointer only.

int insert(struct LinkedList *root, int item)
{
    struct LinkedList *temp;
    temp= (struct LinkedList *)malloc(sizeof(struct LinkedList));
    temp->data=item;
    temp->next=NULL;
    struct LinkedList *p;
    p=root;
    while(p->next!=NULL)
    {
        p=p->next;
    }
    p->next=temp;
    return 0;
}


int main()
{
    int m;

    struct LinkedList *A;

    A=(struct LinkedList *)malloc(sizeof(struct LinkedList));
    //now we want to add one element to the list so that the list becomes non-empty
    A->data=5;
    A->next=NULL;
    cout<<"enter the element to be inserted\n";
    cin>>m;
    insert(A,m);
    return 0;
}


Its simple to explain. We have a pointer in our main function which points to the first node (or root) of the list. In the insert() function we pass the address of the root node and using this address we reach the end of the list and add a node to it. So we can conclude that if we have address of a variable in a function (not the main function) we can make permanent changes in the value of that variable from that function which would reflect in the main function.



OBSERVATION 2- The above method of adding node failed when the list was empty.


int insert(struct LinkedList *root, int item)
{
    struct LinkedList *temp;
    temp= (struct LinkedList *)malloc(sizeof(struct LinkedList));
    temp->data=item;
    temp->next=NULL;
    struct LinkedList *p;
    p=root;
    //check if list is empty
    if(p==NULL)
    {
        p=temp;
    }
    else
    {
      while(p->next!=NULL)
      {
          p=p->next;
      }
      p->next=temp;
    }
    return 0;
}



int main()
{
    int m;

    struct LinkedList *A;

    A=NULL;//initialise the list to be empty
    cout<<"enter the element to be inserted\n";
    cin>>m;
    insert(A,m);
    return 0;
}


If you keep on adding elements and finally display the list then u would find that the list has undergone no changes and still it is empty.
The question which struck my mind was in this case also we are passing the address of the root node then why modifications are not happening as permanent modifications and list in the main function undergoes no changes. WHY? WHY? WHY? 
Then i observed one thing, when i write A=NULL the address of A becomes 0. This means now A is not pointing to any location in memory. So i removed the line
A=NULL; and made some modification in the insert function... 





some modifications......(below insert() function can add only one element to an empty list,, just wrote this function for testing purpose)




int insert(struct LinkedList *root, int item)
{
    root= (struct LinkedList *)malloc(sizeof(struct LinkedList));
    root->data=item;
    root->next=NULL;
    return 0;
}



int main()
{
    int m;

    struct LinkedList *A;
    cout<<"enter the element to be inserted\n";
    cin>>m;
    insert(A,m);
    return 0;
}

the above method also fails because in the insert() function root stores same address as A in the main() function but after the line root= (struct LinkedList *)malloc(sizeof(struct LinkedList)); the address stored in root changes. Thus ,now , root (in insert() function) and A (in main() function)  store different addresses.

SO THE CORRECT FINAL PROGRAM WOULD BE...


int insert(struct LinkedList *root, int item)
{
    root->data=item;
    root->next=NULL;
    return 0;
}



int main()
{
    int m;

    struct LinkedList *A;
    A= (struct LinkedList *)malloc(sizeof(struct LinkedList));
    cout<<"enter the element to be inserted\n";
    cin>>m;
    insert(A,m);
    return 0;
}

But we dont want two different functions for insertion , one when list is empty and other when list is not empty. Now comes double pointer which makes things easy.

now pls give ur full attention,
lets see what double pointer can do,

ONE THING I NOTICED WHICH IS IMPORTANT IS THAT POINTERS STORE ADDRESS AND WHEN USED WITH '*' THEY GIVE VALUE AT THAT ADDRESS BUT POINTERS THEMSELVES HAVE THEIR OWN ADDRESS.
for example-
if we write int *a,p; a=&p; then *a would give value of p, a would give address of p, and &a would give address of pointer a.

now i would first give the complete program and then explain the concepts.....


int insert(struct LinkedList **root,int item)
{
    if(*root==NULL)
    {
        (*root)=(struct LinkedList *)malloc(sizeof(struct LinkedList));
        (*root)->data=item;
        (*root)->next=NULL;
    }
    else
    {
        struct LinkedList *temp=(struct LinkedList *)malloc(sizeof(struct LinkedList));
        temp->data=item;
        temp->next=NULL;
        struct LinkedList *p;
        p=*root;
        while(p->next!=NULL)
        {
            p=p->next;
        }
        p->next=temp;
    }
    return 0;
}


int main()
{
    int n,m;
    struct LinkedList *A=NULL;
    cout<<"enter the no of elements to be inserted\n";
    cin>>n;
    while(n--)
    {
        cin>>m;
        insert(&A,m);
    }
    display(A);
    return 0;
}



following are the observations->
1. root stores the address of pointer A (&A) , *root stores the address stored by pointer A and **root stores the value at address stored by A. In simple language root=&A, *root= A and **root= *A.
2. if we write *root= 1528 then it means that value at address stored in root becomes 1528 and since address stored in root is the address of pointer A (&A) thus now A=1528 (i.e. address stored in A is 1528) and this change is permanent.

 whenever we are changing value of *root we are indeed changing value at address stored in root and since root=&A ( address of pointer A)  we are indirectly changing value of A or address stored in A.

so now if A=NULL (list is empty) *root=NULL , thus we create the first node and store its address at *root  i.e. indirectly we storing the address of first node at A. If list is not empty , everything is same as done in previous functions using single pointer except we have changed root to *root since what was stored in root is now stored in *root.



Thursday, September 13, 2012

Kudiyo ke Nakhre.....




moh maaya me kyu phaste ho tum,
uski adaao pe kyu marte ho tum,
aamir khan ki kahi baat bhul gaye kya,
bus,train to thik h magar uske nakhro ko kyu sehan karte ho tum.

din raat padh kar bhi wo jitna nahi janti,
bina padhe usse jyada jaante ho

 tum,
khud ko bada creative samajhti h wo,
lekin innovation ke maamle me unke baap ho tum.

ek dusre ko saali , kutti , kamini bol kar
sochti h Oh my God aaj to kuch jyada hi bol diya,
unhe kya pata jo wo soch bhi nahi sakti
usse bhi achi gaaliyo ke inventor ho tum.

ghanto make up karke jitna chamakti h wo,
bina nahaye usse jyada chamakte ho tum,
8 ghante sone ke baad bhi kehti h i am sleepy,
3 ghanta so kar bhi padhai plus masti karte ho tum.

40 km/hr par scooty kya chala li,
khud ko samajhti h Rani Lakshmi Bai,
R.S. ke 2 pack aur sutta lagane ke baad bhi,
90 ki speed pe bike chalate ho tum.

2-4 comment kya maar diye,
bolti h wo tumko character less,
lekin train me waiting ticket me jab seat nahi milti,
to character less se unke best friend ban jaate ho tum.

moh maaya me kyu phaste ho tum,
uski adaao pe kyu marte ho tum,
aamir khan ki kahi baat bhul gaye kya,
bus,train to thik h magar uske nakhro ko kyu sehan karte ho tum.