string - Exam exercise about lists and pointers C -


i need fix exam exercise since teacher ask me how fix tomorrow @ 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 2 string equals (one in struct linked list , other k string) , erase string equal k, there 2 output cases wrong:

case 1:

k : dog

if insert 3 strings in : dog -> cat -> cat function doesnt erase "dog" , show me output: dog -> cat -> cat (correct output cat -> cat)

case 2:

another error found is: if list : dog -> dog -> cat output dog -> dog -> cat (right output should be: cat)

all other cases should work right.

struct :

struct nodo { char *chiave; struct nodo *prec; struct nodo *succ; }; typedef struct nodo nodo; 

rest of code is: (read comprehend part personal test; useless exam)

int main() { nodo *lista=createliststring(); // create list visualizza(lista); // views char *stringa="ciao"; // create k string lista=cancellatutto(lista,stringa); // call function visualizza(lista); // views } 

please note need fix this, not write code.

please dont @ overflows, errors, , such things in these function! fix first function! others personal test.

@bluepixy has given really strong hint in comments, going expand on:

case 1:

k : dog

if insert 3 strings in : dog -> cat -> cat function doesnt erase "dog" , show me output: dog -> cat -> cat (correct output cat -> cat)

case 2:

another error found is: if list : dog -> dog -> cat output dog -> dog -> cat (right output should be: cat)

in both of these cases, string want remove occurs @ head of list. cancellatutto needs return new head of list in case, not a1


1. if a supposed represent head of list, please use more meaningful name variable, such head or listhead (or whatever native language equivalent be).


Comments

Popular posts from this blog

how to do line continuation in perl debugger for entering raw multi-line text (EOT)? -

javascript - Create websocket without connecting -

sharepoint - Accessing files across a shared directory using a Windows service -