• 队列的相关操作(队列实现杨辉三角的打印)


    队列的相关操作(队列实现杨辉三角的打印)

    1.实现队列的一种存储结构
    2.实现队列的相关操作
    3.利用队列的操作特点,借助进队与出队操作完成打印二项式系数的任务(杨辉三角的打印)

    杨辉三角打印图

    考虑到实现多种数据类型,采用了C++的模板方式编写,并利用了STL库函数的vector容器类来储存数据,该循环队列主要实现了以下函数:

    class MyCircularQueue 
    {
    	private:
    		vector<T> data;
    		int head;
    		int tail;
    		int size;
    	public:
    	MyCircularQueue() {
    		data.resize(k);
    		head = -1;
    		tail = -1;
    		size = k;
    	}
    	bool setsize(int k);
    	bool enQueue(const T& value);//进队	
    	bool deQueue(T& x);	//出队
    	T getFront() ;//取队列头部元素	
    	T getRear();//取队列位尾部元素
    	bool isEmpty();//判断是否为空	
    	bool isFull();//判断是否为满
    };
    
    

    主要函数的相关实现

    队列元素进队

    /** Insert an element into the circular queue. Return true if the operation is successful. */
    template <class T>
    inline bool MyCircularQueue<T>::enQueue(const T& value) {
    		if (isFull()) {
    			return false;
    		}
    		if (isEmpty()) {
    			head = 0;
    		}
    		tail = (tail + 1) % size;
    		data[tail] = value;
    		return true;
    }
    
    

    队列元素出队

    /** Delete an element from the circular queue. Return true if the operation is successful. */
    template <class T>
    inline bool MyCircularQueue<T>::deQueue(T& x) 
    {
    	x=data[head];
    	if (isEmpty()) {
    		return false;
    	}
    	if (head == tail) {
    		head = -1;
    		tail = -1;
    		return true;
    	}
    	head = (head + 1) % size;
    	return true;
    }
    
    

    取队列头部元素

    /** Get the front item from the queue. */
    template <class T>
    inline T MyCircularQueue<T>::getFront() 
    {
    	if (isEmpty()) {
    		return -1;
    	}
    	return data[head];
    }
    
    

    取队列位尾部元素

    /** Get the last item from the queue. */
    template <class T>
    inline T MyCircularQueue<T>::getRear() 
    {
    		if (isEmpty()) {
    			return -1;
    		}
    		return data[tail];
    }
    
    

    测试函数实现杨辉三角的打印

    MyCircularQueue<int> queve;
    		int temp=0,x;
    		int n;
    		cin>>n;
    		int i=1,j,s=0,k=0,t=0,u;
    		queve.enQueue(i);queve.enQueue(i);
    		for(i=1;i<=n;i++)
    		{
    			cout<<endl;
    			for (j = 1; j<=n - i ; j++)
    			{
    				cout<<setw(3)<<" ";
    			}
    			queve.enQueue(k);
    			for(j=1;j<=i+2;j++)
    			{
    				queve.deQueue(t);
    				u=s+t;
    				queve.enQueue(u);
    				s=t;
    				if(j!=i+2){
    					cout<<setw(6)<<s;
    				}
    			}
    		}
    		return 0;
    

    希望和大家一起进步,一起学习,有什么可以改进的地方希望大家评论区指出来,我加以改正,继续加油。也可以通过我的QQ联系我哟。

    完整代码下载地址

  • 相关阅读:
    React的环境搭建
    Maven学习(3)-依赖管理-项目依赖相关操作命令
    k8s的yaml文件配置详解(转))
    Jenkins学习-Jenkins+K8s(k8s部署)
    Jenkins学习-定时任务设置(转)
    IntelliJ IDEA+Github+Maven+Jenkins+SipringBoot+VUE搭建Web开发环境样例(3)-为查询数据库项目样例创建Jenkins构建任务
    Maven学习(3)-依赖管理-POM文件中依赖的范围定义
    Maven学习(3)-依赖管理-POM文件中依赖的版本锁定详解
    Maven学习(3)-依赖管理-POM文件中依赖的jar包下载过程详解
    Maven学习(3)-依赖管理-依赖仓库管理
  • 原文地址:https://www.cnblogs.com/xiangjunhong/p/12482472.html
Copyright © 2020-2023  润新知