tags:

views:

885

answers:

5

Can anyone please explain this?

struct node 
{ 
    int data; 
    struct node * link; 
} 
main() 
{
    struct node *p, *list, *temp; 
    list = p = temp = NULL; 
    ......................... 
    ......................... 
} 

addbeg() 
{ 
    int x;
    temp=malloc(sizeof(struct node));
    scanf("%d", &x); 
    temp->data=x;
    temp->link = list;
    list=temp;
}

This is a code for entering data in linkedlist through 'C' language.The code is not complete, but, I think its enough for the purpose. Please explain the coding basically these lines:

 temp=malloc(sizeof(struct node));

and

 temp->link = list;
 list=temp;.
A: 

malloc allocates space for a new node.

temp->link = list

and

list = temp

makes that new node the head of your list.

William Pursell
A: 

first line: Allocate memory for a single additional node of the list.

second line: Attach the current list as the continuation after this element.

third line: Make the current element the beginning of a list.

EFraim
+8  A: 

malloc() is used to allocate memory - in this case for a new node.

The addbeg() function defined in your code does the following:

  1. Defines a temporary variable x.
  2. Allocates space for a new node.
  3. Inputs an integer (%d code to scanf) and stores it in x.
  4. Stores the value that was saved to x in the data field of the newly allocated node.
  5. Stores the old "head" of the list pointed to by the variable 'list' as a link in the newly allocated node.
  6. Sets the new node to be the new head of the list stored in the variable 'list'.

It's a very basic implementation of a linked list (http://en.wikipedia.org/wiki/Linked_list) of integers.

Amber
You could usefully add a note that the code should eventually ensure that the memory for each allocation is released with `free()`, muttering about 'memory leaks' and 'does not matter so much for small learning programs but does in big long-running ones', so you may as well learn how to do it right on small learning programs too.
Jonathan Leffler
his program won't work anyway though, unless `list` and `temp` are either passed to addbeg(), or addbeg() is defined within main.
Carson Myers
addbeg cannot be defined within main in standard C. Some compilers allow it as an extension, but it is not valid C.
William Pursell
Firstly the code is not completed, may be we could pass temp and list to addbeg() or may be I should declare temp and list outside main as global variable,but I pick this code from a book, cause I didn't understand it. Thank you very much for your replies.
Ryan
A: 

The operator 'sizeof' is used to calculate the size of datatype. The operator calculated how much space should be allocate on the structure node. malloc - allocate memory for new structure. Remember, if You allocate memory using malloc method, You always must to free this memory using free method.

free(tmp);

If you do not call this method, you will have the memory leak. This is very important in C.

[operator sizeof][1]http://en.wikipedia.org/wiki/Sizeof

mykhaylo
It is not necessarily true that sizeof( struct node ) == sizeof( struct node * ) + sizeof( int ). Internal padding can make a difference. Consider a struct containing a char and an int.
William Pursell
You have a right. It is not rule. Thanks for answer.
mykhaylo
A: 

temp and list aren't visible in addbeg, since they're declared in main. They either need to be passed in to addbeg or declared outside of main (global, yuck).

xcramps