当前位置:   article > 正文

队列的出队/入队的操作_题重新定义队列出队的操作:队首出队的数字重新在队尾入队。 例:队列中有1 2 3三个

题重新定义队列出队的操作:队首出队的数字重新在队尾入队。 例:队列中有1 2 3三个

#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;
}


  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90

output:
1
2
3
4
5
Empty queue
0

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/910057
推荐阅读
相关标签
  

闽ICP备14008679号