使用指针实现队列:分段错误

发布于 2024-11-18 14:49:58 字数 1508 浏览 0 评论 0原文

我正在尝试实现 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一袭白衣梦中忆 2024-11-25 14:49:58

您没有为 Q 分配内存并立即使用它:

struct queue *Q; /* Where does Q point ? */

Q->front=Q->rear=NULL; /* Q not initialized, undefined behavior */

因此 Q 指向堆栈上的某个随机值。要解决这个问题,请使用malloc

struct queue *Q;
Q = malloc(sizeof(*Q));
if (NULL == Q) {
    /* Tinfoil hat. */
}

You are not allocating memory for Q and using it right away:

struct queue *Q; /* Where does Q point ? */

Q->front=Q->rear=NULL; /* Q not initialized, undefined behavior */

So Q points to some random value on the stack. To solve it, use malloc:

struct queue *Q;
Q = malloc(sizeof(*Q));
if (NULL == Q) {
    /* Tinfoil hat. */
}
檐上三寸雪 2024-11-25 14:49:58

您没有为您的struct queue *Q;分配空间;

 struct queue *Q;
 struct cell *q;
 int i;

 Q->front=Q->rear=NULL;

Q只是一个未初始化的指针。它没有指向任何有效的内容,因此您不能像 Q->front=Q->rear=NULL; 那样取消引用它,

将其更改为在堆栈上分配 Q,并将其地址传递给您的函数。

 struct queue Q;
 struct cell *q;
 int i;

 Q.front=Q.rear=NULL;
 for(i=0; i<8; i++) {enqueue(i+1, &Q);} 

You have no space allocated to your struct queue *Q;

 struct queue *Q;
 struct cell *q;
 int i;

 Q->front=Q->rear=NULL;

Q is just an uninitialized pointer. It doesn't point to anything valid, so you can't dereference it as you do in Q->front=Q->rear=NULL;

Change it to allocate Q on the stack, and pass its address to your functions.

 struct queue Q;
 struct cell *q;
 int i;

 Q.front=Q.rear=NULL;
 for(i=0; i<8; i++) {enqueue(i+1, &Q);} 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文