OK, so I'm learning linked lists in my data struct class and we will eventually be using linked lists to implement a STACK. The professor gave us a few of the functions for manipulating the list such as insert_node, add_node, etc. He went over how the deletion functions should work, but he never wrote any actual code. Instead he said that he will leave that up to us.
In explaining deletion routines, he said you should have three functions for deletion; one to delete the head of the list, one to delete nodes in the middle, and one to delete the tail of the list.
I've been able to come up with such routines on my own, but I don't like that fact that they return something. I would like to have them be void functions instead of returning something BECAUSE like nearly all, if not all, of the functions he's showed off for list manipulation have been void functions.
SO THE QUESTION IS: Is there a way to delete the head of the list and make the following node the new head ALL inside of a void function(meaning nothing is returned to the main)?
I came up with this:
=====================================================
struct node
{
int element;
node* next;
};
typedef node* list;
list delete_head(list L) //delete list head
{
if(L->next != NULL)
{
list temp;
temp = L;
L = temp->next;
free(temp);
}
else{free(L);return NULL;}
C'mon guys. You don't have to show me how it's done. A simple yes or no will do. I've looked around the interwebs at how other people delete the head, but none of them were using void functions.
Name:
Anonymous2007-11-10 13:51
(set! list (cdr list))
lolwut
Name:
Anonymous2007-11-10 13:55
You don't really know anything about C, do you?
Read some fucking kindergarten textbook. Simple answer is that any «function» can become a «procedure» in Pascal terms if you remove the return statement.
>>9 >>4 did it earlier, in code tags and using superior language.
Name:
Anonymous2007-11-10 15:57
this is the superior solution (setf list (cdr list))
Name:
Anonymous2007-11-10 20:19
>>1
The easiest way to do that is to create a head sentinel node. Basically, have a node at the front of the list that you never use, but it makes deleting the first REAL node of the list the same operation as deleting any other node of the list except the last one. Because you never change the first node refered to, you don't need to pass the list by reference.
Name:
Anonymous2007-11-10 20:23
>>11 >>9 did it earlier, in paranthesis and using superior set.