顺序队列完成杨辉三角

头文件 

#ifndef _SEQUENCEQUEUE_H
#define _SEQUENCEQUEUE_H
 
#define SIZE  30
 
#define FAILURE  10000
#define SUCCESS  10001
#define FALSE    10002
#define TRUE     10003
 
struct queue
{
    int data[SIZE];
    int front;
    int rear;
};
 
typedef struct queue Queue;
 
int QueueInit(Queue *q);
int QueueEmpty(Queue q);
int QueueEnter(Queue *q, int i);
int QueueExit(Queue *q);
int QueueGet(Queue *q);
int QueueLength(Queue *q);
int QueueClear(Queue *q);
 
#endif

功能函数

#include <stdio.h>
#include "SequenceQueue.h"
 
int QueueInit(Queue *q)
{
    if(NULL == q)
    {
        return FAILURE;
    }
    q->front = q->rear = 0;
    return SUCCESS;
}
 
int QueueEmpty(Queue q)
{
    return (q.front == q.rear) ? TRUE : FALSE;
}
 
int QueueEnter(Queue *q, int e)
{
    if(NULL == q)
    {
        return FAILURE;
    }
    if(((q->rear + 1) % SIZE) == q->front)
    {
        return FAILURE;
    }
    q->data[q->rear] = e;
    q->rear = (q->rear + 1) % SIZE;
    return SUCCESS;
}
 
int QueueExit(Queue *q)
{
    if(NULL == q)
    {
        return FAILURE;
    }
    if(q->front == q->rear)
    {
        return FAILURE;
    }
    int e = q->data[q->front];
    q->front = (q->front + 1) % SIZE;
    return e;
}
 
int QueueGet(Queue *q)
{
    if(NULL == q)
    {
        return FAILURE;
    }
    return q->data[q->front];
}
 
int QueueLength(Queue *q)
{
    if(NULL == q)
    {
        return FAILURE;
    }
 
    return (q->rear - q->front + SIZE) % SIZE;
}
 
int QueueClear(Queue *q)
{
    if(NULL == q)
    {
        return FAILURE;
    }
    q->front = q->rear;
 
    return SUCCESS;
}

主函数实现

#include <stdio.h>
#include "SequenceQueue.h"
 
int main()
{
    Queue q1, q2;
    int i, j;
 
    if(QueueInit(&q1) != SUCCESS || QueueInit(&q2) != SUCCESS)
    {
        printf("Init FAILURE!\n");
    }
 
    QueueEnter(&q1, 0);
    QueueEnter(&q1, 1);
    QueueEnter(&q1, 0);
    //printf("%d %d %d\n", QueueGet(&q1), QueueGet(&q1), QueueGet(&q1));
 
    for(i = 2; i < 12; i++)
    {
        QueueEnter(&q2, 0);
        for(j = 0; j < i; j++)
        {
            QueueEnter(&q1, QueueExit(&q2));
            QueueEnter(&q2, (QueueExit(&q1) + QueueGet(&q1)));
            printf("%d ",QueueGet(&q2));
        }
        printf("\n");
        QueueExit(&q1);
        QueueEnter(&q1, QueueExit(&q2));
        QueueEnter(&q1, 0);
    }
    return 0;
}

 

    原文作者:杨辉三角问题
    原文地址: https://blog.csdn.net/GX0401/article/details/81544716
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞