943,808 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 531
  • C RSS
Mar 28th, 2009
0

Linked list

Expand 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;
  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. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sat4ever0606 is offline Offline
6 posts
since Mar 2009
Mar 28th, 2009
0

Re: Linked list

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.
Reputation Points: 485
Solved Threads: 88
Posting Pro
csurfer is offline Offline
564 posts
since Jan 2009
Mar 28th, 2009
0

Re: Linked list

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...
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008
Mar 30th, 2009
0

Re: Linked list

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!!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sat4ever0606 is offline Offline
6 posts
since Mar 2009
Apr 1st, 2009
0

Re: Linked list

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.
Reputation Points: 128
Solved Threads: 43
Posting Whiz
death_oclock is offline Offline
389 posts
since Apr 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Output multiple files
Next Thread in C Forum Timeline: Question about my FUNC





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC