Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > beginners problem

Reply
Thread Tools

beginners problem

 
 
SM Ryan
Guest
Posts: n/a
 
      02-11-2008
# int main()
# {
# Node *nod = NULL;
# insert(&nod, 4); /* $ */
# printf("%d\n", nod->data);
# printf("%10p\n", (void *) nod); /* $ */
# printf("%10p\n", (void *) nod->next); /* $ */
# return 0;
# }

# void insert(Node ** head /* $ */ , int n)
# {
# Node *tmp;
# if (*head == NULL) { /* $ */
# *head = alloc_node(n); /* $ */
# }
#
# else {
# tmp = alloc_node(n);
# (*head)->next = tmp; /* $ */
# }
# }

I prefer an applicative style. An equivalent to the above would be
...
nod = insert(nod,4);
...
Node *insert(Node * head, int n)
{
Node *tmp;
if (head == NULL) { /* $ */
head = alloc_node(n); /* $ */
}
else {
tmp = alloc_node(n);
head->next = tmp; /* $ */
}
return head;
}

There may be a marginal difference that an applicative style is
easier for the compiler to optimise, but overall it is question
of which is easier for you to program in.

For example if you were to implement a stack, one might do pushes
push(&stack,a);
push(&stack,b);
...
or
stack = push(stack,a);
stack = push(stack,b);
...
or
stack = push(push(push(stack,a),b),...);

--
SM Ryan http://www.rawbw.com/~wyrmwif/
I ASSURE YOU WE'RE OPEN!
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Beginners Problem - Static reference to non-static method William Colls Java 4 03-20-2012 06:28 PM
wxPython beginners problem Ivan Reborin Python 5 08-16-2008 07:30 PM
Beginners Query - Simple counter problem David Barr Python 9 09-07-2007 04:52 AM
how to use stoi? which *.h to use? trivial beginners problem... Zahpod C Programming 4 04-21-2006 06:58 PM
Beginners Guides: Website Hosting With Apache Silverstrand Front Page News 0 10-24-2005 01:44 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57