Line 15: initialize prev and prox to NULL or they will contain garbage.
Line 36: initialize sig to NULL for the same reason.
Line 72: You are leaking the contents of the list. You need to walk through the list and delete the items.
Line 90: delete this line. You allocate a nodo and then overwrite the only pointer to it on the next line, so you're leaking the memory.
Line 93: You may need to update plast here too.
Lines 100-102. You're using the pfirst pointer to iterate through the list, so when the method exits, pfirst will be NULL. Use a separate local variable to walk the list. pfirst should always point to the first item.
Line 121: same comment as line 72
Line 133: You need to update prim->prev->prox before this line
Line 138: You need to update ulti->prox->prev before this line
Line 146: Delete (same comment as line 90)
Now for the question you're asking about. Look at line 192 and 193. The first time through the loop you set n_nuevo = head->sig, thus leaking the original value of n_nuevo. Line 193 never changes the value of head->sig. The loop probably just continues for ever after that. Lines 198 and 199 are the same.
In addition, you aren't adjusting the tail pointer.
To fix this, I think you should start by creating a less-than operator for nodo that expresses the way they compare to each other. Once that's done you can use a nice little trick to insert into a linked list. Rather than keeping a pointer to the previous node in the list, you keep a pointer to the pointer to the node. In other words, a pointer to either head, or the previous node's next pointer. Doing this, the whole inserting process gets really easy:
1 2 3 4 5 6 7 8 9 10 11 12 13
|
void insertar3 ( string tarea , int dinero , int tiempo) {
nodo *n_nuevo = new nodo (tarea, dinero, tiempo);
nodo **pp, *p;
for (pp = &head; (p=*pp); pp = &p->next) {
if (*n_nuevo > *p) break;
}
n_nuevo->sig = *pp;
*pp = n_nuevo;
if (tail == NULL || pp = &tail->sig) {
tail = n_nuevo;
}
}
|