Reputation: 357
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);
}
there's a problem while dequeing it crashes and whenever I debug it it highlights the "f_temp = curr_f->pnext;" in the else if Dequeue part the dequeue part supposedly copies the data at the head and copy it to the f_temp node thus creating a new list
Upvotes: 0
Views: 66
Reputation: 404
This is where thing went wrong:
f_temp = curr_f->pnext;
curr_f = f_temp;
When your first DEQUEUE
, final_list
is NULL
, so
final_list = f_temp;
curr_f = f_temp;
gives that curr_f->pnext
is NULL
as f_temp->pnext
is also NULL
With the second DEQUEUE
, final_list
is not NULL
, so if
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
Then, in the third DEQUEUE
, curr_f
is NULL
so curr_f->pnext
crashes
Otherwise, probably it should be curr_f->pnext = f_temp
(enqueue the dequeued element to final_list
, right?)
Upvotes: 2