I ran into some trouble while attempting to remove a Node form a doubly linked list. While I can generally remove the nodes, the moment I attempt to remove the first element, it crashes my program and returns Error 3221225477.
I create my list with a header, like this:
typedef struct Inode2*Task;
typedef struct Inode2{
int ID;
int Priority;
int Status;
char *Description;
Person *person;
Date *creation;
Date *deadline;
Date *conclusion;
Task next;
Task previous;
}Task_node2;
Task TaskCreate() {
Task aux=(Task)malloc(sizeof(Task));
aux->next=NULL;
aux->previous=NULL;
return aux;
}
So as far as I'm aware, this is creating a list with a header, which is given to me to manipulate further.
I have a function to insert a node at the tail of this List. That seems to be working perfectly.
Whenever I use this removal function on the first element, it crashes:
int TaskRemove(Task h,int IDREMOVE) {
int val;
for(;h;h=h->next)
{
if (h->ID==IDREMOVE)
{
h->previous->next = h->next;
val++;
if (h->previous->previous==NULL)
{
h->previous->next = h->next;
}
}
}
if (val==0)
{
printf("\n\tNo node with such ID\n");
sleep(1);
}
return val;
}
This works on every element but the last. What's happening? Thanks in advance.
typedef struct Inode2*Task;oh, don't typedef pointers. Especially not to a strange name likeTask. Your code will be unreadable for others and - probably - also yourselfTask aux=(Task)malloc(sizeof(Task));. Should be (at least)Task aux=malloc(sizeof(struct Inode2));but better would be:Task_node2* aux=malloc(sizeof *aux);