Linked list

Reply

Join Date: Mar 2009
Posts: 6
Reputation: sat4ever0606 is an unknown quantity at this point 
Solved Threads: 0
sat4ever0606 sat4ever0606 is offline Offline
Newbie Poster

Linked list

 
0
  #1
Mar 28th, 2009
For inserting a node at the beginning y the foll code wont work?
  1. struct node
  2. {
  3. int data;
  4. struct node *-next;
  5. }*head,*run,*ptr;
  6. typedef struct node n;
  7. void create()
  8. {
  9. if(head==NULL)
  10. {
  11. head=n*(malloc(sizeof(n));
  12. }
  13. else
  14. {
  15. ptr=n*(malloc(sizeof(n));
  16. ptr=head;
  17. scanf("%d",&ptr->data);
  18. head=ptr->data;
  19. ptr=ptr->next;
  20. }
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 476
Reputation: csurfer is just really nice csurfer is just really nice csurfer is just really nice csurfer is just really nice csurfer is just really nice 
Solved Threads: 76
csurfer's Avatar
csurfer csurfer is offline Offline
Posting Pro in Training

Re: Linked list

 
0
  #2
Mar 28th, 2009
Originally Posted by sat4ever0606 View Post
For inserting a node at the beginning y the foll code wont work?
  1. struct node
  2. {
  3. int data;
  4. struct node *-next;
  5. }*head,*run,*ptr;
Hey i think you missed out on the erreneous "-" sign behind next in
  1. struct node *-next;
It should be
  1. struct node * next;

And also the malloc is not used like this:
  1. head=n*(malloc(sizeof(n));
it should be as this:
  1. head=(n*)malloc(sizeof(n));
Last edited by csurfer; Mar 28th, 2009 at 1:44 pm.
I Surf in "C"....
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: Linked list

 
0
  #3
Mar 28th, 2009
This code does not wont work, you can't compile this code (feel the difference ).
  1. struct node *-next; /* Why minus sign? */
  1. typedef struct node n;
Well, you declare n as an alias of struct node type. Why?
  1. void create() {
  2. ...
  3. }
These function works with declared elsewhere global variables. Why?
  1. ptr=n*(malloc(sizeof(n));
  2. ptr=head;
Let's forget for a minute that n is a structure type, not a number. The 1st statement allocates memory and saves a pointer to the new object in ptr pointer variable. The next statement overwrites this value immediately. Why?

And so on...
It seems you must reread your C textbook right now...
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 6
Reputation: sat4ever0606 is an unknown quantity at this point 
Solved Threads: 0
sat4ever0606 sat4ever0606 is offline Offline
Newbie Poster

Re: Linked list

 
0
  #4
Mar 30th, 2009
fine tat was a typing mistake... ma doubt is hw 2 proceed..
first i mus allocate memory 4 creating the list right ???
so i did tat..
then the next time head is not null so i must traverse 2 d next node right so i gave such a declaration!!
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 357
Reputation: death_oclock will become famous soon enough death_oclock will become famous soon enough 
Solved Threads: 37
death_oclock's Avatar
death_oclock death_oclock is offline Offline
Posting Whiz

Re: Linked list

 
0
  #5
Apr 1st, 2009
Lets just take a look at this code:
  1. ptr=n*(malloc(sizeof(n));
  2. ptr=head;
  3. scanf("%d",&ptr->data);
  4. head=ptr->data;
  5. ptr=ptr->next;
You don't need line 1 because you just set it to point to a space of memory that has already been allocated ( head=n*(malloc(sizeof(n)); ). Lines 2 and 3 look alright to me. Line 4 doesn't work (I sure hope that doesn't compile) because head is a pointer (to a struct node) and you are trying to assign to it a plain old integer. Since ptr and head point to the same thing, line 3 takes care of what you want. Line 5 may seem to make sense, but it actually does nothing. At the end of this function call, ptr will be deleted. A few final notes: the first time you call this function, it will only allocate memory, not load any data. And you seem to be missing a closing } for the function.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC