zoukankan      html  css  js  c++  java
  • C++语言实现顺序栈

    C++语言实现顺序栈

    在写C语言实现顺序栈的时候,我已经向大家介绍了栈的特点以及介绍了栈的相关操作,并利用C语言实现了相关算法。在这里小编就不在继续给大家介绍了,需要温习的可以去我的博客看看。在这篇博客我就给大家分享一下利用C++模板类来实现顺序栈的相关操作,主要实现了以下功能:
    18VfY9.png

    私有成员的封装

    根据顺序栈的特点,封装了elements这样的数组存放栈中元素,top代表栈顶指针,maxSize代表栈的最大容纳量,其中还封装了一个overflowProcess()函数来对栈的溢出进行处理。

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

    overflowProcess()函数的实现如下
    在栈满的情况下,按照最大容纳量的2倍来扩展栈的容纳量,重新分配空间赋值给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;
    }
    
    

    公有函数的封装

    栈的操作自然少不了进栈,出栈,判空,判满等相关操作,下面分别给大家进行介绍:

    无参构造函数

    默认最大容纳量为10,可自行进行调整。

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

    有参构造函数

    根据自身传递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;
    			}
    }
    

    顺序栈的“入栈”操作

    入栈操作前,首先要判断栈是否溢出,如果栈isFull(),则进行溢出处理,否则将其插入到栈顶。

    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入栈
    	}
    }
    

    顺序栈的“出栈”操作

    栈顶元素出栈,如果栈为空返回false;若栈不为空,栈顶元素出栈,top指针减一就OK啦。

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

    顺序栈的“判满”

    判断栈是否满,只需要将top指针与最大容纳量进行比较就行,如果满返回true,未满返回false

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

    顺序栈的“判空”

    判断栈是否空,直接判断top指针,如果满返回true,未满返回false

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

    打印顺序栈的元素

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

    到这里C++实现顺序栈的文章就结束了,相对而言还是挺简单的,完整的代码我已经上传到github(C++实现顺序栈),欢迎Star! 本来打算把顺序栈的应用也写在这篇文章一起的,工作量实在挺大,也害怕大家接受不了,所以我就后面在更新喽。其他的数据结构的分享大家可以来我的博客,我们一起讨论,我也是一个学生,如果有写的不好的地方,还忘各路大神提出来,我加以改正!

  • 相关阅读:
    UML描述
    Tomcat优化
    Tomcat源码
    Tomcat架构
    搭建K8s集群[无需科学shangwang]
    minikube安装
    K8S核心组件和架构图
    Docker数据持久化
    Java 内存溢出(java.lang.OutOfMemoryError)的常见情况和处理方式
    cookie与session区别?
  • 原文地址:https://www.cnblogs.com/xiangjunhong/p/12482459.html
Copyright © 2011-2022 走看看