Related operations queue (print queue implementation of Pascal's Triangle)

Related operations queue (print queue implementation of Pascal's Triangle)

1. Implementation of a queue memory structure
2. The implementation-dependent operation queue
3 by the operation characteristics of the queue, into the team by the printing operation is completed dequeue task binomial coefficient (printing Pascal triangle)

Pascal's Triangle Print map

In view of achieving a variety of data types, using a C ++ template-prepared, using the STL vector class container library functions to store data, the main achievement of the circular queue, the following functions:

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();//判断是否为满
};

The main function of the associated implementation

Queue element into the team

/** 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;
}

Queue element from the team

/** 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;
}

Queues header element

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

Queues bit trailing elements

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

Test function enables the printing of Pascal's Triangle

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;

We hope and progress together, learn together, what can we hope for improvement noted the comments section, I have to be corrected, continue to fuel. You can also contact me through my QQ yo.

Download the full code

Published 19 original articles · won praise 8 · views 622

Guess you like

Origin blog.csdn.net/qq_43336390/article/details/102498891