(1) You have a memory leak in list_left_to_right()
:
nod *p = (nod*) malloc(sizeof(nod));
p=prim;
This leaks the block returned by malloc()
.
(2)
void insert_element(int numb) {
nod *local=(nod *)malloc(sizeof(nod));
local->number = numb;
// TODO: set local->urm and local->prec to NULL
if (prim==NULL) {
prim=local;
ultim=local;
OK, so the first time insert_element()
is called, the new element is both the head and the tail.
Bug: You need to set the urm
and prec
fields to NULL. They have undefined values initially.
}
else {
ultim->urm = local;
local->prec = ultim;
ultim=local;
}
}
After that, the subsequent elements are inserted as a new tail (ultim).
Bug: But again you need to make sure that local->urm is set to NULL.