#include <stdio.h>
#include <malloc.h>
#define QLengh (sizeof(struct queue))
typedef struct queue
{
int element;
struct queue *next;
}
*Queue;
typedef struct
{
Queue front;
Queue rear;
}
LinkQueue;
void initQueue(LinkQueue *Q)
{
Q->front=Q->rear=malloc(QLengh);
if(Q->rear==NULL)
{
printf("Out of space
");
}
else
{
Q->rear->next=NULL;
}
}
void insertQueue(LinkQueue *Q,int element)
{
Queue NewQueue=malloc(QLengh);
if(NewQueue==NULL)
{
printf("Out of space
");
}
else
{
NewQueue->element=element;
NewQueue->next=NULL;
Q->rear->next=NewQueue;
Q->rear=NewQueue;
}
}
int isEmpty(LinkQueue *Q)
{
return (Q->front->next==NULL);
}
void outQueue(LinkQueue *Q)
{
Queue Qtmp=NULL;
if(isEmpty(Q))
{
printf("Empty Queue
");
}
else
{
Qtmp=Q->front;
Q->front=Q->front->next;
free(Qtmp);
}
}
void printQueue(LinkQueue *Q)
{
Queue P=Q->front->next;
while(P!=NULL)
{
printf("----------
");
printf(" %d
",P->element);
printf("----------
");
P=P->next;
}
}
int main(void)
{
LinkQueue Q;
initQueue(&Q);
printf("入队演示
");
insertQueue(&Q,1);
insertQueue(&Q,2);
insertQueue(&Q,3);
insertQueue(&Q,4);
insertQueue(&Q,5);
printQueue(&Q);
printf("出队演示
");
outQueue(&Q);
outQueue(&Q);
printQueue(&Q);
}
运行结果:
