队列的链式实现,不知道问题出在哪里,请高手帮帮小弟
#include <stdio.h>
#include <stdlib.h>
typedef struct QueueNode
{
int Element;
struct QueueNode * Next;
}LinkQueueList;
typedef struct
{
LinkQueueList * Front;
LinkQueueList * Rear;
}LinkQueue;
void InitQueue(LinkQueue * Q);
//void DestoryQueue(LinkQueue * Q);
//int Empty(LinkQueue * Q);
void GetHead(LinkQueue * Q);
void Enqueue(LinkQueue * Q,int X);
void Dequeue(LinkQueue * Q);
//void Visit(LinkQueue * Q);
void main()
{
LinkQueue *Q;
InitQueue(Q);
Enqueue(Q,5);
Enqueue(Q,6);
GetHead(Q);
}
void InitQueue(LinkQueue * Q)
{
Q-> Front=(LinkQueueList *)malloc(sizeof(LinkQueueList));
Q-> Front=Q-> Rear;
Q-> Front-> Next=NULL;
}
void Enqueue(LinkQueue *Q,int X)
{
LinkQueueList *Tmp;
Tmp=(LinkQueueList *)malloc(sizeof(LinkQueueList));
Tmp-> Element=X;
Tmp-> Next=NULL;
Q-> Rear-> Next=Tmp;
Q-> Rear=Tmp;
}
void Dequeue(LinkQueue *Q)
{
LinkQueueList *Tmp;
if(Q-> Front==Q-> Rear)
printf( "The queue is overflow. ");
else
Tmp=Q-> Front-> Next;
Q-> Front-> Next=Tmp-> Next;
printf( "%d ",Tmp-> Element);
if(Q-> Rear==Tmp)
Q-> Rear=Q-> Front;
free(Tmp);
}
void GetHead(LinkQueue *Q)
{
printf( "%d ",Q-> Front-> Element);
}
[解决办法]
void main()
{
LinkQueue *Q;
Q = (LinkQueue*)malloc(sizeof(LinkQueue));
if (NULL == Q)
{
return;
}
InitQueue(Q);
Enqueue(Q,5);
Enqueue(Q,6);
GetHead(Q);
}
void InitQueue(LinkQueue * Q)
{
Q-> Front=Q-> Rear=NULL;
}
void Enqueue(LinkQueue *Q,int X)
{
LinkQueueList *Tmp;
Tmp=(LinkQueueList *)malloc(sizeof(LinkQueueList));
if (NULL == Tmp)
{
return;
}
Tmp-> Element=X;
Tmp-> Next=NULL;
if (NULL == Q-> Front)
{
Q-> Front = Q-> Rear = Tmp;
}
Q-> Rear-> Next=Tmp;
Q-> Rear=Tmp;
}