使用指针实现队列:分段错误
我正在尝试实现 FIFO。代码编译没有错误,但运行程序时出现分段错误。问题是什么?
#include <stdio.h>
#include <stdlib.h>
struct cell
{
int element;
struct cell *next;
};
struct queue
{
struct cell *front;
struct cell *rear;
};
void enqueue(int x, struct queue *Q);
void dequeue(struct queue *Q);
main()
/* Manipulation of a linked queue of cells. */
{
struct queue *Q;
struct cell *q;
int i;
Q->front=Q->rear=NULL;
for(i=0; i<8; i++) {enqueue(i+1, Q);}
printf("Q->front = %p, Q->rear = %p\n", Q->front, Q->rear);
q=Q->front;
while(q!=NULL)
{
printf("cell = %d, %p\n", q->element, q->next);
q=q->next;
}
for(i=0; i<10; i++) dequeue(Q);
printf("Q->front = %p, Q->rear = %p\n", Q->front, Q->rear);
q=Q->front;
while(q!=NULL)
{
printf("cell = %d, %p\n", q->element, q->next);
q=q->next;
}
return(0);
}
void enqueue(int x, struct queue *Q)
{
struct cell *p;
p=(struct cell *)malloc(sizeof(struct cell));
if(Q->rear != NULL) Q->rear->next = p;
Q->rear = p;
if(Q->front == NULL) Q->front = p;
Q->rear->element = x; Q->rear->next = NULL;
return;
}
void dequeue(struct queue *Q)
{
struct cell *q;
if(Q->front == NULL) {printf("Error: Queue is empty.\n"); exit(1);}
else {q=Q->front; Q->front = Q->front->next; free(q);}
if(Q->front == NULL) Q->rear = NULL;
return;
}
I am trying to implement a FIFO. The code compiles without errors but i get segmentation fault
when running the program. What is the problem?
#include <stdio.h>
#include <stdlib.h>
struct cell
{
int element;
struct cell *next;
};
struct queue
{
struct cell *front;
struct cell *rear;
};
void enqueue(int x, struct queue *Q);
void dequeue(struct queue *Q);
main()
/* Manipulation of a linked queue of cells. */
{
struct queue *Q;
struct cell *q;
int i;
Q->front=Q->rear=NULL;
for(i=0; i<8; i++) {enqueue(i+1, Q);}
printf("Q->front = %p, Q->rear = %p\n", Q->front, Q->rear);
q=Q->front;
while(q!=NULL)
{
printf("cell = %d, %p\n", q->element, q->next);
q=q->next;
}
for(i=0; i<10; i++) dequeue(Q);
printf("Q->front = %p, Q->rear = %p\n", Q->front, Q->rear);
q=Q->front;
while(q!=NULL)
{
printf("cell = %d, %p\n", q->element, q->next);
q=q->next;
}
return(0);
}
void enqueue(int x, struct queue *Q)
{
struct cell *p;
p=(struct cell *)malloc(sizeof(struct cell));
if(Q->rear != NULL) Q->rear->next = p;
Q->rear = p;
if(Q->front == NULL) Q->front = p;
Q->rear->element = x; Q->rear->next = NULL;
return;
}
void dequeue(struct queue *Q)
{
struct cell *q;
if(Q->front == NULL) {printf("Error: Queue is empty.\n"); exit(1);}
else {q=Q->front; Q->front = Q->front->next; free(q);}
if(Q->front == NULL) Q->rear = NULL;
return;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您没有为 Q 分配内存并立即使用它:
因此 Q 指向堆栈上的某个随机值。要解决这个问题,请使用
malloc
:You are not allocating memory for
Q
and using it right away:So Q points to some random value on the stack. To solve it, use
malloc
:您没有为您的
struct queue *Q;
分配空间;Q
只是一个未初始化的指针。它没有指向任何有效的内容,因此您不能像 Q->front=Q->rear=NULL; 那样取消引用它,将其更改为在堆栈上分配 Q,并将其地址传递给您的函数。
You have no space allocated to your
struct queue *Q;
Q
is just an uninitialized pointer. It doesn't point to anything valid, so you can't dereference it as you do inQ->front=Q->rear=NULL;
Change it to allocate Q on the stack, and pass its address to your functions.