関連の操作キュー(パスカルの三角形の印刷キューの実装)

関連の操作キュー(パスカルの三角形の印刷キューの実装)

キューメモリ構造の1実施
2.実装依存操作キュー
キューの動作特性によって3、印刷操作によるチームがデキュータスク二項係数(印刷パスカルの三角形)を完成するに

パスカルの三角形地図をプリント

ストア・データ、循環キュー、以下の機能の主達成するSTLベクトルクラスコンテナライブラリ関数を使用して、C ++テンプレート調製を使用して、データタイプの様々な実現の観点から:

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よを通して私に連絡することができます。

完全なコードをダウンロード

公開された19元の記事 ウォンの賞賛8 ビュー622

おすすめ

転載: blog.csdn.net/qq_43336390/article/details/102498891