队列基本函数(杨辉三角模型)

头文件:

#ifndef _SEQUEUE_H_
#define _SEQUEUE_H_

#define MAXSIZE 10
#define SUCCESS 10000
#define FAILURE 10001

typedef int ElemType;

struct sequeue
{
    ElemType data[MAXSIZE];
    int front;
    int rear;
};

typedef struct sequeue Sequeue;

#endif

子函数:

#include <stdio.h>
#include "SeQueue.h"

int QueueInit(Sequeue *Q)
{
    Q->rear = Q->front = 0;

    return SUCCESS;
}

int EnQueue(Sequeue *Q,ElemType e)
{
    if((Q->rear + 1) % MAXSIZE == Q->front)
    {
        return FAILURE;
    }

    Q->data[Q->rear] = e;
    Q->rear = (Q->rear + 1) % MAXSIZE;

    return SUCCESS;
}

int QueueLength(Sequeue Q)
{
    return (Q.rear - Q.front + MAXSIZE) % MAXSIZE;
}

ElemType GetFront(Sequeue Q)
{
    return Q.data[Q.front];
}

ElemType DelQueue(Sequeue *Q)
{
    ElemType e;

    if(Q->rear == Q->front)
    {
        return FAILURE;
    }

    e = Q->data[Q->front];
    Q->front = (Q->front + 1) % MAXSIZE;

    return e;
}

int QueueClear(Sequeue *Q)
{
    while(Q->rear != Q->front)
    {
        Q->front = (Q->front + 1) % MAXSIZE;     
    }
    return SUCCESS;
}

主函数:

#include <stdio.h>
#include "SeQueue.h"

int main()
{
    int ret,i;
    Sequeue sq;

    ret = QueueInit(&sq);
    if(FAILURE == ret)
    {
        printf("Init Failure!\n");
    }
    else
    {
        printf("Init Success!\n");
    }

    for(i = 0; i < 5; i++)
    {
        ret = EnQueue(&sq);
        if(FAILURE == ret)
        {
            printf("Enter Queue Failure!\n");
        }
        else
        {
            printf("Enter %d Success!\n",i + 1);
        }
    }

    printf("Length is %d!\n",QueueLength(sq));

    printf("Front Element is %d\n",GetFront(sq));

    for(i = 0; i < 3; i++)
    {
        printf("Delete %d Success!\n",DelQueue(&sq));
    }

    ret = QueueClear(&sq);
    if(FAILURE == ret)
    {
        printf("Clear Failure!\n");
    }
    else
    {
        printf("Clear Success!\n");
    }
    printf("Length is %d!\n",QueueLength(sq));
    return 0;
}

杨辉三角模型:

#include <stdio.h>
#include "SeQueue.h"

int main()
{
    Sequeue a,b;
    int i,num;
    int length,j,ret;

    QueueInit(&a);
    QueueInit(&b);

    printf("Please input line:\n");
    scanf("%d",&num);


    for(i = 0; i < num; i++)
    {
        if(0 == i)
        {
            EnQueue(&a,0);
            EnQueue(&a,1);
            EnQueue(&a,0);
            printf("1");
        }
        else
        {
            EnQueue(&b,0);
            while(QueueLength(a) != 1)
            {
                EnQueue(&b,DelQueue(&a) + GetFront(a));
            }
            DelQueue(&a);
            EnQueue(&b,0);
        }

            length = QueueLength(b);

            for(j = 0; j < length; j++)
            {
                ret = DelQueue(&b);
                if(ret != 0)
                {
                    printf("%d ",ret);
                }
                EnQueue(&a,ret);
            }
       printf("\n"); 
    }
}
    原文作者:杨辉三角问题
    原文地址: https://blog.csdn.net/KaiLoveM/article/details/78676747
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞