final* queue(struct node_q* list,struct m_queue* t_list){
    struct final_q* final_list;
    struct final_q* f_temp;
    struct final_q* curr_f;
    struct node_q* head;
    struct node_q* tail;
    struct node_q* temp;
    struct m_queue* m_temp;
    m_temp = t_list;
    int x,y,z;
    string20 w_nput;// word input
    head = list;
    tail = list;
    final_list = NULL;

    do{
    do{
    printf("ENQUEUE or DEQUEUE: ");
    scanf(" %s",w_nput);
    }while(strcmp(w_nput,"ENQUEUE")!=0 && strcmp(w_nput,"DEQUEUE")!=0 && strcmp(w_nput,"STOP")!=0);


    if(strcmp(w_nput,"ENQUEUE")==0){
        temp = malloc(sizeof(struct node_q));
        temp->pnext = NULL;
        printf("ENQUEUE what?: ");
        scanf("%d",&temp->ndata);
        if(list == NULL){
            list = temp;
            head = list;
            tail = temp;
        }
        else{
            x = search(m_temp,temp->ndata);

            if(x == -1){

                tail->pnext = temp;
                tail = temp;
            }
            else{
                y = search_index(m_temp,head,x);
                if(y == -1){
                    tail->pnext = temp;
                    tail = temp;
                }
                else{
                z = countqueue(list);
                if(y == z-1){
                    tail->pnext = temp;
                    tail = temp;
                }
                else
                list = insertnth(head,x,temp);


            }


            }


            }

        }
    else if(strcmp(w_nput,"DEQUEUE")==0){
        temp = head;
        head = temp->pnext;
        temp->pnext=NULL;
        list = head;
        f_temp=malloc(sizeof(struct final_q));
        f_temp->pnext = NULL;
        f_temp->ndata = temp->ndata;
        if(final_list==NULL){
            final_list = f_temp;
            curr_f = f_temp;
        }
        else{
            f_temp = curr_f->pnext;
            curr_f = f_temp;

        }
        free(temp);
        }

当隐藏它时出现问题,当我调试它时,它会突出显示“fútemp=currúf->pnext;”在else if Dequeue部分中,Dequeue部分应该复制头部的数据并将其复制到fútemp节点,从而创建一个新的列表

最佳答案

这就是问题所在:

f_temp = curr_f->pnext;
curr_f = f_temp;

当你的第一个DEQUEUEfinal_listNULL,所以
final_list = f_temp;
curr_f = f_temp;

给出curr_f->pnextNULL因为f_temp->pnext也是NULL
对于第二个DEQUEUEfinal_list不是NULL,所以如果
f_temp = curr_f->pnext; // curr_f->next is NULL, so f_temp is assigned to NULL
curr_f = f_temp; // re-assigned curr_f to NULL as f_temp is NULL

然后,在第三个DEQUEUEcurr_fNULL所以curr_f->pnext崩溃
否则,可能应该是curr_f->pnext = f_temp(将出列元素排队到final_list,对吗?)

09-28 11:34