赞
踩
#include <malloc.h>
#include <stdio.h>
typedef struct Node
{
int data;
struct Node *pNext;
}Node;
typedef struct Queue
{
Node *first;
Node *rear;
}Queue;
Queue *PushBack(Queue *Q,int num)
{
Node *p = (Node *)malloc(sizeof(Node));
p->data = num;
p->pNext = NULL;
if (Q->rear == NULL)//empty queue
{
Q->first = p;
Q->rear = p;
}
else
{
Q->rear->pNext = p;
Q->rear = p;
}
return Q;
}
int PopFirst(Queue *Q)
{
int num;
if (Q->first == NULL)
{
printf("Empty queue\n");
return 0;
}
num = Q->first->data;
Node *p;
p = Q->first;
if (Q->first == Q->rear)
{
Q->first = NULL;
Q->rear = NULL;
}
else
{
Q->first = Q->first->pNext;
}
free(p);
return num;
}
void test()
{
Queue *Q;
Q = (Queue *)malloc(sizeof(Queue));
Q->first = NULL;
Q->rear = NULL;
PushBack(Q,1);
PushBack(Q,2);
PushBack(Q,3);
PushBack(Q,4);
PushBack(Q,5);
printf("%d\n",PopFirst(Q));
printf("%d\n",PopFirst(Q));
printf("%d\n",PopFirst(Q));
printf("%d\n",PopFirst(Q));
printf("%d\n",PopFirst(Q));
printf("%d\n",PopFirst(Q));
}
int main()
{
test();
return 0;
}

output:
1
2
3
4
5
Empty queue
0
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。