[英]“Segmentation fault (core dumped)” in implementation of queue using linked list
I wrote a program for implementation of queue using linked list.. 我编写了一个使用链表实现队列的程序。
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct queue
{
struct node *front;
struct node *rear;
};
struct queue *q;
void create_queue(struct queue *);
struct queue * insert(struct queue *,int);
struct queue * delete(struct queue *);
struct queue * display(struct queue *);
int peek(struct queue *);
int main()
{
printf("a");
int value,option,t=0;
create_queue(q);
while(t==0)
{
printf("\n1.insert\n2.delete\n3.peek\n4.display\n");
scanf("%d",&option);
switch(option)
{
case 1:
printf("enter the number to be inserted");
scanf("%d",&value);
q=insert(q,value);
break;
case 2:
q=delete(q);
break;
case 3:
value=peek(q);
printf("the value pointed by front is %d",value);
break;
case 4:
q=display(q);
break;
default:
printf("invalid option");
}
printf("\n '0' to run again else '1' \n");
scanf("%d",&t);
}
return 0;
}
void create_queue(struct queue *q)
{
q->rear=NULL;
q->front=NULL;
}
struct queue * insert(struct queue *q,int value)
{
struct node *ptr;
ptr=(struct node *)malloc(sizeof(struct node *));
ptr->data=value;
if(q->front==NULL)
{
q->front=ptr;
q->rear=ptr;
q->front->next=q->rear->next=NULL;
}
else
{
q->rear->next=ptr;
q->rear=ptr;
q->rear->next=NULL;
}
return q;
}
struct queue * delete(struct queue *q)
{
struct node *ptr;
ptr=q->front;
if(q->front==NULL)
printf("\n underflow");
else
{
q->front=q->front->next;
printf("\n the value being deleted is %d",ptr->data);
free(ptr);
}
return q;
}
struct queue * display(struct queue *q)
{
struct node *ptr;
ptr=q->front;
if(ptr==NULL)
printf("\n queue is empty");
else
{
printf("\n");
while(ptr!=q->rear)
{
printf("%d \t",ptr->data);
ptr=ptr->next;
}
printf("%d \t",ptr->data);
}
return q;
}
int peek(struct queue *q)
{
return (q->front->data);
}
While execution: The terminal shows "Segmentation fault (core dumped)" and the execution of program stops. 执行期间:终端显示“段故障(核心已转储)”,程序停止执行。 why this has happened? 为什么会这样呢? what modifications must be done in the code for avoiding this? 为避免这种情况,必须在代码中进行哪些修改?
In addition to the problem pointed out by @FredK in his answer , you are not creating the struct queue
properly. 除了@FredK在回答中指出的问题外,您还没有正确创建struct queue
。
Since you have defined q
to be in global scope, it is initialized to NULL
. 由于已将q
定义为全局范围,因此将其初始化为NULL
。 And then you use it as an argument in create_queue
before its value has been set to a valid pointer. 然后,在将其值设置为有效指针之前,将其用作create_queue
的参数。 In create_queue
, you access the pointer as if it points to a valid object. 在create_queue
,您可以访问指针,就像它指向有效对象一样。 Accessing members of a NULL pointer cause undefined behavior. 访问NULL指针的成员会导致未定义的行为。 In your case, that manifests as segmentation fault. 在您的情况下,这表现为分段错误。
Change create_queue
to: 将create_queue
更改为:
struct queue * create_queue()
{
struct queue *q = malloc(sizeof(*q));
q->rear=NULL;
q->front=NULL;
return q;
}
Remove the global variable q
and replace it with a local variable in main
. 删除全局变量q
并将其替换为main
的局部变量。
int main()
{
struct queue *q;
printf("a");
int value,option,t=0;
q = create_queue();
...
}
ptr=(struct node *)malloc(sizeof(struct node *));
This is incorrect. 这是不正确的。 You have allocated space for a pointer to a node rather than for a node. 您已为指向节点而不是节点的指针分配了空间。 To avoid such confusion, always use 为避免这种混乱,请务必使用
ptr = malloc( sizeof(*ptr) );
Don't cast the result of malloc - it will hide the error that you will encounter if you forget to 不要转换malloc的结果-如果您忘记了,它将隐藏您将遇到的错误
#include <stdlib.h>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.