i need to fix this exam exercise since my teacher will ask me how to fix this tomorrow at oral test:
nodo *CancellaTutto(nodo *a, char *k) {
nodo *p,*q;
p = a;
if (p == NULL)
return NULL;
while (p != NULL) {
if (strcmp(p->chiave, k) == 0 ) {
if (p->prec == NULL && p->succ == NULL)
return NULL;
if (p->succ == NULL && p->prec != NULL) {
q = p;
p = p->prec;
p->succ = NULL;
free(q);
}
if (p->prec == NULL && p->succ != NULL) {
q = p;
p = p->succ;
p->prec = NULL;
free(q);
}
if (p->prec != NULL && p->succ != NULL) {
q = p;
p = p->succ;
q->prec->succ = p;
p->prec = q->prec;
free(q);
}
} else { p = p->succ; }
}
return a;
}
This function should see if two string are equals (one in a struct linked list and the other is the k string) and erase all string equal to k, but there are two output cases that are obviously wrong:
CASE 1:
k is : DOG
if i insert 3 strings in a : DOG -> CAT -> CAT the function doesnt erase "DOG" and show me output: DOG -> CAT -> CAT (correct output is CAT -> CAT)
CASE 2:
Another error i found is: if a list is : DOG -> DOG -> CAT i get output DOG -> DOG -> CAT (right output should be: CAT)
All other cases should work right.
struct is :
struct nodo {
char *chiave;
struct nodo *prec;
struct nodo *succ;
};
typedef struct nodo nodo;
rest of code is: (Read only to comprehend this part is just for a personal test; useless for the exam)
int main()
{
nodo *lista=CreateListString(); // create a list
Visualizza(lista); // views it
char *stringa="ciao"; // create k string
lista=CancellaTutto(lista,stringa); // call function
Visualizza(lista); // views it
}
Please note that I need just to fix this, not to write another code.
Please dont look at overflows, errors, and such things in these function! Just fix the first function! others are for a personal test.