C 函数不';不归

C 函数不';不归,c,linked-list,C,Linked List,我已经检查了其他函数,我很确定没有缺陷,但每当我调试我的程序时,它总是突出显示“run=temp->pdown”段。此函数不返回-1如果找到密钥,它只返回。如果temp为NULL,run=temp->pdown,则这是用c语言编写的是无效的操作 struct m_queue{ int ndata; struct m_queue* pnext; struct m_queue* pdown; }; int search(struct m_q

我已经检查了其他函数,我很确定没有缺陷,但每当我调试我的程序时,它总是突出显示“run=temp->pdown”段。此函数不返回-1如果找到密钥,它只返回。如果
temp
NULL
run=temp->pdown,则这是用c语言编写的是无效的操作

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;

    }

问题在于这些方面:

}
temp = temp->pnext;
if (temp!=NULL)  //add this line
    run = temp->pdown;
temp
为空时会发生什么情况?您正在尝试访问
temp->pdown
,而不检查
temp
是否为空

      temp = temp->pnext;
      run = temp->pdown;

进行此更改以避免在temp为NULL时访问它。

在创建新元素时是否将pnext设置为NULL?。。。同样适用于
pdown
   //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
  }