• 顺序栈


    SeqStack.h

    template<typename Type> class SeqStack{
    public:
    	SeqStack(int sz):m_ntop(-1),m_nMaxSize(sz){
    		m_pelements=new Type[sz];
    		if(m_pelements==NULL){
    			cout<<"Application Error!"<<endl;
    			exit(1);
    		}
    	}
    	~SeqStack(){
    		delete[] m_pelements;
    	}
    
    public:
    
    	void Push(const Type item); //push data
    	Type Pop();                 //pop data
    	Type GetTop() const;        //get data
        void Print();               //print the stack
    	void MakeEmpty(){           //make the stack empty
    		m_ntop=-1;
    	}
    	bool IsEmpty() const{
    		return m_ntop==-1;
    	}
    	bool IsFull() const{
    		return m_ntop==m_nMaxSize-1;
    	}
    	
    
    private:
    	int m_ntop;
    	Type *m_pelements;
    	int m_nMaxSize;
    
    };
    
    template<typename Type> void SeqStack<Type>::Push(const Type item){
    	if(IsFull()){
    		cout<<"The stack is full!"<<endl;
    		return;
    	}
    	m_pelements[++m_ntop]=item;
    }
    
    template<typename Type> Type SeqStack<Type>::Pop(){
    	if(IsEmpty()){
    		cout<<"There is no element!"<<endl;
    		exit(1);
    	}
    	return m_pelements[m_ntop--];
    }
    
    template<typename Type> Type SeqStack<Type>::GetTop() const{
    	if(IsEmpty()){
    		cout<<"There is no element!"<<endl;
    		exit(1);
    	}
    	return m_pelements[m_ntop];
    }
    
    template<typename Type> void SeqStack<Type>::Print(){
    	cout<<"bottom";
    	for(int i=0;i<=m_ntop;i++){
    		cout<<"--->"<<m_pelements[i];
    	}
    	cout<<"--->top"<<endl<<endl<<endl;
    }
    

    Test.cpp
    #include<iostream>
    using namespace std;
    
    #include "SeqStack.h"
    
    int main(){
    	SeqStack<int> stack(10);
    	int init[10]={1,2,6,9,0,3,8,7,5,4};
    	for(int i=0;i<10;i++){
    		stack.Push(init[i]);
    	}
    	stack.Print();
    
    	stack.Push(88);
    
    	cout<<stack.Pop()<<endl;
    	stack.Print();
    	
    	stack.MakeEmpty();
    	stack.Print();
    
    	stack.Pop();
    	return 0;
    }
    
  • 相关阅读:
    VS开发工具 因插件问题导致 已停止工作 解决办法
    niceScroll 简单使用 及 插件API
    使用JQGrid 问题汇总 不定时更新
    H5特性 MutationObserver 监听元素 动态改变iframe高度
    UVALIVE 3972 March of the Penguins
    Codeforces #105 DIV2 ABCDE
    UVALIVE 3644 X-Plosives
    UVALIVE 3645 Objective: Berlin
    UVALIVE 3031 Cable TV Network
    UVALIVE 2927 "Shortest" pair of paths
  • 原文地址:https://www.cnblogs.com/rollenholt/p/2438197.html
Copyright © 2020-2023  润新知