struct m_queue{
int ndata;
struct m_queue* pnext;
struct m_queue* pdown;
};
int search(struct m_queue* list,int key){ //returns the index where it founded the key, return -1 if key is not found
struct m_queue* temp;//searches horizontal
struct m_queue* run;//searches downward
int i;
temp = list;
run = temp->pdown;
getch();
while(temp!=NULL){
getch();
while(run!=NULL){
if(run->ndata == key)
return temp->ndata;
else
run = run->pdown;
}
temp = temp->pnext;
run = temp->pdown;
}
printf("returning -1");
getch();
return -1;
}
i checked the other functions already and i'm pretty sure there's no flaw but whenever i debug my program it always highlight the segment "run = temp->pdown". this functions doesn't return -1 it just return if it found the key. this was coded in c language
If temp
is NULL
, run = temp->pdown;
is an invalid operation.
}
temp = temp->pnext;
if (temp!=NULL) //add this line
run = temp->pdown;
The problem lies in these lines:
temp = temp->pnext;
run = temp->pdown;
what happens when temp
is NULL? You are trying to access temp->pdown
without checking whether temp
is NULL.
//run = temp->pdown; //this statement not needed
getch();
while(temp!=NULL){
run = temp->pdown; //add this statement
getch();
while(run!=NULL){
if(run->ndata == key)
return temp->ndata;
else
run = run->pdown;
}
temp = temp->pnext;
// run = temp->pdown; // this statement not needed
}
make this change to avoid accessing temp when it is NULL.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.