Insert node in a linked list.
Hi everybody!
I asked yesterday something about my project: TOPIC
and now that I have almost finished I have problems with the last function that inserts a new line after the current. Here there is the function:
Code:
void InsertAfter(struct list *buffer, char *newline)
struct node *p=MALLOC(sizeof(*p)); //line to be inserted
p->line=newline;
struct node *n=buffer->head; //current line
int i;
for(i=0;i<(buffer->currentpos)-1;++i){ //to fint the current line
n=n->next;
}
p->prev=n;
p->next=n->next;
n->next->prev=p;
n->next=p;
++buffer->size;
}
here the structs:
Code:
struct node {
char *line;
struct node *prev;
struct node *next;
};
struct list {
struct node *head;
struct node *tail;
int size;
int currentpos;
char *current;
};
It gets compiled, and runs perfect, but when I have inserted a new line, and whant to print the file via the function WriteFile which prints the buffer->head the buffer->head-> next etc, I get segmentation fault. I think the pointers do not align correctly, but I don't know why.
Can somebody help me, please?