C ++ language sequence of stack

C ++ language sequence of stack

In the C language to write the order stack time, I have to tell you about the characteristics of the stack , and introduces the stack operation, and use C language to implement the relevant algorithms. Xiao Bian is not here to tell you about the continuing need to brush up can go to my blog to see. In this blog I'll share with you related operations using the C ++ template classes to implement the order of the stack, the main achievement of the following functions:
18VfY9.png

Private member's package

The stack order of the characteristics, encapsulates elements such as the stack array element is stored, Top representatives stack pointer, the maxSize the maximum capacity of the representative stack, which also encapsulates a overflowProcess () function to process the overflow stack.

   private:
		T* elements;//存放栈中元素的数组
		int top;//栈顶元素的指针
		int maxSize;//栈的最大容纳元素个数
		void overflowProcess();//栈的溢出处理操作

overflowProcess () function is implemented as follows
in the case where the full stack, in accordance with capacity of 2 times the maximum capacity of the stack to expand and re-allocation of space assigned to the array elements.

template <class T>
inline void Stack<T>::overflowProcess()
{
	this->maxSize = int (2*this->maxSize);
	T * temp = new T [this->maxSize];
	for(int i = 0; i <= this->top ; i++){
		temp[i] = this->elements[i];
	}
	delete []elements;       //释放原来的空间
	this ->elements = temp;
}

Public function of the package

Stacks on the nature and ultimately, 进栈,出栈,判空,判满and other related operations, the following were introduced to you:

No-argument constructor

The default maximum capacity of 10, can be adjusted itself.

template<class T> Stack<T>::Stack()   
{   
	this ->maxSize = 10;
	this ->top = -1;
	this ->elements = new T[this ->maxSize];
	if(elements==NULL){
			cout<<"动态分配错误!"<<endl;
		}
}

There arg constructor

According to their transfer size to allocate storage space size

template <class T>
inline Stack<T>::Stack(int size)
{
	this ->maxSize = size;
		this ->top = -1;
		this ->elements = new T[this ->maxSize];
		if(elements==NULL){
				cout<<"动态分配错误!"<<endl;
			}
}

The sequence of stack "push" operation

Before the stacking operation, must first determine whether the stack overflows, if the stack isFull (), the overflow process, otherwise it is inserted into the top of the stack.

template <class T>
inline void Stack<T>::Push(const T &x)
{
	if(isFull()==true){
		cout<<"The stack is full , so need to enlarge 2x!"<<endl;
		overflowProcess();//溢出处理,调整空间大小
		elements[++top]=x;
	}
	
	else{
		elements[++top]=x;//将x入栈
	}
}

The sequence of stack "pop" operations

The top element from the stack, if the stack is empty return false; stack if the stack is not empty, the top element, top pointer is decremented by one to OK.

template <class T>
inline bool Stack<T>::Pop(T& x)
{
	if(isEmpty())return false;
	
	else{
		
		x=getTopelements();
		
		top--;
		return true;
	}
}

Stack the order of "full sentence."

Determine whether the stack is full, just to top pointer and the maximum capacity of the line to compare returns true if full, full return false

template <class T>
inline bool Stack<T>::isFull()
{
	if(this->getTop()<this->getMaxsize()-1){
		return false;
	}
	else{
		return true;
	}
}

Stack the order of "empty judgment"

Analyzing the stack is empty, a direct determination top pointer returns true if the full, full return false

template <class T>
inline bool Stack<T>::isEmpty()
{
	if(this->getTop()==-1){
		return true;
	}
	else{
		return false;
	}
}

Print order stack elements

template <class T>
inline void Stack<T>::print()
{
	if(isEmpty()){
		
		cout<<"This Stack is empty!"<<endl;
	}
	cout<<"栈的元素为:"<<endl;
	for(int i=0;i<getTop();i++)
	{
		cout<<"["<<elements[i]<<"]<-";
	}	
	cout<<"["<<elements[getTop()]<<"]"<<endl;
		
}

Here C ++ implementation sequence of stack of paper over, relatively speaking quite simple, complete code I've uploaded to GitHub ( C ++ implementation sequence of stack ), welcomed the Star! Had intended to put the order in the stack also written in this together, they work really big articles, but also afraid we can not accept, so I update myself on the back. Share other data structures we can come to my blog , we discuss, I am also a student, if there is a bad place to write, but also forget the great God raised from various quarters, I correct them!

Published 19 original articles · won praise 8 · views 608

Guess you like

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