Question about link list?

Page 1 of 1

4 Replies - 274 Views - Last Post: 21 June 2012 - 12:51 PM Rate Topic: -----

#1 jakes99  Icon User is offline

  • New D.I.C Head

Reputation: -10
  • View blog
  • Posts: 4
  • Joined: 21-June 12

Question about link list?

Posted 21 June 2012 - 12:05 PM

main()
{
/*function call*/
start = addatbeg(st, data);
start=del(st, data);}


insert
struct node *addatbeg(struct node *start, int data)
{
struct node *tmp;
tmp=(struct node *)malloc(sizeof(struct node));
tmp->info=data;
tmp->link=start;
start=tmp;
return start;
}



delete
struct node *del(struct node *start, int data)
{
struct node *tmp;
if(data == start->info)
{
tmp = start;
start = tmp->link;
free(tmp);
return start;
}
}


QUESTION:
start will have its own address suppose that address is 11d and start is a pointer that will store an address of the first node suppose the address of that first node is 100c.
Now look at the delete function. if we delete the first node.
tmp = start; this means that tmp is a pointer which will store the address of start address of start and address of start is 11d

start = start->link;
this means pointer start will now store the address of the next node. address of start is 11d. and in 11d memory location it should store the address of new node

Now we do free(tmp);
tmp contains the address of start which is 11d... if we are freeing 11d memory location then shouldn't all its data be erased in the previous step we assigned start=start->link so it should also have got erased..
Whats happening ?

This post has been edited by NickDMax: 21 June 2012 - 12:09 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Question about link list?

#2 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Question about link list?

Posted 21 June 2012 - 12:10 PM

This looks like a homework question to me.

What do you think is going on. Try to explain it to us and we can help you understand your answer.
Was This Post Helpful? 0
  • +
  • -

#3 jakes99  Icon User is offline

  • New D.I.C Head

Reputation: -10
  • View blog
  • Posts: 4
  • Joined: 21-June 12

Re: Question about link list?

Posted 21 June 2012 - 12:15 PM

View PostNickDMax, on 21 June 2012 - 12:10 PM, said:

This looks like a homework question to me.

What do you think is going on. Try to explain it to us and we can help you understand your answer.


think you didn't understand the question

View Postjakes99, on 21 June 2012 - 12:11 PM, said:

View PostNickDMax, on 21 June 2012 - 12:10 PM, said:

This looks like a homework question to me.

What do you think is going on. Try to explain it to us and we can help you understand your answer.


think you didn't understand the question



its using pointer to pointer. I am bit confused in understanding pointer to pointer thats what causing the confusion
Was This Post Helpful? -2
  • +
  • -

#4 jakes99  Icon User is offline

  • New D.I.C Head

Reputation: -10
  • View blog
  • Posts: 4
  • Joined: 21-June 12

Re: Question about link list?

Posted 21 June 2012 - 12:40 PM

View PostNickDMax, on 21 June 2012 - 12:10 PM, said:

This looks like a homework question to me.

What do you think is going on. Try to explain it to us and we can help you understand your answer.


This is just one of those guys who think they only know every thing and everyone else is a lamer. But this guy is the greatest dumb ass in the world..


This question was totally made my me and i have figured out the answer. The concept of double pointers is being used.
suppose the address of start is 11d and the address of st which is in main function is 5b .. when you are calling from main function you are assigning start = st through parameters.

Now we do tmp = start
Now tmp is a double pointer so it will not store 6b instead it will store 5b.
start = tmp->link .. address of new node assigned to start ..the address of start is 6b


free(tmp);
now temp is storing address 5b .. 5b actually the address of the node which is to be delete ie; first node so we are freeing that 5b memory location.

return start;
because start has the address of new node now we will return it to main function

This post has been edited by JackOfAllTrades: 21 June 2012 - 01:00 PM

Was This Post Helpful? -4
  • +
  • -

#5 jimblumberg  Icon User is offline

  • member icon


Reputation: 3993
  • View blog
  • Posts: 12,322
  • Joined: 25-December 09

Re: Question about link list?

Posted 21 June 2012 - 12:51 PM

*
POPULAR

That's enough of this. Topic closed.

Jim
Was This Post Helpful? 5
  • +
  • -

Page 1 of 1