GSXdan
10-19-2003, 03:11 PM
I have 5 remove code segments to remove books from a linked list. each book has a category, so the main list plus the 4 category lists. i can remove the book from the main list, but using the same code modified for one of the sublists causes a segmentation fault???
code for main list remove:
void library:: remove(char * target) {
Book * tptr;
Book * tptrF;
Book * tptrS;
Book * tptrC;
Book * tptrO;
tptr = ptFirstAll;
tptrF = ptFirstFiction;
tptrS = ptFirstScience;
tptrC = ptFirstChild;
tptrO = ptFirstOther;
char type;
while(tptr->title!=NULL) {
// check if book is last
if(tptr->ptAll == 0 && strcmp(tptr->title, target) == 0) {
tptr->ptAll = NULL;
countAll--;
type = tptr->type;
break;
}
// check if book is first
else if(strcmp(ptFirstAll->title, target) == 0) {
ptFirstAll = ptFirstAll->ptAll;
countAll--;
type = tptr->type;
break;
}
// check if book is in middle
else if(strcmp((tptr->ptAll)->title, target) == 0) {
tptr->ptAll = (tptr->ptAll)->ptAll;
countAll--;
type = tptr->type;
break;
}
tptr = tptr->ptAll;
}
and here is the code for the segment causing the problem:
if(type=='C') {
while(tptrC->title!=NULL) {
// check if book is last
if(tptrC->ptSub == 0 && strcmp(tptrC->title, target) == 0) {
tptrC->ptSub = NULL;
countC--;
return;
}
// check if book is first
else if(strcmp(ptFirstChild->title, target) == 0) {
ptFirstChild = ptFirstChild->ptSub;
countC--;
return;
}
// check if book is in middle
else if(strcmp((tptrC->ptSub)->title, target) == 0) {
tptrC->ptSub = (tptrC->ptSub)->ptSub;
countC--;
return;
}
tptrC = tptrC->ptSub;
}
}
anyone see what the problem is?
thanks ^dan
code for main list remove:
void library:: remove(char * target) {
Book * tptr;
Book * tptrF;
Book * tptrS;
Book * tptrC;
Book * tptrO;
tptr = ptFirstAll;
tptrF = ptFirstFiction;
tptrS = ptFirstScience;
tptrC = ptFirstChild;
tptrO = ptFirstOther;
char type;
while(tptr->title!=NULL) {
// check if book is last
if(tptr->ptAll == 0 && strcmp(tptr->title, target) == 0) {
tptr->ptAll = NULL;
countAll--;
type = tptr->type;
break;
}
// check if book is first
else if(strcmp(ptFirstAll->title, target) == 0) {
ptFirstAll = ptFirstAll->ptAll;
countAll--;
type = tptr->type;
break;
}
// check if book is in middle
else if(strcmp((tptr->ptAll)->title, target) == 0) {
tptr->ptAll = (tptr->ptAll)->ptAll;
countAll--;
type = tptr->type;
break;
}
tptr = tptr->ptAll;
}
and here is the code for the segment causing the problem:
if(type=='C') {
while(tptrC->title!=NULL) {
// check if book is last
if(tptrC->ptSub == 0 && strcmp(tptrC->title, target) == 0) {
tptrC->ptSub = NULL;
countC--;
return;
}
// check if book is first
else if(strcmp(ptFirstChild->title, target) == 0) {
ptFirstChild = ptFirstChild->ptSub;
countC--;
return;
}
// check if book is in middle
else if(strcmp((tptrC->ptSub)->title, target) == 0) {
tptrC->ptSub = (tptrC->ptSub)->ptSub;
countC--;
return;
}
tptrC = tptrC->ptSub;
}
}
anyone see what the problem is?
thanks ^dan