我寫了一個用回圈鏈表表示佇列的程式,運行出來沒結果,用debug除錯彈出Program received signal SIGSEGV,Segmentation fault.我找半天也沒有找到原因,求指導!!!!!!
下面是我的程式:
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
int data;
struct node *next;
}LinkQueueNode;
typedef struct
{
LinkQueueNode *front;
LinkQueueNode *rear;
}LinkQueue;
int InitQueue(LinkQueue *Q)
{
Q->front=(LinkQueueNode *)malloc(sizeof(LinkQueueNode));
if(Q->front!=NULL)
{
Q->rear=Q->front;
Q->front->next=NULL;
return 1;
}
else
{
return 0;
}
}
int EnterQueue(LinkQueue *Q,int x)
{
LinkQueueNode *p;
p=(LinkQueueNode *)malloc(sizeof(LinkQueueNode));
if(p!=NULL)
{
p->data=https://bbs.csdn.net/topics/x;
p->next=NULL;
Q->rear->next=p;
Q->rear=p;
return 1;
}
else
return 0;
}
int DeleteQueue(LinkQueue *Q,int *x)
{
LinkQueueNode *p;
if(Q->front->next!=Q->rear)
{
p=Q->front->next;
Q->front->next=p->next;
if(Q->rear==p)
Q->rear=Q->front;
*x=p->data;
free(p);
return 1;
}
else
return 0;
}
int main()
{
LinkQueue *Q;
InitQueue(Q);
printf("請輸入資料(-1表示結束):");
int x;
int f=0;
scanf("%d",&x);
while(x!=-1)
{
EnterQueue(Q,x);
f=1;
}
if(f==1)
{
printf("輸入資料為:");
while(Q->front->next!=Q->rear)
{
DeleteQueue(Q,&x);
printf("%d ",x);
}
}
else
printf("未輸入資料!\n");
return 0;
}
uj5u.com熱心網友回復:
段錯誤,檢查下是否有陣列越界,或者說malloc的時候是否有型別的不符合,比如將一個int型別的數賦給了一個malloc出來的char陣列。uj5u.com熱心網友回復:
是因為我的data型別是int的原因嘛。。。
uj5u.com熱心網友回復:
看了下 你這里陷入死回圈了
scanf("%d",&x);
while(x!=-1)
{
EnterQueue(Q,x);
f=1;
}
隨便輸個不等于-1的數就死回圈了
你是不是想這樣寫
//scanf("%d",&x);
while(scanf("%d",&x) && x!=-1)
{
EnterQueue(Q,x);
f=1;
}
這樣我運行了一下能運行,但好像輸出的不是想要的結果,你再看看吧。
uj5u.com熱心網友回復:
段錯誤,檢查下是否有陣列越界,或者說malloc的時候是否有型別的不符合,比如將一個int型別的數賦給了一個malloc出來的char陣列。
是因為我的data型別是int的原因嘛。。。
看了下 你這里陷入死回圈了
scanf("%d",&x);
while(x!=-1)
{
EnterQueue(Q,x);
f=1;
}
隨便輸個不等于-1的數就死回圈了
你是不是想這樣寫
//scanf("%d",&x);
while(scanf("%d",&x) && x!=-1)
{
EnterQueue(Q,x);
f=1;
}
這樣我運行了一下能運行,但好像輸出的不是想要的結果,你再看看吧。
好的感謝!我再去試試!
uj5u.com熱心網友回復:
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
int data;
struct node *next;
}LinkQueueNode;
typedef struct
{
LinkQueueNode *front;
LinkQueueNode *rear;
}LinkQueue;
int InitQueue(LinkQueue *Q)
{
Q->front=(LinkQueueNode *)malloc(sizeof(LinkQueueNode));
if(Q->front!=NULL)
{
Q->rear=Q->front;
Q->front->next=NULL;
return 1;
}
else
{
return 0;
}
}
int EnterQueue(LinkQueue *Q,int x)
{
#if 1
LinkQueueNode *p;
p = (LinkQueueNode *)malloc(sizeof(LinkQueueNode));
if (!p)
exit(0);
p->data=https://bbs.csdn.net/topics/x;
p->next=NULL;
Q->rear->next=p;
Q->rear=p;
#else
LinkQueueNode *p;
p=(LinkQueueNode *)malloc(sizeof(LinkQueueNode));
if(p!=NULL)
{
p->data=https://bbs.csdn.net/topics/x;
p->next=NULL;
Q->rear->next=p;
Q->rear=p;
return 1;
}
else
return 0;
#endif
}
int DeleteQueue(LinkQueue *Q,int *x)
{
#if 1
LinkQueueNode *p;
if(Q->front == Q->rear)
return 0;
p = Q->front->next;
Q->front->next = p->next;
if(Q->rear == p)
Q->rear=Q->front;
*x = p->data;
free(p);
return 1;
#else
LinkQueueNode *p;
if(Q->front->next!=Q->rear)
{
p=Q->front->next;
Q->front->next=p->next;
if(Q->rear==p)
Q->rear=Q->front;
*x=p->data;
free(p);
return 1;
}
else
return 0;
#endif
}
int main()
{
LinkQueue *Q;
Q = (LinkQueue *)malloc(sizeof(LinkQueue));
if (!Q)
return -1;
InitQueue(Q);
printf("請輸入資料(-1表示結束):");
int x;
int f = 0;
scanf("%d",&x);
while(x!=-1)
{
EnterQueue(Q,x);
scanf("%d",&x);
f=1;
}
if (f!=1) {
printf("未輸入資料!\n");
return 0;
}
//if(f==1)
// {
printf("輸入資料為:");
//while(Q->front->next != Q->rear)
while(Q->front != Q->rear)
{
DeleteQueue(Q,&x);
printf("%d ",x);
}
// }
// else
// printf("未輸入資料!\n");
return 0;
}
供參考~
uj5u.com熱心網友回復:
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
int data;
struct node *next;
}LinkQueueNode;
typedef struct
{
LinkQueueNode *front;
LinkQueueNode *rear;
}LinkQueue;
int InitQueue(LinkQueue *Q)
{
Q->front=(LinkQueueNode *)malloc(sizeof(LinkQueueNode));
if(Q->front!=NULL)
{
Q->rear=Q->front;
Q->front->next=NULL;
return 1;
}
else
{
return 0;
}
}
int EnterQueue(LinkQueue *Q,int x)
{
#if 1
LinkQueueNode *p;
p = (LinkQueueNode *)malloc(sizeof(LinkQueueNode));
if (!p)
exit(0);
p->data=https://bbs.csdn.net/topics/x;
p->next=NULL;
Q->rear->next=p;
Q->rear=p;
#else
LinkQueueNode *p;
p=(LinkQueueNode *)malloc(sizeof(LinkQueueNode));
if(p!=NULL)
{
p->data=https://bbs.csdn.net/topics/x;
p->next=NULL;
Q->rear->next=p;
Q->rear=p;
return 1;
}
else
return 0;
#endif
}
int DeleteQueue(LinkQueue *Q,int *x)
{
#if 1
LinkQueueNode *p;
if(Q->front == Q->rear)
return 0;
p = Q->front->next;
Q->front->next = p->next;
if(Q->rear == p)
Q->rear=Q->front;
*x = p->data;
free(p);
return 1;
#else
LinkQueueNode *p;
if(Q->front->next!=Q->rear)
{
p=Q->front->next;
Q->front->next=p->next;
if(Q->rear==p)
Q->rear=Q->front;
*x=p->data;
free(p);
return 1;
}
else
return 0;
#endif
}
int main()
{
LinkQueue *Q;
Q = (LinkQueue *)malloc(sizeof(LinkQueue));
if (!Q)
return -1;
InitQueue(Q);
printf("請輸入資料(-1表示結束):");
int x;
int f = 0;
scanf("%d",&x);
while(x!=-1)
{
EnterQueue(Q,x);
scanf("%d",&x);
f=1;
}
if (f!=1) {
printf("未輸入資料!\n");
return 0;
}
//if(f==1)
// {
printf("輸入資料為:");
//while(Q->front->next != Q->rear)
while(Q->front != Q->rear)
{
DeleteQueue(Q,&x);
printf("%d ",x);
}
// }
// else
// printf("未輸入資料!\n");
return 0;
}
供參考~
感謝!!??????
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/104436.html
標籤:C語言
