"Segmentation fault (core dumped)"在使用链表实现队列
"Segmentation fault (core dumped)" in 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);
}
执行时:
终端显示"Segmentation fault (core dumped)",程序停止执行。
为什么会这样?
为了避免这种情况,必须在代码中进行哪些修改?
ptr=(struct node *)malloc(sizeof(struct node *));
这是不正确的。您已将 space 分配给指向节点的指针而不是节点。为避免此类混淆,请始终使用
ptr = malloc( sizeof(*ptr) );
不要强制转换 malloc 的结果 - 它会隐藏您忘记
时遇到的错误
#include <stdlib.h>
除了@FredK 在 中指出的问题之外,您没有正确创建 struct queue
。
由于您已将 q
定义为在全局范围内,因此它被初始化为 NULL
。然后在将其值设置为有效指针之前将其用作 create_queue
中的参数。在 create_queue
中,您访问指针就像它指向一个有效对象一样。访问 NULL 指针的成员会导致未定义的行为。在您的情况下,这表现为分段错误。
将create_queue
更改为:
struct queue * create_queue()
{
struct queue *q = malloc(sizeof(*q));
q->rear=NULL;
q->front=NULL;
return q;
}
删除全局变量 q
并用 main
中的局部变量替换它。
int main()
{
struct queue *q;
printf("a");
int value,option,t=0;
q = create_queue();
...
}
我写了一个用链表实现队列的程序..
#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);
}
执行时: 终端显示"Segmentation fault (core dumped)",程序停止执行。 为什么会这样? 为了避免这种情况,必须在代码中进行哪些修改?
ptr=(struct node *)malloc(sizeof(struct node *));
这是不正确的。您已将 space 分配给指向节点的指针而不是节点。为避免此类混淆,请始终使用
ptr = malloc( sizeof(*ptr) );
不要强制转换 malloc 的结果 - 它会隐藏您忘记
时遇到的错误#include <stdlib.h>
除了@FredK 在 struct queue
。
由于您已将 q
定义为在全局范围内,因此它被初始化为 NULL
。然后在将其值设置为有效指针之前将其用作 create_queue
中的参数。在 create_queue
中,您访问指针就像它指向一个有效对象一样。访问 NULL 指针的成员会导致未定义的行为。在您的情况下,这表现为分段错误。
将create_queue
更改为:
struct queue * create_queue()
{
struct queue *q = malloc(sizeof(*q));
q->rear=NULL;
q->front=NULL;
return q;
}
删除全局变量 q
并用 main
中的局部变量替换它。
int main()
{
struct queue *q;
printf("a");
int value,option,t=0;
q = create_queue();
...
}