this is code from hw and i have no idea what is this sanity checker. i am not su
ID: 3768137 • Letter: T
Question
this is code from hw and i have no idea what is this sanity checker. i am not sure how to do make last function anyone can posslbie help me how to make this?
typedef struct node {
ElemType val;
struct node *next;
} NODE;
struct list_struct {
NODE *front;
NODE *back;
};
/* These are "sanity checker" functions that check to see
* list invariants hold or not.
*/
int lst_sanity1(LIST *l) {
if(l->front == NULL && l->back != NULL){
fprintf(stderr, "lst_sanity1 error: front NULL but back non-NULL ");
return 0;
}
if(l->back == NULL && l->front != NULL){
fprintf(stderr, "lst_sanity1 error: back NULL but front non-NULL ");
return 0;
}
return 1;
}
int lst_sanity2(LIST *l) {
if(l->back != NULL && l->back->next != NULL) {
fprintf(stderr, "lst_sanity2 error: back elem has a non-NULL next? ");
return 0;
}
return 1;
}
/*
* makes sure that the back pointer is also the last reachable
* node when you start walking from front.
* HINT: use pointer comparison
*/
int lst_sanity3(LIST *l) {
printf("lst_sanity3 not implemented ");
return 1;
}
Explanation / Answer
typedef struct node { ElemType val; struct node *next; } NODE; struct list_struct { NODE *front; NODE *back; }; /* These are "sanity checker" functions that check to see * list invariants hold or not. */ int lst_sanity1(LIST *l) { if(l->front == NULL && l->back != NULL){ fprintf(stderr, "lst_sanity1 error: front NULL but back non-NULL "); return 0; } if(l->back == NULL && l->front != NULL){ fprintf(stderr, "lst_sanity1 error: back NULL but front non-NULL "); return 0; } return 1; } int lst_sanity2(LIST *l) { if(l->back != NULL && l->back->next != NULL) { fprintf(stderr, "lst_sanity2 error: back elem has a non-NULL next? "); return 0; } return 1; } /* * makes sure that the back pointer is also the last reachable * node when you start walking from front. * HINT: use pointer comparison */ int lst_sanity3(LIST *l) { if(l->front != NULL && l->back->next != NULL) { printf(stderr, "lst_sanity2 error: he back pointer is also the last reachable"); return 0; } return 1; } Or int lst_sanity3(LIST *l) { if(l->front<l->back) { printf(stderr, "lst_sanity2 error: he back pointer is also the last reachable"); return 0; } return 1; }
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.