Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Deleting the first node in a linked list?

Name: SomeGuy 2007-11-10 13:36

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;}

    return L;
}
=============================================

How can I accomplish the same thing in a void function?

Name: Anonymous 2007-11-10 20:23

>>11
>>9 did it earlier, in paranthesis and using superior set.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List