gpt4 book ai didi

c - c语言中的链表崩溃

转载 作者:太空宇宙 更新时间:2023-11-04 08:42:57 25 4
gpt4 key购买 nike

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;

当你的第一个DEQUEUE时,final_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

然后,在第三个DEQUEUE中,curr_fNULL,所以curr_f->pnext崩溃

否则,可能应该是curr_f->pnext = f_temp(将出队的元素入队到final_list,对吧?)

关于c - c语言中的链表崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22827069/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com