- 论坛徽章:
- 0
|
//是个队列的小程序,可是运行时总是出现段错误。我把有问题的部分挑出来了
#include <stdio.h>
#include <stdlib.h>
typedef struct QNode
{
int data;
struct QNode *next;
}QNode, *QPtr;
typedef struct QLink
{
QPtr front, rear;
}QLink;
void Error(char *s)
{
printf("%s\n",s);
}
void InitQueue(QLink *Q)
{
Q->front = ( QPtr )malloc(sizeof(QNode));
if(!Q->front)
{
Error("InitQueue memory error!");
exit(0);
}
Q->rear = Q->front;
Q->front->next = NULL;
}
void DestroyQueue(QLink *Q)
{
while(Q->front)
{
Q->rear = Q->front->next;
free(Q->front);
Q->front = Q->rear;
}
}
int main()
{
QLink *Q;
int i, t;
InitQueue(Q);
DestroyQueue(Q);
return 0;
}
调试的时候是这样,gdb用的也不熟悉,找不到哪儿错了。
(gdb) r
Starting program: /home/guorh/datastructure/a.out
Program received signal SIGSEGV, Segmentation fault.
0x00000000 in ?? ()
(gdb) where
#0 0x00000000 in ?? ()
#1 0x00000001 in ?? ()
#2 0xbfb20344 in ?? ()
#3 0xbfb2034c in ?? ()
#4 0xb7f4c878 in ?? ()
#5 0x00000000 in ?? () |
|