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

头文件:

#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"); 
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值