队列的出队/入队的操作

来源:互联网 发布:淘宝网包包新款男士 编辑:程序博客网 时间:2024/04/27 20:10
#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

0 0
原创粉丝点击