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中,如果Dequeue部分出列部分,则应该将数据复制到头部并将其复制到f_temp节点,从而创建一个新列表
答案 0 :(得分:2)
这是出错的地方:
f_temp = curr_f->pnext;
curr_f = f_temp;
当您的第一个DEQUEUE
,final_list
为NULL
时,
final_list = f_temp;
curr_f = f_temp;
认为curr_f->pnext
为NULL
,因为f_temp->pnext
也是NULL
对于第二个DEQUEUE
,final_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
然后,在第三个DEQUEUE
中,curr_f
为NULL
,因此curr_f->pnext
崩溃
否则,它应该是curr_f->pnext = f_temp
(将出列的元素排入final_list
,对吧?)