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

Pages: 1-

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 13:41

die. linked lists are trivial.

Name: SomeGuy 2007-11-10 13:43

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: Anonymous 2007-11-10 13:51

(set! list (cdr list))

lolwut

Name: Anonymous 2007-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.

Name: Anonymous 2007-11-10 13:59

>>1
yes you can.
Pass 'list' by reference

the function prototype should look like this

void delete_head(list *);

and inside the body..

/* ... */
*L = temp->next;
/* ... */
return /* void */;

Name: Anonymous 2007-11-10 14:01

(setq list (rest list))

Name: Anonymous 2007-11-10 14:04

>>7
>>4 did it earlier, in code tags and using superior language.

Name: Anonymous 2007-11-10 14:28

>>8
(setq list (rest list))

Name: Anonymous 2007-11-10 14:30

>>9
>>4 did it earlier, in code tags and using superior language.

Name: Anonymous 2007-11-10 15:57

this is the superior solution (setf list (cdr list))

Name: Anonymous 2007-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: Anonymous 2007-11-10 20:23

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

Name: Anonymous 2007-11-10 20:28

>>13
lies. (> setf setq)

Name: Anonymous 2007-11-10 20:48

>>14
your mom sux dick

Name: Anonymous 2007-11-10 20:51

>>12
Yeah, because passing by reference is really fucking difficult...

Name: Anonymous 2007-11-10 21:52

You don't necessarily need a sentinel node. I think using a handle is a little bit more elegant.

My c is a little rusty but you want to do something like this:

=====================================================
struct node
{
    int element;
    node* next;
};

typedef node* list;

typedef list* list_handle;

//this code creates a new empty list
list_handle new_list()
{
  list_handle lh = malloc(sizeof(list_handle));

  *lh = NULL;

  return lh;
}


void delete_head(list_handle L)
{

    if(*L != NULL)
    {
        list temp;
        temp = *L;
        *L = temp->next;
        free(temp);
    }

}

=====================================================

The list handle is never null, it points to the head of the list, it points to null if the list is empty.

Name: Anonymous 2007-11-10 22:49

I think there is a bug in my code in the line:
list_handle lh = malloc(sizeof(list_handle));

I think it should be:
list_handle lh = (list*) malloc(sizeof(list));

I think I got it right this time...like I said my code is a little rusty and malloc can b confusing.

Name: Anonymous 2007-11-11 0:11

>>18
YOURE A FUCKING FAGGTO YLGETYGFH TEUFKC OYU OLUY JORLGOJ ERMTPORWJTIOFHU7ASDUIF DHBSDGUF DAHOFKLJDAYFHADUI FDGPAB FWAGFHP UADF ADBHIFLDAGH FBUASD9 FLADKFHUIDA9O8GH IFDAHFUDAIH F;ADO8FH ADUOIFHBDAUOIFBPDAOIUFBHADIOUFADHUF ADHFUDAOFH DABFUYADFHINAD FIUADNFUIADNFIDUANFIUDANFUIADHDFABFIUADBFGUIADH FUIDAHFIUDAHFUDAI FYADH8U9IFHAD980SFNZXCH0 89DACNAR0F8NQWE8RFN2348N3EFESAIOF DJF./DA,FYWDN FIETGELKJ RUEJCEKRYEMFM E MNTOEP MFYUDKCFE RFIA UDASDKAS DUFDASF0ANMD90-ASMNCA  MIOMOTEYCKE RUEF MFH OPJASPDKJAS 9FJADOFEWMOT EYURK CKEUR SKUFKUCKEUKRUEKFUKEURKUKCERUKUFKEUKCUEKUFKUCFKEUKFUCKEUFKUCEURK

Name: Anonymous 2007-11-11 6:28

DccChat* AddDccChat(char*Name,char*DccChatInfo,struct DccChat* DccChatList,int status)
{
    struct DccChat* Temporary = (DccChat*)s_alloc(sizeof(DccChat));
    memset(Temporary,0x0,sizeof(DccChat));
    sscanf(DccChatInfo,"\1DCC CHAT chat %u %d\1",&Temporary->ip,&Temporary->port);

    while (DccChatList->next != 0x0)
    {
        DccChatList = DccChatList->next;
    }

    DccChatList->ip = Temporary->ip;
    DccChatList->port = Temporary->port;
    DccChatList->timestarted = time(0);
    DccChatList->status = status;
    strcpy(DccChatList->Name,Name);

   
    DccChatList->next = (DccChat*)s_alloc(sizeof(DccChat));
    memset(DccChatList->next,0x0,sizeof(DccChat));
    DccChatList->next->previous = DccChatList;

    freememory(Temporary);

    return DccChatList;
}
void RemoveDccChat(unsigned int port,struct DccChat* DccChatList)
{   
   
    while (DccChatList->next != 0x0)
    {
        if (DccChatList->port == port)
        {
            if (DccChatList->previous)
            {
                if (DccChatList->next)
                {
                    DccChatList->previous->next = DccChatList->next;
                    DccChatList->next->previous = DccChatList->previous;
                }
                else
                    DccChatList->previous->next = 0x0;
               
                freememory(DccChatList);
               
            }
            else
            {

                if (DccChatList->next)
                {
                    DccCList = DccChatList->next;
                    DccCList->previous = 0x0;
                    freememory(DccChatList);
                }
                else
                {
                    memset(DccChatList,0x0,sizeof(DccChat));
                }
            }
            break;
        }
        else
        {
            DccChatList = DccChatList->next;
        }
    }   
}

Name: Anonymous 2007-11-11 7:04

>>20
ty seems good

Name: Anonymous 2007-11-11 14:35

>>19
ID guy would love that post.

Name: Anonymous 2007-11-11 14:53

>>22
You know, it's funny because it happends that i am >>19 and ID guy.

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