Series summary data structure (D) - circular queue of Pascal's Triangle

Today we write applications a circular queue of Oh!

Pascal's Triangle to solve the problem is ~~

For data have close ties between the upper and lower such a multi-layered, if only with arrays and loops to solve the problem, obviously will waste a lot of space and time,

So we use a queue to solve this problem:

Was selected because it is a circular queue is very effective use of space, to facilitate our work:

Start defining the structure:

typedef struct  // define a circular queue 
{
     int Data [maxmize];
     int Front;
     int Rear; 
} RollQueue;

Here the maximum value (MAXMIZE) you may want to limit their own definition of the macro definition with Yo

About circular queue, because it does not waste space, so the back is very useful to calculate more than a little insert here:

So after we determine the conditions will be a little more -

Queue correlation function settings:

Because the queue is a team can only be inserted from the tail, remove the head from the team structure, thus simplifying our operations:

void InitQueue(RollQueue &R)//队列初始化函数
{
    R.Front=R.Rear=0;//博主这里没有用指针,直接用了数组~
}

void InsertQueue(RollQueue &R,int Data)//插入队尾
{
    //首先判断是否满队列。
    if((R.Rear+1)%MAXMIZE==R.Front)//满队列条件
    {
        cout << "This queue is full." << endl;
    }
    else
    {
        R.data[R.Rear]=Data;
        R.Rear=(R.Rear+1)%MAXMIZE;//success
    }
}

int DeleteQueue(RollQueue &R,int &Re)//删除队头元素,用re返回
{
    if(R.Rear==R.Front)//判断是否队空
    {
        cout << "This queue is empty." << endl;
        return 0;
    }
    else
    {
        Re=R.data[R.Front];
        R.Front=(R.Front+1)%MAXMIZE;
        return Re;
    }
}

最后是杨辉三角的建立:

void YangHui(int n)
{
    RollQueue R;
    InitQueue(R);
    InsertQueue(R,1);//预先放入第一行的系数
    InsertQueue(R,1);//预先放入第一行的系数
    int s=0;
    for(int i=1;i<=n;i++)
    {
        cout << endl;//这里换行鸭
        InsertQueue(R,0);//开头插入一个0用来进行第一次加法
        for(int j=1;j<=i+2;j++)//处理第i行的i+2个系数
        {
            int t;
            DeleteQueue(R,t);
            InsertQueue(R,s+t);//这里把上一行的两个数据相加得到这一行的数据s
            s=t;
            if(j!=i+2)
            {
                cout << s << ' ' ;
            }
        }
    }
}

最后是整个程序:

#include <bits/stdc++.h>

using namespace std;
#define MAXMIZE 100

typedef struct //定义循环队列
{
    int data[MAXMIZE];
    int Front;
    int Rear;
}RollQueue;

void InitQueue(RollQueue &R)//队列初始化函数
{
    R.Front=R.Rear=0;//博主这里没有用指针,直接用了数组~
}

void InsertQueue(RollQueue &R,int Data)//插入队尾
{
    //首先判断是否满队列。
    if((R.Rear+1)%MAXMIZE==R.Front)//满队列条件
    {
        cout << "This queue is full." << endl;
    }
    else
    {
        R.data[R.Rear]=Data;
        R.Rear=(R.Rear+1)%MAXMIZE;//success
    }
}

int DeleteQueue(RollQueue &R,int &Re)//删除队头元素,用re返回
{
    if(R.Rear==R.Front)//判断是否队空
    {
        cout << "This queue is empty." << endl;
        return 0;
    }
    else
    {
        Re=R.data[R.Front];
        R.Front=(R.Front+1)%MAXMIZE;
        return Re;
    }
}

void YangHui(int n)
{
    RollQueue R;
    InitQueue(R);
    InsertQueue(R,1);//预先放入第一行的系数
    InsertQueue(R,1);//预先放入第一行的系数
    int s=0;
    for(int i=1;i<=n;i++)
    {
        cout << endl;//这里换行鸭
        InsertQueue(R,0);//开头插入一个0用来进行第一次加法
        for(int j=1;j<=i+2;j++)//处理第i行的i+2个系数
        {
            int t;
            DeleteQueue(R,t);
            InsertQueue(R,s+t);//这里把上一行的两个数据相加得到这一行的数据s
            s=t;
            if(j!=i+2)
            {
                cout << s << ' ' ;
            }
        }
    }
}

int main()
{
    cout << "Input YangHui triangle n:" << endl;
    int n;
    cin>> n;
    YangHui(n);
    return 0;
}

那么我们的循环队列应用就讲到这里啦~~

 

 

 

Guess you like

Origin www.cnblogs.com/ever17/p/10962794.html
Recommended