I'm not sure what you want the "dlist_position" to be, but what you're doing in that insert won't work at all. How you have it coded right now is very likely to crash when executing newPosition->next = p->next as the first time it's called p is NULL (likely; possibly some random value depending on compiler/optimizations, etc) and you can not execute p->next through the NULL pointer.
I would assume you want "dlist_position" to be a node in the list (maybe consider calling it dlist_node?). If that's the case, you'll have to special case when you want the new data to be inserted as the first position in the list.
I suggest allowing dlist_insert to special case when p == NULL, handling it correctly by linking the new position to the head of l. Then the very first call:
becomes:
Having no idea what the structure for dlist is, however, we can't help any more.
edit: also, dlist_position newPosition=malloc(sizeof(element)); looks odd -- I can only hope that dlist_position is just typedef'd as a pointer to an element...but, again, not enough code to tell.
Last edited by DreamWarrior; 01-30-2012 at 07:06 PM..
yes that is exactly what I want. I am going to try to do what you mentioned, althought I am not sure if I understood the part of the special case.
"....have to special case when you want the node to be inserted as the first position in the list. Maybe, in that case, you should pass the p variable as NULL and then check for NULL in dlist_insert and handle it correctly by linking it to the head of l...."
Could you elaborate a bit on that please? Thank you again!
yes that is exactly what I want. I am going to try to do what you mentioned, althought I am not sure if I understood the part of the special case.
"....have to special case when you want the node to be inserted as the first position in the list. Maybe, in that case, you should pass the p variable as NULL and then check for NULL in dlist_insert and handle it correctly by linking it to the head of l...."
Could you elaborate a bit on that please? Thank you again!
Not really, because I have no idea what the dlist structure looks like. But, I'd guess you have a head pointer in dlist. So:
I'm also curious why you're returning p and not newPosition; I'd figure you should probably return the node you just inserted...no?
At this point, I think this may be the extent to which I can assist. At this very basic level, it smacks of your being in an algorithms class. At which point, I'm wondering if this isn't homework. I don't want to be a jerk, but if you're going to succeed in this industry, you will eventually need to have these basics figured out. So, before I assist any more, I want to see a little more thought put in by yourself. You can get there....
Hi, thanks for your help, and excuse me for being so vague, it was late for me yesterday and I just hit the wall... I am just trying to learn how to use this data structure. I will post a more thoughtful question next time.
i have a linked list set up like
typedef struct client_list {
char *client_name;
int client_socket_fd;
struct client_list *next;
} client;
client *client_list=NULL;
before adding to the list i check if it already exists, only if it does not then i add
if (client_list==NULL... (1 Reply)
Suppose to have:
struct Tstudent
{
string name, surname;
int matriculation_num;
};
struct Tnode
{
Tstudent* student;
Tnodo* next;
} L;I want to deference that "student" pointer. For example, I tried with:
*(L->student).matriculation_numbut it not worked, as terminal... (4 Replies)